MediaWiki  1.33.0
OATHAuthHooks.php
Go to the documentation of this file.
1 <?php
22 
34  public static function getOATHUserRepository() {
35  global $wgOATHAuthDatabase;
36 
37  static $service = null;
38 
39  if ( $service == null ) {
40  $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
41  $service = new OATHUserRepository(
42  $factory->getMainLB( $wgOATHAuthDatabase ),
43  new HashBagOStuff(
44  [
45  'maxKeys' => 5,
46  ]
47  )
48  );
49  }
50 
51  return $service;
52  }
53 
63  public static function onAuthChangeFormFields(
64  array $requests, array $fieldInfo, array &$formDescriptor, $action
65  ) {
66  if ( isset( $fieldInfo['OATHToken'] ) ) {
67  $formDescriptor['OATHToken'] += [
68  'cssClass' => 'loginText',
69  'id' => 'wpOATHToken',
70  'size' => 20,
71  'autofocus' => true,
72  'persistent' => false,
73  'autocomplete' => false,
74  'spellcheck' => false,
75  ];
76  }
77  return true;
78  }
79 
90  public static function onTwoFactorIsEnabled( &$isEnabled ) {
91  global $wgUser;
92 
93  $user = self::getOATHUserRepository()->findByUser( $wgUser );
94  if ( $user && $user->getKey() !== null ) {
95  $isEnabled = true;
96  # This two-factor extension is enabled by the user,
97  # we don't need to check others.
98  return false;
99  } else {
100  $isEnabled = false;
101  # This two-factor extension isn't enabled by the user,
102  # but others may be.
103  return true;
104  }
105  }
106 
114  public static function onGetPreferences( User $user, array &$preferences ) {
115  $oathUser = self::getOATHUserRepository()->findByUser( $user );
116 
117  // If there is no existing key, and the user is not allowed to enable it,
118  // we have nothing to show.
119  if ( $oathUser->getKey() === null && !$user->isAllowed( 'oathauth-enable' ) ) {
120  return true;
121  }
122 
123  $msg = $oathUser->getKey() !== null ? 'oathauth-disable' : 'oathauth-enable';
124 
125  $control = new \OOUI\ButtonWidget( [
126  'href' => SpecialPage::getTitleFor( 'OATH' )->getLinkURL( [
127  'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText()
128  ] ),
129  'label' => wfMessage( $msg )->text(),
130  ] );
131 
132  $preferences[$msg] = [
133  'type' => 'info',
134  'raw' => true,
135  'default' => (string)$control,
136  'label-message' => 'oathauth-prefs-label',
137  'section' => 'personal/info', ];
138 
139  return true;
140  }
141 
146  public static function onLoadExtensionSchemaUpdates( $updater ) {
147  $base = dirname( __DIR__ );
148  switch ( $updater->getDB()->getType() ) {
149  case 'mysql':
150  case 'sqlite':
151  $updater->addExtensionTable( 'oathauth_users', "$base/sql/mysql/tables.sql" );
152  $updater->addExtensionUpdate( [ [ __CLASS__, 'schemaUpdateOldUsersFromInstaller' ] ] );
153  $updater->dropExtensionField(
154  'oathauth_users',
155  'secret_reset',
156  "$base/sql/mysql/patch-remove_reset.sql"
157  );
158  break;
159 
160  case 'oracle':
161  $updater->addExtensionTable( 'oathauth_users', "$base/sql/oracle/tables.sql" );
162  break;
163 
164  case 'postgres':
165  $updater->addExtensionTable( 'oathauth_users', "$base/sql/postgres/tables.sql" );
166  break;
167  }
168 
169  return true;
170  }
171 
181  return self::schemaUpdateOldUsers( $updater->getDB() );
182  }
183 
191  public static function schemaUpdateOldUsers( IDatabase $db ) {
192  if ( !$db->fieldExists( 'oathauth_users', 'secret_reset' ) ) {
193  return true;
194  }
195 
196  $res = $db->select(
197  'oathauth_users',
198  [ 'id', 'scratch_tokens' ],
199  [ 'is_validated != 0' ],
200  __METHOD__
201  );
202 
203  foreach ( $res as $row ) {
204  Wikimedia\suppressWarnings();
205  $scratchTokens = unserialize( base64_decode( $row->scratch_tokens ) );
206  Wikimedia\restoreWarnings();
207  if ( $scratchTokens ) {
208  $db->update(
209  'oathauth_users',
210  [ 'scratch_tokens' => implode( ',', $scratchTokens ) ],
211  [ 'id' => $row->id ],
212  __METHOD__
213  );
214  }
215  }
216 
217  // Remove rows from the table where user never completed the setup process
218  $db->delete( 'oathauth_users', [ 'is_validated' => 0 ], __METHOD__ );
219 
220  return true;
221  }
222 }
OATHAuthHooks\onAuthChangeFormFields
static onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
Definition: OATHAuthHooks.php:63
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
DatabaseUpdater
Class for handling database updates.
Definition: DatabaseUpdater.php:36
OATHAuthHooks\onTwoFactorIsEnabled
static onTwoFactorIsEnabled(&$isEnabled)
Determine if two-factor authentication is enabled for $wgUser.
Definition: OATHAuthHooks.php:90
OATHUserRepository
Definition: OATHUserRepository.php:23
OATHAuthHooks
Hooks for Extension:OATHAuth.
Definition: OATHAuthHooks.php:28
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:82
$formDescriptor
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead & $formDescriptor
Definition: hooks.txt:2064
$res
$res
Definition: database.txt:21
$base
$base
Definition: generateLocalAutoload.php:11
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
string
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition: hooks.txt:175
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
Wikimedia\Rdbms\IDatabase\update
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
OATHAuthHooks\onGetPreferences
static onGetPreferences(User $user, array &$preferences)
Add the necessary user preferences for OATHAuth.
Definition: OATHAuthHooks.php:114
OATHAuthHooks\schemaUpdateOldUsers
static schemaUpdateOldUsers(IDatabase $db)
Helper function for converting old users to the new schema.
Definition: OATHAuthHooks.php:191
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
Wikimedia\Rdbms\IDatabase\fieldExists
fieldExists( $table, $field, $fname=__METHOD__)
Determines whether a field exists in a table.
OATHAuthHooks\onLoadExtensionSchemaUpdates
static onLoadExtensionSchemaUpdates( $updater)
Definition: OATHAuthHooks.php:146
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$requests
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead. & $feedLinks hooks can tweak the array to change how login etc forms should look $requests
Definition: hooks.txt:273
Wikimedia\Rdbms\IDatabase\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
$updater
$page->newPageUpdater($user) $updater
Definition: pageupdater.txt:63
OATHAuthHooks\schemaUpdateOldUsersFromInstaller
static schemaUpdateOldUsersFromInstaller(DatabaseUpdater $updater)
Helper function for converting old users to the new schema.
Definition: OATHAuthHooks.php:180
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
OATHAuthHooks\getOATHUserRepository
static getOATHUserRepository()
Get the singleton OATH user repository.
Definition: OATHAuthHooks.php:34
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
Wikimedia\Rdbms\IDatabase\delete
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
MediaWiki\Auth\AuthenticationRequest
This is a value object for authentication requests.
Definition: AuthenticationRequest.php:37