2014-05-11 08:16:03 +00:00
|
|
|
<?php
|
|
|
|
|
2015-11-16 02:52:23 +00:00
|
|
|
use MediaWiki\Auth\AuthenticationRequest;
|
|
|
|
use MediaWiki\Auth\AuthManager;
|
|
|
|
use MediaWiki\Auth\ConfirmLinkSecondaryAuthenticationProvider;
|
|
|
|
use MediaWiki\Auth\EmailNotificationSecondaryAuthenticationProvider;
|
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
/**
|
|
|
|
* Hooks for Extension:OATHAuth
|
2014-05-22 07:33:40 +00:00
|
|
|
*
|
|
|
|
* @ingroup Extensions
|
2014-05-11 08:16:03 +00:00
|
|
|
*/
|
|
|
|
class OATHAuthHooks {
|
2015-05-25 23:47:03 +00:00
|
|
|
/**
|
|
|
|
* Get the singleton OATH user repository
|
|
|
|
*
|
|
|
|
* @return OATHUserRepository
|
|
|
|
*/
|
|
|
|
public static function getOATHUserRepository() {
|
|
|
|
global $wgOATHAuthDatabase;
|
|
|
|
|
|
|
|
static $service = null;
|
|
|
|
|
|
|
|
if ( $service == null ) {
|
|
|
|
$service = new OATHUserRepository( wfGetLB( $wgOATHAuthDatabase ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $service;
|
|
|
|
}
|
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
/**
|
2015-11-16 02:52:23 +00:00
|
|
|
* Register hooks which depend on MediaWiki core version
|
2014-05-11 08:16:03 +00:00
|
|
|
*/
|
2015-11-16 02:52:23 +00:00
|
|
|
public static function onRegistration() {
|
|
|
|
global $wgDisableAuthManager, $wgAuthManagerAutoConfig;
|
|
|
|
|
|
|
|
if ( !$wgDisableAuthManager && class_exists( AuthManager::class ) ) {
|
|
|
|
$wgAuthManagerAutoConfig['secondaryauth'] += [
|
|
|
|
TOTPSecondaryAuthenticationProvider::class => [
|
|
|
|
'class' => TOTPSecondaryAuthenticationProvider::class,
|
|
|
|
// after non-interactive prroviders but before the ones that run after a
|
|
|
|
// succesful authentication
|
|
|
|
'sort' => 50,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
Hooks::register( 'AuthChangeFormFields', 'OATHAuthHooks::onAuthChangeFormFields' );
|
2014-05-11 08:16:03 +00:00
|
|
|
} else {
|
2015-11-16 02:52:23 +00:00
|
|
|
Hooks::register( 'AbortChangePassword', 'OATHAuthLegacyHooks::AbortChangePassword' );
|
|
|
|
Hooks::register( 'AbortLogin', 'OATHAuthLegacyHooks::AbortLogin' );
|
|
|
|
Hooks::register( 'ChangePasswordForm', 'OATHAuthLegacyHooks::ChangePasswordForm' );
|
2014-05-11 08:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-16 02:52:23 +00:00
|
|
|
* @param AuthenticationRequest[] $requests
|
|
|
|
* @param array $fieldInfo Field information array (union of the
|
|
|
|
* AuthenticationRequest::getFieldInfo() responses).
|
|
|
|
* @param array $formDescriptor HTMLForm descriptor. The special key 'weight' can be set
|
|
|
|
* to change the order of the fields.
|
|
|
|
* @param string $action One of the AuthManager::ACTION_* constants.
|
2014-05-11 08:16:03 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-11-16 02:52:23 +00:00
|
|
|
public static function onAuthChangeFormFields(
|
|
|
|
array $requests, array $fieldInfo, array &$formDescriptor, $action
|
|
|
|
) {
|
|
|
|
if ( isset( $fieldInfo['OATHToken'] ) ) {
|
|
|
|
$formDescriptor['OATHToken'] += [
|
|
|
|
'cssClass' => 'loginText',
|
|
|
|
'id' => 'wpOATHToken',
|
|
|
|
'size' => 20,
|
|
|
|
'autofocus' => true,
|
|
|
|
'persistent' => false,
|
|
|
|
];
|
2014-05-11 08:16:03 +00:00
|
|
|
}
|
2015-11-16 02:52:23 +00:00
|
|
|
return true;
|
2014-05-11 08:16:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 20:32:05 +00:00
|
|
|
/**
|
|
|
|
* Determine if two-factor authentication is enabled for $wgUser
|
|
|
|
*
|
2015-11-16 02:52:23 +00:00
|
|
|
* This isn't the preferred mechanism for controlling access to sensitive features
|
|
|
|
* (see AuthManager::securitySensitiveOperationStatus() for that) but there is no harm in
|
|
|
|
* keeping it.
|
2016-04-01 20:32:05 +00:00
|
|
|
*
|
2015-11-16 02:52:23 +00:00
|
|
|
* @param bool &$isEnabled Will be set to true if enabled, false otherwise
|
2016-04-01 20:32:05 +00:00
|
|
|
* @return bool False if enabled, true otherwise
|
|
|
|
*/
|
2015-11-16 02:52:23 +00:00
|
|
|
public static function onTwoFactorIsEnabled( &$isEnabled ) {
|
2016-04-01 20:32:05 +00:00
|
|
|
global $wgUser;
|
|
|
|
|
|
|
|
$user = self::getOATHUserRepository()->findByUser( $wgUser );
|
|
|
|
if ( $user && $user->getKey() !== null ) {
|
|
|
|
$isEnabled = true;
|
|
|
|
# This two-factor extension is enabled by the user,
|
|
|
|
# we don't need to check others.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$isEnabled = false;
|
|
|
|
# This two-factor extension isn't enabled by the user,
|
|
|
|
# but others may be.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
/**
|
|
|
|
* Add the necessary user preferences for OATHAuth
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array $preferences
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-11-16 02:52:23 +00:00
|
|
|
public static function onGetPreferences( User $user, array &$preferences ) {
|
2015-05-26 00:08:47 +00:00
|
|
|
if ( !$user->isAllowed( 'oathauth-enable' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-25 23:47:03 +00:00
|
|
|
$oathUser = self::getOATHUserRepository()->findByUser( $user );
|
2014-05-11 08:16:03 +00:00
|
|
|
|
|
|
|
$title = SpecialPage::getTitleFor( 'OATH' );
|
2014-05-22 07:33:40 +00:00
|
|
|
$msg = $oathUser->getKey() !== null ? 'oathauth-disable' : 'oathauth-enable';
|
|
|
|
|
|
|
|
$preferences[$msg] = array(
|
|
|
|
'type' => 'info',
|
|
|
|
'raw' => 'true',
|
|
|
|
'default' => Linker::link(
|
|
|
|
$title,
|
|
|
|
wfMessage( $msg )->escaped(),
|
|
|
|
array(),
|
|
|
|
array( 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() )
|
|
|
|
),
|
|
|
|
'label-message' => 'oathauth-prefs-label',
|
|
|
|
'section' => 'personal/info',
|
|
|
|
);
|
2014-05-11 08:16:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DatabaseUpdater $updater
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-11-16 02:52:23 +00:00
|
|
|
public static function onLoadExtensionSchemaUpdates( $updater ) {
|
2014-05-11 08:16:03 +00:00
|
|
|
$base = dirname( __FILE__ );
|
|
|
|
switch ( $updater->getDB()->getType() ) {
|
|
|
|
case 'mysql':
|
2014-06-30 13:20:39 +00:00
|
|
|
case 'sqlite':
|
2014-05-19 00:05:59 +00:00
|
|
|
$updater->addExtensionTable( 'oathauth_users', "$base/sql/mysql/tables.sql" );
|
|
|
|
$updater->addExtensionUpdate( array( array( __CLASS__, 'schemaUpdateOldUsersFromInstaller' ) ) );
|
|
|
|
$updater->dropExtensionField( 'oathauth_users', 'secret_reset',
|
|
|
|
"$base/sql/mysql/patch-remove_reset.sql" );
|
2014-05-11 08:16:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for converting old users to the new schema
|
|
|
|
* @see OATHAuthHooks::OATHAuthSchemaUpdates
|
|
|
|
*
|
|
|
|
* @param DatabaseUpdater $updater
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function schemaUpdateOldUsersFromInstaller( DatabaseUpdater $updater ) {
|
|
|
|
return self::schemaUpdateOldUsers($updater->getDB());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for converting old users to the new schema
|
|
|
|
* @see OATHAuthHooks::OATHAuthSchemaUpdates
|
|
|
|
*
|
2015-11-16 02:52:23 +00:00
|
|
|
* @param DatabaseBase $db
|
2014-05-19 00:05:59 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function schemaUpdateOldUsers( DatabaseBase $db ) {
|
|
|
|
if ( !$db->fieldExists( 'oathauth_users', 'secret_reset' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:54:29 +00:00
|
|
|
$res = $db->select(
|
|
|
|
'oathauth_users',
|
|
|
|
array( 'id', 'scratch_tokens' ),
|
|
|
|
array( 'is_validated != 0' ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2014-05-19 00:05:59 +00:00
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$scratchTokens = unserialize( base64_decode( $row->scratch_tokens ) );
|
|
|
|
if ( $scratchTokens ) {
|
|
|
|
$db->update(
|
|
|
|
'oathauth_users',
|
|
|
|
array( 'scratch_tokens' => implode( ',', $scratchTokens ) ),
|
|
|
|
array( 'id' => $row->id ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:54:29 +00:00
|
|
|
// Remove rows from the table where user never completed the setup process
|
|
|
|
$db->delete( 'oathauth_users', array( 'is_validated' => 0 ), __METHOD__ );
|
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|