diff --git a/maintenance/updateTOTPScratchTokensToArray.php b/maintenance/updateTOTPScratchTokensToArray.php new file mode 100644 index 00000000..0d07396c --- /dev/null +++ b/maintenance/updateTOTPScratchTokensToArray.php @@ -0,0 +1,57 @@ +addDescription( 'Script to update TOTP Scratch Tokens to an array' ); + $this->requireExtension( 'OATHAuth' ); + } + + public function execute() { + global $wgOATHAuthDatabase; + $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory() + ->getMainLB( $wgOATHAuthDatabase ); + $dbw = $lb->getConnectionRef( DB_MASTER, [], $wgOATHAuthDatabase ); + + if ( !UpdateTables::switchTOTPScratchTokensToArray( $dbw ) ) { + $this->error( "Failed to update TOTP Scratch Tokens.\n", 1 ); + } + $this->output( "Done.\n" ); + } +} + +$maintClass = UpdateTOTPScratchTokensToArray::class; +require_once RUN_MAINTENANCE_IF_MAIN; diff --git a/src/Hook/LoadExtensionSchemaUpdates/UpdateTables.php b/src/Hook/LoadExtensionSchemaUpdates/UpdateTables.php index a40f6e2d..0d393cf8 100644 --- a/src/Hook/LoadExtensionSchemaUpdates/UpdateTables.php +++ b/src/Hook/LoadExtensionSchemaUpdates/UpdateTables.php @@ -71,6 +71,10 @@ class UpdateTables { [ [ __CLASS__, 'schemaUpdateTOTPToMultipleKeys' ] ] ); + $this->updater->addExtensionUpdate( + [ [ __CLASS__, 'schemaUpdateTOTPScratchTokensToArray' ] ] + ); + break; case 'postgres': @@ -124,6 +128,16 @@ class UpdateTables { return self::switchTOTPToMultipleKeys( self::getDatabase() ); } + /** + * Helper function for converting single TOTP keys to multi-key system + * @param DatabaseUpdater $updater + * @return bool + * @throws ConfigException + */ + public static function schemaUpdateTOTPScratchTokensToArray( DatabaseUpdater $updater ) { + return self::switchTOTPScratchTokensToArray( self::getDatabase() ); + } + /** * Converts old, TOTP specific, column values to new structure * @param IDatabase $db @@ -219,6 +233,49 @@ class UpdateTables { return true; } + /** + * Switch scratch tokens from string to an array + * + * @param IDatabase $db + * @return bool + * @throws ConfigException + */ + public static function switchTOTPScratchTokensToArray( IDatabase $db ) { + if ( !$db->fieldExists( 'oathauth_users', 'data' ) ) { + return true; + } + + $res = $db->select( + 'oathauth_users', + [ 'id', 'data' ], + [ + 'module' => 'totp' + ], + __METHOD__ + ); + + foreach ( $res as $row ) { + $data = FormatJson::decode( $row->data, true ); + + foreach ( $data['keys'] as &$k ) { + if ( is_string( $k['scratch_tokens'] ) ) { + $k['scratch_tokens'] = explode( ',', $k['scratch_tokens'] ); + } + } + + $db->update( + 'oathauth_users', + [ + 'data' => FormatJson::encode( $data ) + ], + [ 'id' => $row->id ], + __METHOD__ + ); + } + + return true; + } + /** * Helper function for converting old users to the new schema *