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
@@ -68,6 +68,9 @@ type EmailToken struct {
|
||||
ExpiresAt time.Time
|
||||
ConsumedAt *time.Time
|
||||
InvalidatedAt *time.Time
|
||||
// Code is set on a verification token that carries a six-digit code and
|
||||
// nil otherwise. Its digest is never the code itself.
|
||||
Code *EmailCode
|
||||
// Pending is set on the verification token of a registration and nil on
|
||||
// a token that carries no credentials.
|
||||
Pending *PendingRegistration
|
||||
@@ -82,6 +85,11 @@ type NewEmailTokenInput struct {
|
||||
RegistrationVersion int64
|
||||
IssuedAt time.Time
|
||||
Lifetime time.Duration
|
||||
// CodeHash and CodeLifetime describe the optional verification code. Both
|
||||
// are set together or both are zero. The code expires at the earlier of
|
||||
// the token expiry and IssuedAt plus CodeLifetime.
|
||||
CodeHash VerificationCodeHash
|
||||
CodeLifetime time.Duration
|
||||
// Pending is optional; see PendingRegistration.
|
||||
Pending *PendingRegistration
|
||||
}
|
||||
@@ -99,24 +107,45 @@ func NewEmailToken(input NewEmailTokenInput) (EmailToken, error) {
|
||||
return EmailToken{}, invalidEmailToken("issue time must be set")
|
||||
case input.Lifetime <= 0:
|
||||
return EmailToken{}, invalidEmailToken("lifetime must be positive")
|
||||
case input.CodeHash.IsZero() != (input.CodeLifetime == 0):
|
||||
return EmailToken{}, invalidEmailToken("code hash and code lifetime must be set together")
|
||||
case input.CodeLifetime < 0:
|
||||
return EmailToken{}, invalidEmailToken("code lifetime must not be negative")
|
||||
}
|
||||
if _, err := ParseEmailTokenPurpose(string(input.Purpose)); err != nil {
|
||||
return EmailToken{}, fmt.Errorf("domain.NewEmailToken - validate purpose: %w", err)
|
||||
}
|
||||
if !input.CodeHash.IsZero() && input.Purpose != EmailTokenPurposeEmailVerification {
|
||||
return EmailToken{}, invalidEmailToken("only an email verification token carries a code")
|
||||
}
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return EmailToken{}, fmt.Errorf("domain.NewEmailToken - generate token ID: %w", err)
|
||||
}
|
||||
return EmailToken{
|
||||
expiresAt := input.IssuedAt.Add(input.Lifetime)
|
||||
token := EmailToken{
|
||||
ID: id,
|
||||
UserID: input.UserID,
|
||||
Purpose: input.Purpose,
|
||||
TokenHash: input.TokenHash,
|
||||
RegistrationVersion: input.RegistrationVersion,
|
||||
CreatedAt: input.IssuedAt,
|
||||
ExpiresAt: input.IssuedAt.Add(input.Lifetime),
|
||||
ExpiresAt: expiresAt,
|
||||
Pending: input.Pending,
|
||||
}, nil
|
||||
}
|
||||
if !input.CodeHash.IsZero() {
|
||||
token.Code = &EmailCode{Hash: input.CodeHash, ExpiresAt: earlierInstant(expiresAt, input.IssuedAt.Add(input.CodeLifetime))}
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// earlierInstant returns the earlier of two instants. The time type is not
|
||||
// ordered by the built-in min.
|
||||
func earlierInstant(left, right time.Time) time.Time {
|
||||
if right.Before(left) {
|
||||
return right
|
||||
}
|
||||
return left
|
||||
}
|
||||
|
||||
func invalidEmailToken(reason string) error {
|
||||
|
||||
Reference in new issue
Block a user