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

+22 -8
View File
@@ -54,8 +54,8 @@ var (
// a time rejects the zero time.
//
// Lock order: an operation that changes both an account and its email tokens
// locks the tokens first and the account second, as Consume and
// MarkEmailVerified do in a confirmation. A re-registration or a resend
// locks the tokens first and the account second, as Consume and ConfirmEmail
// do in a confirmation. A re-registration or a resend
// therefore calls InvalidateActive before UpdateRegistration or Insert; the
// opposite order can deadlock against a concurrent confirmation. The only
// exception is DeleteUnverifiedRegisteredBefore, which locks the account
@@ -112,11 +112,17 @@ type UserRepository interface {
// MarkEmailVerified sets the verification time when the account is still
// unverified and still has registrationVersion, and returns the stored
// account. Otherwise it returns ErrUserVersionConflict. A confirmation
// calls it after EmailTokenRepository.Consume in the same transaction; see
// Consume.
// account. Otherwise it returns ErrUserVersionConflict. It keeps the
// storage operation available to existing callers; registration-token
// confirmation uses ConfirmEmail to apply pending credentials as well.
MarkEmailVerified(ctx context.Context, id uuid.UUID, registrationVersion int64, at time.Time) (domain.User, error)
// ConfirmEmail applies the pending credentials of one registration and
// verifies the account only while that registration version is current.
// It returns ErrUserVersionConflict when the account is already verified
// or its registration has changed.
ConfirmEmail(ctx context.Context, id uuid.UUID, registrationVersion int64, pending domain.PendingRegistration, at time.Time) (domain.User, error)
// DeleteUnverifiedRegisteredBefore deletes at most limit accounts that are
// unverified and were last registered before cutoff, oldest first, with
// their tokens, sessions, mail deliveries, and consent records, and
@@ -133,6 +139,13 @@ type UserRepository interface {
// EmailTokenRepository stores email token hashes. It never receives a raw
// token.
type EmailTokenRepository interface {
// FindByHash returns the token row, including pending registration data,
// whether or not it has already ended. It returns ErrEmailTokenNotFound
// when the hash or purpose does not match, and ErrEmailTokenInvalidated
// when its registration version is no longer current. Callers that use this
// read to prepare work must recheck the token with Consume in their transaction.
FindByHash(ctx context.Context, hash domain.SecretTokenHash, purpose domain.EmailTokenPurpose) (domain.EmailToken, error)
// Insert returns ErrUserNotFound when the account does not exist and
// ErrTokenHashConflict for a duplicate hash.
Insert(ctx context.Context, token domain.EmailToken) error
@@ -147,8 +160,9 @@ type EmailTokenRepository interface {
// many it marked.
InvalidateActive(ctx context.Context, userID uuid.UUID, purpose domain.EmailTokenPurpose, at time.Time) (int64, error)
// Consume atomically marks the token consumed at the given time and
// returns it. Of concurrent calls with the same hash exactly one succeeds;
// Consume atomically marks the token consumed at the given time, clears its
// pending registration data, and returns the token with that data from
// before the update. Of concurrent calls with the same hash exactly one succeeds;
// the others wait for its transaction and then fail. It fails with
// ErrEmailTokenNotFound, ErrEmailTokenConsumed, ErrEmailTokenInvalidated,
// or ErrEmailTokenExpired, in that order of precedence.
@@ -157,7 +171,7 @@ type EmailTokenRepository interface {
// as its statement reads the account, without locking it, so a
// re-registration that commits right after it is not detected. Binding the
// token to the current registration therefore requires a confirmation to
// call Users().MarkEmailVerified(token.UserID, token.RegistrationVersion)
// call Users().ConfirmEmail(token.UserID, token.RegistrationVersion, ...)
// in the same transaction and to roll back when it returns
// ErrUserVersionConflict; the rollback also restores the token. The
// confirmation usecase maps that outcome to verification_token_invalid.