robotstxt-2fa/includes/login/class-email-code-sender.php
2026-06-05 14:07:16 +00:00

160 lines
4.1 KiB
PHP

<?php
/**
* Email verification code sender.
*
* @package Robotstxt_2FA
*/
namespace Robotstxt\TwoFA\Login;
use Robotstxt\TwoFA\User\User_Settings_Repository;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles generation and delivery of email-based verification codes.
*/
class Email_Code_Sender {
/**
* Length of the generated numeric code.
*/
private const CODE_LENGTH = 7;
/**
* Number of seconds the generated code remains valid.
*/
private const EXPIRATION_SECONDS = 600;
/**
* Minimum delay between email deliveries in seconds.
*/
private const RESEND_INTERVAL = 60;
/**
* User settings repository.
*
* @var User_Settings_Repository
*/
private User_Settings_Repository $user_settings_repository;
/**
* Constructor.
*/
public function __construct() {
$this->user_settings_repository = new User_Settings_Repository();
}
/**
* Retrieve the expected code length.
*
* @return int
*/
public function get_code_length(): int {
return self::CODE_LENGTH;
}
/**
* Retrieve the minimum seconds that must elapse between resends.
*
* @return int
*/
public function get_resend_interval(): int {
return self::RESEND_INTERVAL;
}
/**
* Send a verification code to the provided user.
*
* @param \WP_User $user User who is requesting authentication.
*
* @return bool True when the email was sent or throttled successfully, false on failure.
*/
public function send_code( \WP_User $user ): bool {
if ( empty( $user->user_email ) || ! is_email( $user->user_email ) ) {
return false;
}
$challenge = $this->user_settings_repository->get_email_challenge( $user->ID );
$current_time = time();
if ( ! empty( $challenge['hash'] ) && $challenge['expires'] > $current_time && $challenge['sent_at'] >= ( $current_time - self::RESEND_INTERVAL ) ) {
return true;
}
$code = $this->generate_code();
$expires = $current_time + self::EXPIRATION_SECONDS;
$raw_blogname = get_option( 'blogname', '' );
$blog_name = wp_specialchars_decode( is_string( $raw_blogname ) ? $raw_blogname : '', ENT_QUOTES );
$display_name = wp_strip_all_tags( ! empty( $user->display_name ) ? $user->display_name : $user->user_login );
/* translators: %s: site name. */
$email_subject = sprintf( __( '[%s] Your verification code', 'robotstxt-2fa' ), ! empty( $blog_name ) ? $blog_name : __( 'WordPress', 'robotstxt-2fa' ) );
$message_lines = array(
/* translators: %s: user display name. */
sprintf( __( 'Hi %s,', 'robotstxt-2fa' ), $display_name ),
'',
/* translators: %s: site name. */
sprintf( __( 'Use the following verification code to finish signing in to %s:', 'robotstxt-2fa' ), ! empty( $blog_name ) ? $blog_name : __( 'your site', 'robotstxt-2fa' ) ),
'',
$code,
'',
__( 'The code expires in 10 minutes.', 'robotstxt-2fa' ),
'',
__( 'If you did not request this code you can ignore this email.', 'robotstxt-2fa' ),
);
$message = implode( "\n", $message_lines );
$headers = array( 'Content-Type: text/plain; charset=UTF-8' );
if ( ! wp_mail( $user->user_email, $email_subject, $message, $headers ) ) {
return false;
}
$this->user_settings_repository->store_email_challenge( $user->ID, $code, $expires );
return true;
}
/**
* Verify a submitted code against the stored challenge.
*
* @param \WP_User $user User attempting to authenticate.
* @param string $code Submitted verification code.
*
* @return bool
*/
public function verify_code( \WP_User $user, string $code ): bool {
$challenge = $this->user_settings_repository->get_email_challenge( $user->ID );
$code = preg_replace( '/[^0-9]/', '', $code ) ?? '';
if ( '' === $challenge['hash'] ) {
return false;
}
if ( $challenge['expires'] <= time() ) {
return false;
}
if ( '' === $code ) {
return false;
}
return wp_check_password( $code, $challenge['hash'] );
}
/**
* Generate a random numeric verification code.
*
* @return string
*/
private function generate_code(): string {
$max_value = ( 10 ** self::CODE_LENGTH ) - 1;
$code = (string) random_int( 0, $max_value );
return str_pad( $code, self::CODE_LENGTH, '0', STR_PAD_LEFT );
}
}