This commit is contained in:
Javier Casares 2026-06-05 14:04:51 +00:00
commit f936aeb807

View file

@ -0,0 +1,259 @@
<?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<string, mixed>
*/
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();
}
$defaults = array(
'enabled' => false,
'methods' => array(),
'frequency' => Frequency_Options::FREQUENCY_SESSION,
);
$settings = wp_parse_args( $raw_settings, $defaults );
$settings['enabled'] = (bool) $settings['enabled'];
$settings['methods'] = array_values(
array_unique(
array_filter(
array_map( 'sanitize_key', (array) $settings['methods'] )
)
)
);
$settings['frequency'] = Frequency_Options::sanitize( (string) $settings['frequency'] );
return $settings;
}
/**
* Persist 2FA settings for a user.
*
* @param int $user_id User identifier.
* @param array $settings Settings to store.
*
* @return void
*/
public function save_user_settings( int $user_id, array $settings ): void {
$prepared = array(
'enabled' => ! empty( $settings['enabled'] ),
'methods' => array_values(
array_unique(
array_filter(
array_map( 'sanitize_key', $settings['methods'] ?? array() )
)
)
),
'frequency' => Frequency_Options::sanitize( (string) ( $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<string, int|string>
*/
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();
}
$record = wp_parse_args(
$record,
array(
'hash' => '',
'expires' => 0,
'sent_at' => 0,
)
);
$record['hash'] = is_string( $record['hash'] ) ? $record['hash'] : '';
$record['expires'] = (int) $record['expires'];
$record['sent_at'] = (int) $record['sent_at'];
return $record;
}
/**
* 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 ( array $a, array $b ): int {
return ( $a['verified_at'] ?? 0 ) <=> ( $b['verified_at'] ?? 0 );
}
);
$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;
}
if ( empty( $record[ $method ][ $context_key ]['verified_at'] ) ) {
return 0;
}
return (int) $record[ $method ][ $context_key ]['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 );
}
}