robotstxt-2fa/includes/user/class-user-settings-repository.php
2026-06-05 14:06:16 +00:00

257 lines
7.1 KiB
PHP

<?php
/**
* Manage persistence of user two-factor settings.
*
* @package Robotstxt_2FA
*/
namespace Robotstxt\TwoFA\User;
use Robotstxt\TwoFA\Frequency_Options;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Provides CRUD helpers for user 2FA preferences and challenges.
*/
class User_Settings_Repository {
/**
* User meta key storing 2FA settings.
*
* @var string
*/
private const USER_SETTINGS_META = 'robotstxt_2fa_user_settings';
/**
* User meta key storing email challenges.
*
* @var string
*/
private const EMAIL_CHALLENGE_META = 'robotstxt_2fa_email_challenge';
/**
* User meta key storing last successful verifications per context.
*
* @var string
*/
private const LAST_VERIFICATIONS_META = 'robotstxt_2fa_last_verifications';
/**
* Maximum number of device contexts stored per verification method.
*/
private const CONTEXT_LIMIT = 20;
/**
* Retrieve stored 2FA settings for a user.
*
* @param int $user_id User identifier.
*
* @return array{enabled: bool, methods: array<int, string>, frequency: string}
*/
public function get_user_settings( int $user_id ): array {
$raw_settings = get_user_meta( $user_id, self::USER_SETTINGS_META, true );
if ( ! is_array( $raw_settings ) ) {
$raw_settings = array();
}
$raw_methods = isset( $raw_settings['methods'] ) && is_array( $raw_settings['methods'] ) ? $raw_settings['methods'] : array();
return array(
'enabled' => ! empty( $raw_settings['enabled'] ),
'methods' => array_values(
array_unique(
array_filter(
array_map(
static function ( mixed $v ): string {
return sanitize_key( is_string( $v ) ? $v : '' );
},
$raw_methods
)
)
)
),
'frequency' => Frequency_Options::sanitize( isset( $raw_settings['frequency'] ) && is_string( $raw_settings['frequency'] ) ? $raw_settings['frequency'] : '' ),
);
}
/**
* Persist 2FA settings for a user.
*
* @param int $user_id User identifier.
* @param array<string, mixed> $settings Settings to store.
*
* @return void
*/
public function save_user_settings( int $user_id, array $settings ): void {
$raw_methods = isset( $settings['methods'] ) && is_array( $settings['methods'] ) ? $settings['methods'] : array();
$prepared = array(
'enabled' => ! empty( $settings['enabled'] ),
'methods' => array_values(
array_unique(
array_filter(
array_map(
static function ( mixed $v ): string {
return sanitize_key( is_string( $v ) ? $v : '' );
},
$raw_methods
)
)
)
),
'frequency' => Frequency_Options::sanitize( isset( $settings['frequency'] ) && is_string( $settings['frequency'] ) ? $settings['frequency'] : Frequency_Options::FREQUENCY_SESSION ),
);
update_user_meta( $user_id, self::USER_SETTINGS_META, $prepared );
if ( empty( $prepared['enabled'] ) ) {
$this->delete_email_challenge( $user_id );
$this->delete_last_verifications( $user_id );
}
}
/**
* Store email challenge details for a user.
*
* @param int $user_id User identifier.
* @param string $code Plain-text verification code.
* @param int $expires Expiration timestamp.
*
* @return void
*/
public function store_email_challenge( int $user_id, string $code, int $expires ): void {
$record = array(
'hash' => wp_hash_password( $code ),
'expires' => $expires,
'sent_at' => time(),
);
update_user_meta( $user_id, self::EMAIL_CHALLENGE_META, $record );
}
/**
* Retrieve email challenge details for a user.
*
* @param int $user_id User identifier.
*
* @return array{hash: string, expires: int, sent_at: int}
*/
public function get_email_challenge( int $user_id ): array {
$record = get_user_meta( $user_id, self::EMAIL_CHALLENGE_META, true );
if ( ! is_array( $record ) ) {
$record = array();
}
return array(
'hash' => isset( $record['hash'] ) && is_string( $record['hash'] ) ? $record['hash'] : '',
'expires' => isset( $record['expires'] ) && is_numeric( $record['expires'] ) ? (int) $record['expires'] : 0,
'sent_at' => isset( $record['sent_at'] ) && is_numeric( $record['sent_at'] ) ? (int) $record['sent_at'] : 0,
);
}
/**
* Remove stored email challenge details.
*
* @param int $user_id User identifier.
*
* @return void
*/
public function delete_email_challenge( int $user_id ): void {
delete_user_meta( $user_id, self::EMAIL_CHALLENGE_META );
}
/**
* Record a successful verification timestamp for a method and device context.
*
* @param int $user_id User identifier.
* @param string $method Verification method identifier.
* @param string $context_key Normalized device context key.
* @param int $timestamp Verification timestamp.
*
* @return void
*/
public function record_last_verification( int $user_id, string $method, string $context_key, int $timestamp ): void {
$method = sanitize_key( $method );
$context_key = sanitize_text_field( $context_key );
$timestamp = max( 0, $timestamp );
if ( '' === $method || '' === $context_key || 0 === $timestamp ) {
return;
}
$record = get_user_meta( $user_id, self::LAST_VERIFICATIONS_META, true );
if ( ! is_array( $record ) ) {
$record = array();
}
if ( ! isset( $record[ $method ] ) || ! is_array( $record[ $method ] ) ) {
$record[ $method ] = array();
}
$record[ $method ][ $context_key ] = array(
'verified_at' => $timestamp,
);
if ( count( $record[ $method ] ) > self::CONTEXT_LIMIT ) {
uasort(
$record[ $method ],
static function ( mixed $a, mixed $b ): int {
$a_ts = is_array( $a ) && isset( $a['verified_at'] ) && is_int( $a['verified_at'] ) ? $a['verified_at'] : 0;
$b_ts = is_array( $b ) && isset( $b['verified_at'] ) && is_int( $b['verified_at'] ) ? $b['verified_at'] : 0;
return $a_ts <=> $b_ts;
}
);
$record[ $method ] = array_slice( $record[ $method ], -1 * self::CONTEXT_LIMIT, null, true );
}
update_user_meta( $user_id, self::LAST_VERIFICATIONS_META, $record );
}
/**
* Retrieve the last verification timestamp for a method and context.
*
* @param int $user_id User identifier.
* @param string $method Verification method identifier.
* @param string $context_key Normalized device context key.
*
* @return int
*/
public function get_last_verification_timestamp( int $user_id, string $method, string $context_key ): int {
$method = sanitize_key( $method );
$context_key = sanitize_text_field( $context_key );
if ( '' === $method || '' === $context_key ) {
return 0;
}
$record = get_user_meta( $user_id, self::LAST_VERIFICATIONS_META, true );
if ( ! is_array( $record ) || empty( $record[ $method ] ) || ! is_array( $record[ $method ] ) ) {
return 0;
}
$entry = $record[ $method ][ $context_key ] ?? null;
if ( ! is_array( $entry ) || empty( $entry['verified_at'] ) || ! is_int( $entry['verified_at'] ) ) {
return 0;
}
return $entry['verified_at'];
}
/**
* Remove stored verification timestamps.
*
* @param int $user_id User identifier.
*
* @return void
*/
public function delete_last_verifications( int $user_id ): void {
delete_user_meta( $user_id, self::LAST_VERIFICATIONS_META );
}
}