feat(identity): confirm the email address with a six-digit code from the message

This commit is contained in:
shira committed 2026-10-10 00:52:46 +03:00
1 parent c94434cd09
commit d365a05c96
87 files changed
+4764 -224

No files matched your search

@@ -4,6 +4,7 @@ import (
"fmt"
"log/slog"
"strings"
"time"
"unicode"
"unicode/utf8"
@@ -36,6 +37,12 @@ type VerificationData struct {
// TokenLifetime is the policy value stated in the copy, from 1 hour to
// 7 days.
TokenLifetime domain.Hours
// Code is the six-digit code the message shows. VerificationCode redacts
// itself in fmt and slog output.
Code domain.VerificationCode
// CodeLifetime is the validity of the code stated in the copy: whole
// minutes, from 1 to 60.
CodeLifetime time.Duration
}
// PasswordResetData is the input of the password reset message.
@@ -77,6 +84,15 @@ type tokenLinkView struct {
LifetimeHours int
}
// verificationCodeView is what the verification templates receive: the link
// view plus the code and its validity in whole minutes.
type verificationCodeView struct {
tokenLinkView
Code string
CodeLifetimeMinutes int
}
// notificationView is what the notification templates receive. TextBody is
// the sanitized body for the plain-text part; Paragraphs holds the same lines
// for the HTML part, where html/template escapes each line.
@@ -97,6 +113,26 @@ func (renderer *Renderer) tokenLinkView(linkPath string, token domain.SecretToke
return tokenLinkView{Link: renderer.tokenLink(linkPath, token), LifetimeHours: int(lifetime)}, nil
}
// maxCodeLifetimeMinutes bounds the code copy; the code lifetime is a fixed
// product value of 30 minutes, so a longer value in the copy would be a bug.
const maxCodeLifetimeMinutes = 60
func (renderer *Renderer) verificationCodeView(data VerificationData) (verificationCodeView, error) {
const location = "Renderer.verificationCodeView"
link, err := renderer.tokenLinkView(verifyEmailPath, data.Token, data.TokenLifetime)
if err != nil {
return verificationCodeView{}, err
}
if data.Code.IsZero() {
return verificationCodeView{}, invalidData(location, "verification code must be set")
}
minutes := data.CodeLifetime / time.Minute
if data.CodeLifetime%time.Minute != 0 || minutes < 1 || minutes > maxCodeLifetimeMinutes {
return verificationCodeView{}, invalidData(location, fmt.Sprintf("code lifetime must be whole minutes from 1 to %d", maxCodeLifetimeMinutes))
}
return verificationCodeView{tokenLinkView: link, Code: data.Code.Reveal(), CodeLifetimeMinutes: int(minutes)}, nil
}
func (renderer *Renderer) notificationView(data NotificationData) (notificationView, error) {
if !utf8.ValidString(data.Subject) || !utf8.ValidString(data.Body) {
return notificationView{}, invalidData("Renderer.notificationView", "subject and body must be valid UTF-8")