2014-05-11 08:16:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* @param $extraFields array
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function ChangePasswordForm( &$extraFields ) {
|
|
|
|
$tokenField = array( 'wpOATHToken', 'oathauth-token', 'password', '' );
|
|
|
|
array_push( $extraFields, $tokenField );
|
2014-05-11 08:27:11 +00:00
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
* @param $password string
|
|
|
|
* @param $newpassword string
|
|
|
|
* @param &$errorMsg string
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function AbortChangePassword( $user, $password, $newpassword, &$errorMsg ) {
|
2014-05-27 18:00:21 +00:00
|
|
|
global $wgRequest;
|
|
|
|
|
|
|
|
$token = $wgRequest->getText( 'wpOATHToken' );
|
|
|
|
$oathrepo = self::getOATHUserRepository();
|
|
|
|
$oathuser = $oathrepo->findByUser( $user );
|
|
|
|
# Though it's weird to default to true, we only want to deny
|
|
|
|
# users who have two-factor enabled and have validated their
|
|
|
|
# token.
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
if ( $oathuser->getKey() !== null ) {
|
|
|
|
$result = $oathuser->getKey()->verifyToken( $token, $oathuser );
|
|
|
|
}
|
2015-05-25 23:47:03 +00:00
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
if ( $result ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$errorMsg = 'oathauth-abortlogin';
|
2014-05-11 08:27:11 +00:00
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
* @param $password string
|
|
|
|
* @param &$abort int
|
|
|
|
* @param &$errorMsg string
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function AbortLogin( $user, $password, &$abort, &$errorMsg ) {
|
2014-05-27 18:00:21 +00:00
|
|
|
$context = RequestContext::getMain();
|
|
|
|
$request = $context->getRequest();
|
|
|
|
$output = $context->getOutput();
|
2014-05-11 08:16:03 +00:00
|
|
|
|
2014-05-27 18:00:21 +00:00
|
|
|
$oathrepo = self::getOATHUserRepository();
|
|
|
|
$oathuser = $oathrepo->findByUser( $user );
|
2014-05-11 08:27:11 +00:00
|
|
|
|
2014-05-27 18:00:21 +00:00
|
|
|
if ( $oathuser->getKey() !== null && !$request->getCheck( 'token' ) ) {
|
|
|
|
$request->setSessionData( 'oath_login', $request->getValues() );
|
|
|
|
$output->redirect( SpecialPage::getTitleFor( 'OATH' )->getFullURL( '', false, PROTO_CURRENT ) );
|
2014-05-11 08:16:03 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the necessary user preferences for OATHAuth
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array $preferences
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function manageOATH( 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
|
|
|
|
*/
|
|
|
|
public static function OATHAuthSchemaUpdates( $updater ) {
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @param DatabaseUpdater $updater
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function schemaUpdateOldUsers( DatabaseBase $db ) {
|
|
|
|
if ( !$db->fieldExists( 'oathauth_users', 'secret_reset' ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $db->select( 'oathauth_users', array( 'id', 'scratch_tokens' ), '', __METHOD__ );
|
|
|
|
|
|
|
|
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__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|