Admin
4 days ago
Emails
4 days ago
CustomerEmailVerification.php
4 days ago
EmailVerificationService.php
4 days ago
VerificationController.php
4 days ago
VerificationEventListener.php
4 days ago
EmailVerificationService.php
302 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\CustomerEmailVerification; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Utilities\Users; |
| 7 | |
| 8 | /** |
| 9 | * Service class providing the foundational primitives for customer email verification. |
| 10 | * |
| 11 | * This class is the single source of truth for whether a customer has proven they |
| 12 | * control their account email address. It manages the verified-status meta and the |
| 13 | * short-lived, single-use verification key carried by the emailed verify-link, together |
| 14 | * with the helpers consumed by the rest of the email-verification feature. |
| 15 | * |
| 16 | * The verify-link is only ever *completed* by a request authenticated as the link's target |
| 17 | * user (see {@see VerificationController::handle_confirm_submission()}), so the key needs no |
| 18 | * brute-force protection: it is high-entropy ({@see self::KEY_LENGTH} chars), single-use, and |
| 19 | * expires after {@see self::KEY_TTL}. |
| 20 | * |
| 21 | * @since 11.0.0 |
| 22 | */ |
| 23 | class EmailVerificationService { |
| 24 | |
| 25 | /** |
| 26 | * Length of the generated verification key. A 20-char alphanumeric key is high-entropy enough that |
| 27 | * it needs no attempt limiting (unlike a 6-digit code), so this flow carries no lockout machinery. |
| 28 | */ |
| 29 | private const KEY_LENGTH = 20; |
| 30 | |
| 31 | /** |
| 32 | * How long a freshly minted verification key remains valid. |
| 33 | */ |
| 34 | private const KEY_TTL = DAY_IN_SECONDS; |
| 35 | |
| 36 | /** |
| 37 | * User meta key that stores the verified email address (lower-cased). |
| 38 | * The customer is considered verified only while this matches their current account email. |
| 39 | */ |
| 40 | private const VERIFIED_META = '_wc_email_verified'; |
| 41 | |
| 42 | /** |
| 43 | * User meta key that stores the verification token as "{timestamp}:{key_hash}:{email_hash}". |
| 44 | * Overwritten on every new key; deleted when the key is consumed or the user verifies. |
| 45 | */ |
| 46 | private const KEY_META = '_wc_email_verification_key'; |
| 47 | |
| 48 | /** |
| 49 | * The user's account email, lower-cased, or null when the user does not exist. |
| 50 | * |
| 51 | * Lower-casing here is the single normalisation point, so the verified-status match and the |
| 52 | * key's email-binding hash stay consistent however the address was capitalised. |
| 53 | * |
| 54 | * @param int $user_id WordPress user ID. |
| 55 | * @return string|null |
| 56 | */ |
| 57 | private function get_account_email( int $user_id ): ?string { |
| 58 | $user = get_user_by( 'id', $user_id ); |
| 59 | |
| 60 | return $user instanceof \WP_User ? strtolower( $user->user_email ) : null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Return whether the given user has verified their current account email address. |
| 65 | * |
| 66 | * A user is verified only while the stored verified email matches their current |
| 67 | * account email, so changing the account email automatically invalidates the |
| 68 | * status — no change event needs to be observed. |
| 69 | * |
| 70 | * @since 11.0.0 |
| 71 | * |
| 72 | * @param int $user_id WordPress user ID. |
| 73 | * @return bool True when the stored verified email matches the user's current email. |
| 74 | */ |
| 75 | public function is_verified( int $user_id ): bool { |
| 76 | $verified_email = (string) Users::get_site_user_meta( $user_id, self::VERIFIED_META ); |
| 77 | |
| 78 | // Both sides are lower-cased (stored that way, get_account_email() normalises), so === is exact. |
| 79 | return '' !== $verified_email && $verified_email === $this->get_account_email( $user_id ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Mark the given user as having verified their current account email address. |
| 84 | * |
| 85 | * Stores the verified email address, clears any pending key, and fires the |
| 86 | * {@see 'woocommerce_customer_email_verified'} action. No-ops if the user is already |
| 87 | * verified for their current email. |
| 88 | * |
| 89 | * @since 11.0.0 |
| 90 | * |
| 91 | * @param int $user_id WordPress user ID. |
| 92 | * @return void |
| 93 | */ |
| 94 | public function mark_verified( int $user_id ): void { |
| 95 | if ( $this->is_verified( $user_id ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $account_email = $this->get_account_email( $user_id ); |
| 100 | |
| 101 | if ( null === $account_email ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // Storing the email (not a bool) lets the status self-invalidate if the account email later changes. |
| 106 | Users::update_site_user_meta( $user_id, self::VERIFIED_META, $account_email ); |
| 107 | Users::delete_site_user_meta( $user_id, self::KEY_META ); |
| 108 | |
| 109 | /** |
| 110 | * Fires after a customer has verified their email address. |
| 111 | * |
| 112 | * @param int $user_id The WordPress user ID of the verified customer. |
| 113 | * |
| 114 | * @since 11.0.0 |
| 115 | */ |
| 116 | do_action( 'woocommerce_customer_email_verified', $user_id ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Clear the email-verification status for the given user. |
| 121 | * |
| 122 | * Removes both the verified-email meta and any pending verification key, |
| 123 | * effectively resetting the user to an unverified state. |
| 124 | * |
| 125 | * @since 11.0.0 |
| 126 | * |
| 127 | * @param int $user_id WordPress user ID. |
| 128 | * @return void |
| 129 | */ |
| 130 | public function clear_verification( int $user_id ): void { |
| 131 | Users::delete_site_user_meta( $user_id, self::VERIFIED_META ); |
| 132 | Users::delete_site_user_meta( $user_id, self::KEY_META ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Generate and store a one-time email-verification key for the given user. |
| 137 | * |
| 138 | * The plaintext key is returned for inclusion in the verification email link. The stored value is |
| 139 | * a "{timestamp}:{key_hash}:{email_hash}" triplet so the plaintext is never persisted, the key |
| 140 | * expires after {@see self::KEY_TTL}, and the email hash binds the key to the account email in |
| 141 | * effect at issuance (a key emailed to one address can never verify a different address the account |
| 142 | * is later switched to). |
| 143 | * |
| 144 | * @since 11.0.0 |
| 145 | * |
| 146 | * @param int $user_id WordPress user ID. |
| 147 | * @return string The plaintext verification key. |
| 148 | */ |
| 149 | public function create_verification_key( int $user_id ): string { |
| 150 | $key = wp_generate_password( self::KEY_LENGTH, false ); |
| 151 | $account_email = $this->get_account_email( $user_id ); |
| 152 | $email_hash = null !== $account_email ? wp_fast_hash( $account_email ) : ''; |
| 153 | |
| 154 | Users::update_site_user_meta( $user_id, self::KEY_META, time() . ':' . wp_fast_hash( $key ) . ':' . $email_hash ); |
| 155 | |
| 156 | return $key; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Build a one-time email-verification URL for the given user. |
| 161 | * |
| 162 | * Mints a fresh verification key and returns the My Account URL carrying that key and the user ID |
| 163 | * as query args, ready to drop into an email. The matching reader is |
| 164 | * {@see VerificationController::maybe_process_request()}. |
| 165 | * |
| 166 | * @since 11.0.0 |
| 167 | * |
| 168 | * @param int $user_id WordPress user ID. |
| 169 | * @return string The verification URL. |
| 170 | */ |
| 171 | public function build_verification_url( int $user_id ): string { |
| 172 | return $this->build_verification_url_for_key( $user_id, $this->create_verification_key( $user_id ) ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Build the My Account verify-link URL carrying a specific (already-issued) key. |
| 177 | * |
| 178 | * Unlike {@see self::build_verification_url()} this mints nothing — it re-emits an existing key, e.g. |
| 179 | * to bounce a logged-out visitor back to the link they opened so they can finish after signing in. |
| 180 | * |
| 181 | * @since 11.0.0 |
| 182 | * |
| 183 | * @param int $user_id WordPress user ID. |
| 184 | * @param string $key Plaintext verification key to embed. |
| 185 | * @return string The verification URL. |
| 186 | */ |
| 187 | public function build_verification_url_for_key( int $user_id, string $key ): string { |
| 188 | return add_query_arg( |
| 189 | array( |
| 190 | 'wc_verify_email_key' => $key, |
| 191 | 'wc_verify_email_user' => $user_id, |
| 192 | ), |
| 193 | wc_get_page_permalink( 'myaccount' ) |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Validate a plaintext verification key against the stored hash for the given user. |
| 199 | * |
| 200 | * Returns false if no key is stored, if the key has expired, if the account email has changed since |
| 201 | * the key was issued, or if the key does not match the stored hash. |
| 202 | * |
| 203 | * @since 11.0.0 |
| 204 | * |
| 205 | * @param int $user_id WordPress user ID. |
| 206 | * @param string $key The plaintext verification key to check. |
| 207 | * @return bool True when the key is valid and has not expired. |
| 208 | */ |
| 209 | public function check_verification_key( int $user_id, string $key ): bool { |
| 210 | if ( '' === $key ) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | $parsed = $this->parse_stored_key( $user_id ); |
| 215 | |
| 216 | if ( null === $parsed ) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | list( $timestamp, $hash, $email_hash ) = $parsed; |
| 221 | |
| 222 | if ( time() - $timestamp > self::KEY_TTL ) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | // The key is void if the account email no longer matches the one it was minted for. |
| 227 | $account_email = $this->get_account_email( $user_id ); |
| 228 | |
| 229 | if ( null === $account_email || '' === $email_hash || ! wp_verify_fast_hash( $account_email, $email_hash ) ) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return wp_verify_fast_hash( $key, $hash ); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Whether the user currently has a pending (minted, unexpired) verification key awaiting use. |
| 238 | * |
| 239 | * Used to decide whether the My Account prompt shows the "check your inbox" notice or the "send a |
| 240 | * confirmation link" call to action. |
| 241 | * |
| 242 | * @since 11.0.0 |
| 243 | * |
| 244 | * @param int $user_id WordPress user ID. |
| 245 | * @return bool |
| 246 | */ |
| 247 | public function has_pending_key( int $user_id ): bool { |
| 248 | $parsed = $this->parse_stored_key( $user_id ); |
| 249 | |
| 250 | return null !== $parsed && time() - $parsed[0] <= self::KEY_TTL; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Parse the stored verification token into its timestamp, key-hash, and email-hash parts. |
| 255 | * |
| 256 | * The token is persisted as "{timestamp}:{key_hash}:{email_hash}"; this is the single place that |
| 257 | * knows that format. |
| 258 | * |
| 259 | * @param int $user_id WordPress user ID. |
| 260 | * @return array{0: int, 1: string, 2: string}|null The triplet, or null when none is stored. |
| 261 | */ |
| 262 | private function parse_stored_key( int $user_id ): ?array { |
| 263 | $stored = (string) Users::get_site_user_meta( $user_id, self::KEY_META ); |
| 264 | |
| 265 | if ( ! str_contains( $stored, ':' ) ) { |
| 266 | return null; |
| 267 | } |
| 268 | |
| 269 | $parts = explode( ':', $stored, 3 ); |
| 270 | $timestamp = (int) ( $parts[0] ?? 0 ); |
| 271 | $hash = (string) ( $parts[1] ?? '' ); |
| 272 | $email_hash = (string) ( $parts[2] ?? '' ); |
| 273 | |
| 274 | if ( '' === $hash || '' === $email_hash || 0 === $timestamp ) { |
| 275 | return null; |
| 276 | } |
| 277 | |
| 278 | return array( $timestamp, $hash, $email_hash ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Return the number of seconds elapsed since the last verification key was issued, or null if none |
| 283 | * exists. |
| 284 | * |
| 285 | * @since 11.0.0 |
| 286 | * |
| 287 | * @param int $user_id WordPress user ID. |
| 288 | * @return int|null Seconds since the last key was created, or null when none is stored. |
| 289 | */ |
| 290 | public function seconds_since_last_key( int $user_id ): ?int { |
| 291 | $parsed = $this->parse_stored_key( $user_id ); |
| 292 | |
| 293 | if ( null === $parsed ) { |
| 294 | return null; |
| 295 | } |
| 296 | |
| 297 | // Clamp to zero so a future timestamp (clock skew, migrations) can't report negative |
| 298 | // elapsed time and wedge the resend rate-limit / "recently sent" notice logic. |
| 299 | return max( 0, time() - $parsed[0] ); |
| 300 | } |
| 301 | } |
| 302 |