regenerateScratchTokens(); return $object; } /** * @see T408225, T401393 */ private static function removeBase32Padding( string $paddedBase32String ): string { return rtrim( $paddedBase32String, '=' ); } /** * @param array $data * @return TOTPKey|null on invalid data */ public static function newFromArray( array $data ) { if ( !isset( $data['secret'] ) || !isset( $data['scratch_tokens'] ) ) { return null; } return new static( $data['id'] ?? null, $data['secret'], $data['scratch_tokens'] ); } /** * @param int|null $id the database id of this key * @param string $secret * @param array $recoveryCodes */ public function __construct( ?int $id, $secret, array $recoveryCodes ) { $this->id = $id; // Currently hardcoded values; might be used in the future $this->secret = [ 'mode' => 'hotp', 'secret' => $secret, 'period' => 30, 'algorithm' => 'SHA1', ]; $this->recoveryCodes = array_values( $recoveryCodes ); } /** * @return int|null */ public function getId(): ?int { return $this->id; } /** * @return string */ public function getSecret() { return $this->secret['secret']; } /** * @return string[] */ public function getScratchTokens() { return $this->recoveryCodes; } /** * @param array $data * @param OATHUser $user * @return bool * @throws MWException */ public function verify( $data, OATHUser $user ) { global $wgOATHAuthWindowRadius; $token = $data['token']; if ( $this->secret['mode'] !== 'hotp' ) { throw new DomainException( 'OATHAuth extension does not support non-HOTP tokens' ); } // Prevent replay attacks $services = MediaWikiServices::getInstance(); $store = $services->getMainObjectStash(); if ( $store instanceof EmptyBagOStuff ) { // Try and find some usable cache if the MainObjectStash isn't useful $store = $services->getObjectCacheFactory()->getLocalServerInstance( CACHE_ANYTHING ); } $key = $store->makeKey( 'oathauth-totp', 'usedtokens', $user->getCentralId() ); $lastWindow = (int)$store->get( $key ); $results = HOTP::generateByTimeWindow( Base32::decode( $this->secret['secret'] ), $this->secret['period'], -$wgOATHAuthWindowRadius, $wgOATHAuthWindowRadius ); // Remove any whitespace from the received token, which can be an intended group separator $token = preg_replace( '/\s+/', '', $token ); $clientIP = RequestContext::getMain()->getRequest()->getIP(); $logger = $this->getLogger(); // Check to see if the user's given token is in the list of tokens generated // for the time window. foreach ( $results as $window => $result ) { if ( $window > $lastWindow && hash_equals( $result->toHOTP( 6 ), $token ) ) { $lastWindow = $window; $logger->info( 'OATHAuth user {user} entered a valid OTP from {clientip}', [ 'user' => $user->getAccount(), 'clientip' => $clientIP, ] ); $store->set( $key, $lastWindow, $this->secret['period'] * ( 1 + 2 * $wgOATHAuthWindowRadius ) ); return true; } } // See if the user is using a recovery code foreach ( $this->recoveryCodes as $i => $recoveryCode ) { if ( hash_equals( $token, $recoveryCode ) ) { // If we used a recovery code, remove it from the recovery code list. // This is saved below via OATHUserRepository::persist array_splice( $this->recoveryCodes, $i, 1 ); // TODO: Probably a better home for this... // It could go in OATHUserRepository::persist(), but then we start having to hard code checks // for Keys being TOTPKey... // And eventually we want to do T232336 to split them to their own 2FA method... if ( count( $this->recoveryCodes ) <= self::RECOVERY_CODES_NOTIFICATION_NUMBER ) { Manager::notifyRecoveryTokensRemaining( $user, self::RECOVERY_CODES_NOTIFICATION_NUMBER, self::RECOVERY_CODES_COUNT ); } $logger->info( 'OATHAuth user {user} used a recovery token from {clientip}', [ 'user' => $user->getAccount(), 'clientip' => $clientIP, ] ); OATHAuthServices::getInstance() ->getUserRepository() ->updateKey( $user, $this ); return true; } } return false; } public function regenerateScratchTokens() { $codes = []; for ( $i = 0; $i < self::RECOVERY_CODES_COUNT; $i++ ) { $codes[] = Base32::encode( random_bytes( self::RECOVERY_CODE_LENGTH ) ); } $this->recoveryCodes = $codes; } /** * Check if a token is one of the recovery codes for this two-factor key. * * @param string $token Token to verify * * @return bool true if this is a recovery code. */ public function isScratchToken( $token ) { $token = preg_replace( '/\s+/', '', $token ); return in_array( $token, $this->recoveryCodes, true ); } /** @inheritDoc */ public function getModule(): string { return TOTP::MODULE_NAME; } /** * @return LoggerInterface */ private function getLogger() { return LoggerFactory::getInstance( 'authentication' ); } public function jsonSerialize(): array { return [ 'secret' => $this->getSecret(), 'scratch_tokens' => $this->getScratchTokens() ]; } }