Merge "OATHUserRepository: Remove some legacy handling"

This commit is contained in:
jenkins-bot 2022-03-25 20:44:02 +00:00 committed by Gerrit Code Review
commit 1ee5f486d6

View file

@ -26,7 +26,6 @@ use MediaWiki\MediaWikiServices;
use MWException; use MWException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use RequestContext; use RequestContext;
use stdClass;
use User; use User;
use Wikimedia\Rdbms\DBConnRef; use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\ILoadBalancer; use Wikimedia\Rdbms\ILoadBalancer;
@ -84,21 +83,13 @@ class OATHUserRepository {
->centralIdFromLocalUser( $user ); ->centralIdFromLocalUser( $user );
$res = $this->getDB( DB_REPLICA )->selectRow( $res = $this->getDB( DB_REPLICA )->selectRow(
'oathauth_users', 'oathauth_users',
'*', [ 'module', 'data' ],
[ 'id' => $uid ], [ 'id' => $uid ],
__METHOD__ __METHOD__
); );
if ( $res ) { if ( $res ) {
$data = $res->data; $module = $this->auth->getModuleByKey( $res->module );
$moduleKey = $res->module;
if ( $this->isLegacy( $res ) ) {
$module = $this->auth->getModuleByKey( 'totp' );
$data = $this->checkAndResolveLegacy( $data, $res );
} else {
$module = $this->auth->getModuleByKey( $moduleKey );
}
if ( $module === null ) { if ( $module === null ) {
// For sanity
throw new MWException( 'oathauth-module-invalid' ); throw new MWException( 'oathauth-module-invalid' );
} }
@ -204,40 +195,4 @@ class OATHUserRepository {
return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase ); return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase );
} }
/**
* @param stdClass $row
* @return bool
*/
private function isLegacy( $row ) {
if ( $row->module !== '' ) {
return false;
}
if ( property_exists( $row, 'secret' ) && $row->secret !== null ) {
return true;
}
return false;
}
/**
* Checks if the DB data is in the new format,
* if not converts old data to new
*
* @param string $data
* @param stdClass $row
* @return string
*/
private function checkAndResolveLegacy( $data, $row ) {
if ( $data ) {
// New data exists - no action required
return $data;
}
if ( property_exists( $row, 'secret' ) && property_exists( $row, 'scratch_tokens' ) ) {
return FormatJson::encode( [
'secret' => $row->secret,
'scratch_tokens' => $row->scratch_tokens
] );
}
return '';
}
} }