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 {
|
|
|
|
/**
|
|
|
|
* @param $template UserloginTemplate
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function ModifyUITemplate( &$template ) {
|
|
|
|
$input = '<div><label for="wpOATHToken">'
|
|
|
|
. wfMessage( 'oathauth-token' )->escaped()
|
|
|
|
. '</label>'
|
|
|
|
. Html::input( 'wpOATHToken', null, 'text', array(
|
|
|
|
'class' => 'loginText', 'id' => 'wpOATHToken', 'tabindex' => '3', 'size' => '20'
|
|
|
|
) ) . '</div>';
|
2014-05-11 08:27:11 +00:00
|
|
|
|
|
|
|
$template->set( 'extrafields', $template->get( 'extrafields', '' ) . $input );
|
2014-05-11 08:16:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
$result = self::authenticate( $user );
|
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 ) {
|
|
|
|
$result = self::authenticate( $user );
|
|
|
|
if ( $result ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$abort = LoginForm::ABORTED;
|
|
|
|
$errorMsg = 'oathauth-abortlogin';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
static function authenticate( $user ) {
|
|
|
|
global $wgRequest;
|
2014-05-11 08:27:11 +00:00
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
$token = $wgRequest->getText( 'wpOATHToken' );
|
2015-05-25 23:47:03 +00:00
|
|
|
$oathuser = self::getOATHUserRepository()->findByUser( $user );
|
2014-05-11 08:16:03 +00:00
|
|
|
# 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;
|
2014-05-19 00:05:59 +00:00
|
|
|
|
|
|
|
if ( $oathuser->getKey() !== null ) {
|
|
|
|
$result = $oathuser->getKey()->verifyToken( $token, $oathuser );
|
2014-05-11 08:16:03 +00:00
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2014-05-11 08:16:03 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if two-factor authentication is enabled for $wgUser
|
|
|
|
*
|
|
|
|
* @param bool &$isEnabled Will be set to true if enabled, false otherwise
|
|
|
|
*
|
|
|
|
* @return bool False if enabled, true otherwise
|
|
|
|
*/
|
|
|
|
static function TwoFactorIsEnabled( &$isEnabled ) {
|
|
|
|
global $wgUser;
|
|
|
|
|
2015-05-25 23:47:03 +00:00
|
|
|
$user = self::getOATHUserRepository()->findByUser( $wgUser );
|
2014-05-19 00:05:59 +00:00
|
|
|
if ( $user && $user->getKey() !== null ) {
|
2014-05-11 08:16:03 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|