feat(identity): confirm the email address with a six-digit code from the message
This commit is contained in:
1 parent
c94434cd09
commit
d365a05c96
87 files changed
+4764
-224
No files matched your search
@@ -395,7 +395,7 @@ func (repository emailTokenRepository) FindByHash(
|
||||
if owner.RegistrationVersion != row.RegistrationVersion {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - token belongs to an earlier registration: %w", location, outbound.ErrEmailTokenInvalidated)
|
||||
}
|
||||
return emailTokenFromRow(row)
|
||||
return emailTokenFromRow(emailTokenModelFromHashRow(row))
|
||||
}
|
||||
|
||||
func (repository emailTokenRepository) Insert(ctx context.Context, token domain.EmailToken) error {
|
||||
@@ -411,6 +411,10 @@ func (repository emailTokenRepository) Insert(ctx context.Context, token domain.
|
||||
CreatedAt: pgTime(token.CreatedAt),
|
||||
ExpiresAt: pgTime(token.ExpiresAt),
|
||||
}
|
||||
if code := token.Code; code != nil {
|
||||
params.CodeHash = code.Hash[:]
|
||||
params.CodeExpiresAt = pgTime(code.ExpiresAt)
|
||||
}
|
||||
if pending := token.Pending; pending != nil {
|
||||
params.PendingPasswordHash = pgtype.Text{String: pending.PasswordHash.PHC(), Valid: true}
|
||||
params.PendingScenario = pgtype.Text{String: string(pending.Scenario), Valid: true}
|
||||
@@ -501,7 +505,7 @@ func (repository emailTokenRepository) Consume(
|
||||
ConsumedAt: pgTime(at),
|
||||
})
|
||||
if err == nil {
|
||||
return emailTokenFromRow(sqlc.EmailToken(row))
|
||||
return emailTokenFromRow(consumedEmailTokenRow(row))
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.EmailToken{}, fmt.Errorf("emailTokenRepository.Consume - update email token: %w", mapIdentityError(err))
|
||||
@@ -512,6 +516,84 @@ func (repository emailTokenRepository) Consume(
|
||||
return domain.EmailToken{}, errors.New("emailTokenRepository.Consume - token became consumable after a failed attempt")
|
||||
}
|
||||
|
||||
// emailTokenModelFromHashRow maps the row of GetEmailTokenByHash, which selects
|
||||
// the token by its hash, to the table model.
|
||||
func emailTokenModelFromHashRow(row sqlc.GetEmailTokenByHashRow) sqlc.EmailToken {
|
||||
return sqlc.EmailToken{
|
||||
ID: row.ID,
|
||||
UserID: row.UserID,
|
||||
Purpose: row.Purpose,
|
||||
TokenHash: row.TokenHash,
|
||||
RegistrationVersion: row.RegistrationVersion,
|
||||
CreatedAt: row.CreatedAt,
|
||||
ExpiresAt: row.ExpiresAt,
|
||||
ConsumedAt: row.ConsumedAt,
|
||||
InvalidatedAt: row.InvalidatedAt,
|
||||
CodeHash: row.CodeHash,
|
||||
CodeExpiresAt: row.CodeExpiresAt,
|
||||
CodeFailedAttempts: row.CodeFailedAttempts,
|
||||
PendingPasswordHash: row.PendingPasswordHash,
|
||||
PendingScenario: row.PendingScenario,
|
||||
PendingOnboardingStep: row.PendingOnboardingStep,
|
||||
}
|
||||
}
|
||||
|
||||
// emailTokenModelFromOpenCodeRow maps the row of GetOpenEmailCodeByAddress to
|
||||
// the table model. The columns are the same as in GetEmailTokenByHash.
|
||||
func emailTokenModelFromOpenCodeRow(row sqlc.GetOpenEmailCodeByAddressRow) sqlc.EmailToken {
|
||||
return sqlc.EmailToken{
|
||||
ID: row.ID,
|
||||
UserID: row.UserID,
|
||||
Purpose: row.Purpose,
|
||||
TokenHash: row.TokenHash,
|
||||
RegistrationVersion: row.RegistrationVersion,
|
||||
CreatedAt: row.CreatedAt,
|
||||
ExpiresAt: row.ExpiresAt,
|
||||
ConsumedAt: row.ConsumedAt,
|
||||
InvalidatedAt: row.InvalidatedAt,
|
||||
CodeHash: row.CodeHash,
|
||||
CodeExpiresAt: row.CodeExpiresAt,
|
||||
CodeFailedAttempts: row.CodeFailedAttempts,
|
||||
PendingPasswordHash: row.PendingPasswordHash,
|
||||
PendingScenario: row.PendingScenario,
|
||||
PendingOnboardingStep: row.PendingOnboardingStep,
|
||||
}
|
||||
}
|
||||
|
||||
// consumedEmailTokenRow maps the row of ConsumeEmailToken, which holds the token
|
||||
// as it was before the update, to the table model. The update clears the code,
|
||||
// so the returned row has none to map.
|
||||
func consumedEmailTokenRow(row sqlc.ConsumeEmailTokenRow) sqlc.EmailToken {
|
||||
return sqlc.EmailToken{
|
||||
ID: row.ID,
|
||||
UserID: row.UserID,
|
||||
Purpose: row.Purpose,
|
||||
TokenHash: row.TokenHash,
|
||||
RegistrationVersion: row.RegistrationVersion,
|
||||
CreatedAt: row.CreatedAt,
|
||||
ExpiresAt: row.ExpiresAt,
|
||||
ConsumedAt: row.ConsumedAt,
|
||||
InvalidatedAt: row.InvalidatedAt,
|
||||
PendingPasswordHash: row.PendingPasswordHash,
|
||||
PendingScenario: row.PendingScenario,
|
||||
PendingOnboardingStep: row.PendingOnboardingStep,
|
||||
}
|
||||
}
|
||||
|
||||
// emailCodeFromColumns decodes the code state of a token row. A NULL digest
|
||||
// means the token carries no code.
|
||||
func emailCodeFromColumns(digest []byte, expiresAt pgtype.Timestamptz) (*domain.EmailCode, error) {
|
||||
if len(digest) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var hash domain.VerificationCodeHash
|
||||
if len(digest) != len(hash) || !expiresAt.Valid {
|
||||
return nil, errors.New("postgres.emailCodeFromColumns - stored code state is incomplete")
|
||||
}
|
||||
copy(hash[:], digest)
|
||||
return &domain.EmailCode{Hash: hash, ExpiresAt: expiresAt.Time}, nil
|
||||
}
|
||||
|
||||
// explainUnconsumable classifies a token that the atomic consume did not
|
||||
// match. A nil result means the token is consumable again, which only a
|
||||
// concurrent transaction that changed and then rolled back the row can cause.
|
||||
@@ -540,6 +622,43 @@ func (repository emailTokenRepository) explainUnconsumable(
|
||||
}
|
||||
}
|
||||
|
||||
// FindOpenCodeByAddress reads the newest open token with a code of the
|
||||
// unverified account at the address. The account is found by the normalized
|
||||
// address; the code itself is compared by the usecase.
|
||||
func (repository emailTokenRepository) FindOpenCodeByAddress(ctx context.Context, email domain.EmailAddress) (domain.EmailToken, error) {
|
||||
const location = "emailTokenRepository.FindOpenCodeByAddress"
|
||||
row, err := repository.queries.GetOpenEmailCodeByAddress(ctx, sqlc.GetOpenEmailCodeByAddressParams{
|
||||
EmailNormalized: email.Normalized(),
|
||||
Purpose: string(domain.EmailTokenPurposeEmailVerification),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, fmt.Errorf("%s - select open code: %w", location, mapNoRows(err, outbound.ErrEmailTokenNotFound))
|
||||
}
|
||||
return emailTokenFromRow(emailTokenModelFromOpenCodeRow(row))
|
||||
}
|
||||
|
||||
// RecordFailedCodeAttempt counts a failed attempt in one statement. A token
|
||||
// that is no longer open matches no row, which is not an error: the attempt
|
||||
// then counts nothing, and the caller refuses the request either way.
|
||||
func (repository emailTokenRepository) RecordFailedCodeAttempt(ctx context.Context, id uuid.UUID, at time.Time) (bool, error) {
|
||||
const location = "emailTokenRepository.RecordFailedCodeAttempt"
|
||||
if err := requireTime(location, at, domain.ErrInvalidEmailToken); err != nil {
|
||||
return false, err
|
||||
}
|
||||
burned, err := repository.queries.RecordFailedEmailCodeAttempt(ctx, sqlc.RecordFailedEmailCodeAttemptParams{
|
||||
MaxAttempts: domain.VerificationCodeMaxFailedAttempts,
|
||||
AttemptedAt: pgTime(at),
|
||||
ID: pgUUID(id),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s - count failed attempt: %w", location, mapIdentityError(err))
|
||||
}
|
||||
return burned, nil
|
||||
}
|
||||
|
||||
func emailTokenFromRow(row sqlc.EmailToken) (domain.EmailToken, error) {
|
||||
purpose, err := domain.ParseEmailTokenPurpose(row.Purpose)
|
||||
if err != nil {
|
||||
@@ -553,8 +672,13 @@ func emailTokenFromRow(row sqlc.EmailToken) (domain.EmailToken, error) {
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, err
|
||||
}
|
||||
code, err := emailCodeFromColumns(row.CodeHash, row.CodeExpiresAt)
|
||||
if err != nil {
|
||||
return domain.EmailToken{}, err
|
||||
}
|
||||
return domain.EmailToken{
|
||||
Pending: pending,
|
||||
Code: code,
|
||||
ID: uuid.UUID(row.ID.Bytes),
|
||||
UserID: uuid.UUID(row.UserID.Bytes),
|
||||
Purpose: purpose,
|
||||
|
||||
Reference in new issue
Block a user