feat(identity): confirm registration email atomically

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 09:12:15 +03:00
1 parent 8e7ce8d71c
commit fade4e76e4
75 files changed
+4188 -403

No files matched your search

@@ -4,14 +4,29 @@ post:
description: |-
Public operation without a session. The verification link has the form `<frontend origin>/verify-email#token=<token>`: the token travels in the URL fragment, so it never reaches a server log or a Referer header. The page first removes the fragment with `history.replaceState`, then waits for an explicit user action, such as a Confirm button, before it posts the token here. A link scanner that only fetches the page therefore does not consume the token.
The server looks up the token by its hash and, in one transaction, checks that it is unused, not expired, and bound to the current registration version, marks it used, and marks the email verified. Only one of several parallel requests with the same token succeeds. An expired token answers verification_token_expired; an unknown, used, replaced, or superseded token answers verification_token_invalid; neither changes anything. For both, the client offers a new link and sign-in, because the address may already be verified.
The server looks up the token by its hash and, in one transaction, checks that it is unused, not expired, and bound to the current registration version, marks it used, and marks the email verified. Only one of several parallel requests with the same token succeeds. An expired token of the current registration answers verification_token_expired; an unknown, used, replaced, or superseded token answers verification_token_invalid, even when that token is also expired. Neither refusal changes anything. For both, the client offers a new link and sign-in, because the address may already be verified.
Confirmation also requires the password chosen for the registration that issued this token. A token from a replaced registration cannot apply another registration's password or scenario. The server checks the password against the token's pending password hash before consuming the token, then applies that token's password, scenario, and onboarding step only if the current registration's C1 consent matches its registration version. A wrong password or token without pending registration data answers verification_token_invalid and leaves the account and token unchanged.
A repeated confirmation with an already used token is not an idempotent 200: exactly one request may succeed for a token, so the outcome of parallel requests stays unambiguous, and the next step after either outcome is sign-in anyway.
Success does not start a session: the link may be opened on another device or by a mail scanner, and a session must come only from the password. The response returns the scenario and the saved onboarding step so the client can continue after sign-in. The operation is rate-limited and requires an allowed Origin.
Success does not start a session: the link may be opened on another device or by a mail scanner, and a session must come only from the password. The response returns the scenario and the saved onboarding step so the client can continue after sign-in. The operation is rate-limited and requires an allowed Origin. Missing Origin, the special value `null`, or an Origin other than the configured public application origin answers csrf_check_failed.
security: []
tags:
- identity
parameters:
- name: Origin
in: header
required: false
description: >-
The exact configured public application origin. This optional parameter
is read by the server as empty when absent, so the operation answers
csrf_check_failed rather than treating a missing Origin as an allowed
request.
schema:
type: string
minLength: 1
maxLength: 2048
requestBody:
required: true
content:
@@ -20,9 +35,10 @@ post:
$ref: ../components/schemas.yml#/EmailVerificationRequest
examples:
token:
summary: Token from the link
summary: Token from the link and password chosen during registration
value:
token: q3Zt9b2Kx7LmP4vR8sW1yC6nD0fH5jA2eG9uI3oT7kE
password: "correct horse battery staple"
responses:
"200":
description: The email is verified.
+2 -2
View File
@@ -6,8 +6,8 @@ post:
The answer is the same 202 whether the email is new, registered but not verified, or verified, so registration does not enumerate accounts:
- New email: the server creates an unverified account with the scenario and the first onboarding step, records C1 with its text version and text hash, stores only the hash of a one-time verification token, and queues the verification message, all in one transaction.
- Registered but not verified: the server treats the request as a new registration of that address. In one transaction it replaces the password hash, the scenario, the onboarding step, and the C1 record, advances the registration version, invalidates every earlier verification token, and queues a message with a new token. A pre-registration with someone else's password therefore never survives the address owner's own registration.
- New email: the server creates an unverified account with the scenario and the first onboarding step, records C1 with its text version and text hash, stores only the hash of a one-time verification token plus the pending registration credentials, and queues the verification message, all in one transaction.
- Registered but not verified: the server treats the request as a new registration of that address. In one transaction it advances the registration version, replaces the C1 record, invalidates every earlier verification token, and queues a message with a new token carrying the pending password hash, scenario, and onboarding step. The account's saved credentials and scenario change only when the address owner confirms that token with its registration password, so a pre-registration with someone else's password cannot take over the account.
- Verified: nothing changes. The server may queue a rate-limited "you already have an account" notice to the address.
Every verification token is bound to the registration version it was issued for, so a token issued before a re-registration can never confirm the newer credentials. An account that stays unverified is deleted with its consent record and tokens 7 days after its last registration (planned value from configuration). Verification tokens live 24 hours (planned value from configuration).