$secret, 'uri' => self::provisioning_uri( $user_id, $secret ), ); } public static function confirm_enrolment( $user_id, $code ) { $user_id = (int) $user_id; $pending = (string) get_user_meta( $user_id, self::META_PENDING, true ); $secret = '' === $pending ? '' : AIEO_Login_Security::decrypt( $pending ); if ( '' === $secret ) { return array( 'ok' => false, 'codes' => array(), 'message' => __( 'Start again — there is no half-finished setup to confirm.', 'ai-eshop-optimizer' ), ); } $step = self::matching_step( $secret, $code ); if ( false === $step ) { return array( 'ok' => false, 'codes' => array(), 'message' => __( 'That code did not match. Check your phone\'s clock is set automatically, then try the next code.', 'ai-eshop-optimizer' ), ); } $codes = self::issue_recovery_codes( $user_id ); update_user_meta( $user_id, self::META_SECRET, AIEO_Login_Security::encrypt( $secret ) ); update_user_meta( $user_id, self::META_ACTIVE, '1' ); update_user_meta( $user_id, self::META_LAST_STEP, (string) $step ); update_user_meta( $user_id, self::META_ENROLLED, (string) time() ); delete_user_meta( $user_id, self::META_PENDING ); return array( 'ok' => true, 'codes' => $codes, 'message' => __( 'Two-factor authentication is on for this account.', 'ai-eshop-optimizer' ), ); } public static function disable( $user_id ) { $user_id = (int) $user_id; foreach ( array( self::META_SECRET, self::META_PENDING, self::META_ACTIVE, self::META_RECOVERY, self::META_LAST_STEP, self::META_ENROLLED ) as $key ) { delete_user_meta( $user_id, $key ); } } public static function verify( $user_id, $code ) { $user_id = (int) $user_id; $secret = self::secret( $user_id ); if ( '' === $secret ) { return false; } $step = self::matching_step( $secret, $code ); if ( false === $step ) { return false; } $last = (int) get_user_meta( $user_id, self::META_LAST_STEP, true ); if ( $step <= $last ) { return false; } update_user_meta( $user_id, self::META_LAST_STEP, (string) $step ); return true; } public static function verify_recovery_code( $user_id, $code ) { $user_id = (int) $user_id; $code = self::normalise_recovery( $code ); if ( '' === $code ) { return false; } $hashes = get_user_meta( $user_id, self::META_RECOVERY, true ); if ( ! is_array( $hashes ) || empty( $hashes ) ) { return false; } foreach ( $hashes as $i => $hash ) { if ( wp_check_password( $code, $hash ) ) { unset( $hashes[ $i ] ); update_user_meta( $user_id, self::META_RECOVERY, array_values( $hashes ) ); return true; } } return false; } public static function recovery_codes_left( $user_id ) { $hashes = get_user_meta( (int) $user_id, self::META_RECOVERY, true ); return is_array( $hashes ) ? count( $hashes ) : 0; } public static function issue_recovery_codes( $user_id ) { $plain = array(); $hashes = array(); for ( $i = 0; $i < self::RECOVERY_COUNT; $i++ ) { $code = strtolower( bin2hex( random_bytes( 5 ) ) ); $plain[] = substr( $code, 0, 5 ) . '-' . substr( $code, 5, 5 ); $hashes[] = wp_hash_password( $code ); } update_user_meta( (int) $user_id, self::META_RECOVERY, $hashes ); return $plain; } private static function matching_step( $secret, $code ) { $code = preg_replace( '/\D/', '', (string) $code ); if ( strlen( $code ) !== self::DIGITS ) { return false; } $now = (int) floor( time() / self::PERIOD ); for ( $offset = -self::DRIFT; $offset <= self::DRIFT; $offset++ ) { $step = $now + $offset; if ( hash_equals( self::code_at( $secret, $step ), $code ) ) { return $step; } } return false; } public static function code_at( $secret, $step ) { $key = self::base32_decode( $secret ); if ( '' === $key ) { return ''; } $binary = pack( 'NN', ( $step >> 32 ) & 0xFFFFFFFF, $step & 0xFFFFFFFF ); $hash = hash_hmac( 'sha1', $binary, $key, true ); $offset = ord( $hash[ strlen( $hash ) - 1 ] ) & 0x0F; $part = substr( $hash, $offset, 4 ); $number = ( ( ( ord( $part[0] ) & 0x7F ) << 24 ) | ( ( ord( $part[1] ) & 0xFF ) << 16 ) | ( ( ord( $part[2] ) & 0xFF ) << 8 ) | ( ord( $part[3] ) & 0xFF ) ); return str_pad( (string) ( $number % ( 10 ** self::DIGITS ) ), self::DIGITS, '0', STR_PAD_LEFT ); } public static function random_secret() { return self::base32_encode( random_bytes( 20 ) ); } public static function provisioning_uri( $user_id, $secret ) { $user = get_userdata( (int) $user_id ); if ( ! $user ) { return ''; } $issuer = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); $issuer = trim( (string) $issuer ); if ( '' === $issuer ) { $issuer = (string) wp_parse_url( home_url(), PHP_URL_HOST ); } $issuer = str_replace( ':', ' ', $issuer ); $label = rawurlencode( $issuer ) . ':' . rawurlencode( $user->user_login ); return 'otpauth://totp/' . $label . '?' . http_build_query( array( 'secret' => $secret, 'issuer' => $issuer, 'algorithm' => 'SHA1', 'digits' => self::DIGITS, 'period' => self::PERIOD, ), '', '&', PHP_QUERY_RFC3986 ); } public static function format_secret( $secret ) { return trim( chunk_split( (string) $secret, 4, ' ' ) ); } const B32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; public static function base32_encode( $bytes ) { $bits = ''; $len = strlen( $bytes ); for ( $i = 0; $i < $len; $i++ ) { $bits .= str_pad( decbin( ord( $bytes[ $i ] ) ), 8, '0', STR_PAD_LEFT ); } if ( strlen( $bits ) % 5 ) { $bits = str_pad( $bits, strlen( $bits ) + ( 5 - ( strlen( $bits ) % 5 ) ), '0', STR_PAD_RIGHT ); } $out = ''; foreach ( str_split( $bits, 5 ) as $chunk ) { $out .= self::B32_ALPHABET[ bindec( $chunk ) ]; } return $out; } public static function base32_decode( $b32 ) { $b32 = strtoupper( preg_replace( '/[^A-Za-z2-7]/', '', (string) $b32 ) ); if ( '' === $b32 ) { return ''; } $bits = ''; $len = strlen( $b32 ); for ( $i = 0; $i < $len; $i++ ) { $pos = strpos( self::B32_ALPHABET, $b32[ $i ] ); if ( false === $pos ) { continue; } $bits .= str_pad( decbin( $pos ), 5, '0', STR_PAD_LEFT ); } $out = ''; foreach ( str_split( $bits, 8 ) as $chunk ) { if ( 8 === strlen( $chunk ) ) { $out .= chr( bindec( $chunk ) ); } } return $out; } private static function normalise_recovery( $code ) { return strtolower( preg_replace( '/[^A-Fa-f0-9]/', '', (string) $code ) ); } }