feat(identity): confirm registration email atomically
This commit is contained in:
1 parent
8e7ce8d71c
commit
fade4e76e4
75 files changed
+4188
-403
No files matched your search
@@ -274,6 +274,34 @@ func (repository userRepository) MarkEmailVerified(
|
||||
return userFromRow(row)
|
||||
}
|
||||
|
||||
func (repository userRepository) ConfirmEmail(
|
||||
ctx context.Context,
|
||||
id uuid.UUID,
|
||||
registrationVersion int64,
|
||||
pending domain.PendingRegistration,
|
||||
at time.Time,
|
||||
) (domain.User, error) {
|
||||
const location = "userRepository.ConfirmEmail"
|
||||
if err := requireTime(location, at, domain.ErrInvalidUser); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
if pending.PasswordHash.IsZero() || !pending.Scenario.AllowsStep(pending.OnboardingStep) {
|
||||
return domain.User{}, fmt.Errorf("%s - pending registration was not built by domain.NewPendingRegistration: %w", location, domain.ErrInvalidUser)
|
||||
}
|
||||
row, err := repository.queries.ConfirmUserEmail(ctx, sqlc.ConfirmUserEmailParams{
|
||||
ID: pgUUID(id),
|
||||
RegistrationVersion: registrationVersion,
|
||||
PasswordHash: pending.PasswordHash.PHC(),
|
||||
Scenario: string(pending.Scenario),
|
||||
OnboardingStep: string(pending.OnboardingStep),
|
||||
VerifiedAt: pgTime(at),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("%s - apply pending registration: %w", location, mapNoRows(err, outbound.ErrUserVersionConflict))
|
||||
}
|
||||
return userFromRow(row)
|
||||
}
|
||||
|
||||
func (repository userRepository) DeleteUnverifiedRegisteredBefore(ctx context.Context, cutoff time.Time, limit int32) (int64, error) {
|
||||
if err := requireTime("userRepository.DeleteUnverifiedRegisteredBefore", cutoff, domain.ErrInvalidUser); err != nil {
|
||||
return 0, err
|
||||
@@ -342,6 +370,35 @@ type emailTokenRepository struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func (repository emailTokenRepository) FindByHash(
|
||||
ctx context.Context,
|
||||
hash domain.SecretTokenHash,
|
||||
purpose domain.EmailTokenPurpose,
|
||||
) (domain.EmailToken, error) {
|
||||
const location = "emailTokenRepository.FindByHash"
|
||||
if hash.IsZero() {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - token hash must be set: %w", location, domain.ErrInvalidEmailToken)
|
||||
}
|
||||
if _, err := domain.ParseEmailTokenPurpose(string(purpose)); err != nil {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - validate purpose: %w", location, err)
|
||||
}
|
||||
row, err := repository.queries.GetEmailTokenByHash(ctx, sqlc.GetEmailTokenByHashParams{
|
||||
TokenHash: hash[:],
|
||||
Purpose: string(purpose),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - select email token: %w", location, mapNoRows(err, outbound.ErrEmailTokenNotFound))
|
||||
}
|
||||
owner, err := repository.queries.GetUserByID(ctx, row.UserID)
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - read token owner: %w", location, mapNoRows(err, outbound.ErrEmailTokenInvalidated))
|
||||
}
|
||||
if owner.RegistrationVersion != row.RegistrationVersion {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - token belongs to an earlier registration: %w", location, outbound.ErrEmailTokenInvalidated)
|
||||
}
|
||||
return emailTokenFromRow(row)
|
||||
}
|
||||
|
||||
func (repository emailTokenRepository) Insert(ctx context.Context, token domain.EmailToken) error {
|
||||
if token.ID == uuid.Nil || token.TokenHash.IsZero() {
|
||||
return fmt.Errorf("emailTokenRepository.Insert - token was not built by domain.NewEmailToken: %w", domain.ErrInvalidEmailToken)
|
||||
@@ -445,7 +502,7 @@ func (repository emailTokenRepository) Consume(
|
||||
ConsumedAt: pgTime(at),
|
||||
})
|
||||
if err == nil {
|
||||
return emailTokenFromRow(row)
|
||||
return emailTokenFromRow(sqlc.EmailToken(row))
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.EmailToken{}, fmt.Errorf("emailTokenRepository.Consume - update email token: %w", mapIdentityError(err))
|
||||
|
||||
Reference in new issue
Block a user