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
@@ -1015,6 +1015,201 @@ func (s *Server) handleConfirmEmailRequest(args [0]string, argsEscaped bool, w h
|
||||
}
|
||||
}
|
||||
|
||||
// handleConfirmEmailByCodeRequest handles ConfirmEmailByCode operation.
|
||||
//
|
||||
// Public operation without a session. The verification message carries a six-digit code next to the
|
||||
// link. The client sends the address, the code, and the password chosen for the registration that
|
||||
// issued the code, so the user types only the code after registration. A reloaded page has no password
|
||||
// in memory, so it asks for the address, the password, and the code together.
|
||||
//
|
||||
// The server finds the newest open verification token of the unverified account with this normalized
|
||||
// address, whose registration is current and which carries a code. It compares the keyed digest of the
|
||||
// code with the stored digest, checks the password against the token's pending password hash, and
|
||||
// checks that the code has not expired. The code lives 30 minutes, or until the token ends if the
|
||||
// token ends sooner. When all three checks pass, the server consumes the token, verifies the email,
|
||||
// and applies the pending credentials in one transaction, exactly as the link confirmation does, and
|
||||
// answers 200 with the same body. Success does not start a session.
|
||||
//
|
||||
// Each refusal answers 400 `verification_token_invalid` with the same body, whatever the cause: an
|
||||
// unknown address, an address without an open code, a wrong code, an expired code, a wrong password,
|
||||
// or a code that was burned. This keeps the answer from showing which addresses have a pending
|
||||
// registration. Every failed attempt of an open code counts against that token. The fifth failed
|
||||
// attempt burns the token: the code and the link stop working, and the user must request a new
|
||||
// message. A refused attempt changes nothing else.
|
||||
//
|
||||
// The same refusal is returned after the rate limits are checked. The operation is limited per client
|
||||
// address (`IDENTITY_CLIENT_HOURLY_LIMIT`) and per normalized email
|
||||
// (`IDENTITY_LOGIN_EMAIL_HOURLY_LIMIT`, shared with login); a refused request answers 429 with
|
||||
// Retry-After. A body that does not match the contract, such as a code that is not six digits, answers
|
||||
// 400 `validation_failed` before any lookup. Like every unsafe public identity operation, it requires
|
||||
// an Origin equal to the configured public application origin and answers 403 `csrf_check_failed`
|
||||
// otherwise.
|
||||
//
|
||||
// POST /api/v1/identity/email-verifications/code
|
||||
func (s *Server) handleConfirmEmailByCodeRequest(args [0]string, argsEscaped bool, w http.ResponseWriter, r *http.Request) {
|
||||
statusWriter := &codeRecorder{ResponseWriter: w}
|
||||
w = statusWriter
|
||||
otelAttrs := []attribute.KeyValue{
|
||||
otelogen.OperationID("ConfirmEmailByCode"),
|
||||
semconv.HTTPRequestMethodKey.String("POST"),
|
||||
semconv.HTTPRouteKey.String("/api/v1/identity/email-verifications/code"),
|
||||
}
|
||||
// Add attributes from config.
|
||||
otelAttrs = append(otelAttrs, s.cfg.Attributes...)
|
||||
|
||||
// Start a span for this request.
|
||||
ctx, span := s.cfg.Tracer.Start(r.Context(), ConfirmEmailByCodeOperation,
|
||||
trace.WithAttributes(otelAttrs...),
|
||||
serverSpanKind,
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
// Add Labeler to context.
|
||||
labeler := &Labeler{attrs: otelAttrs}
|
||||
ctx = contextWithLabeler(ctx, labeler)
|
||||
|
||||
// Run stopwatch.
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
elapsedDuration := time.Since(startTime)
|
||||
|
||||
attrSet := labeler.AttributeSet()
|
||||
attrs := attrSet.ToSlice()
|
||||
code := statusWriter.status
|
||||
if code != 0 {
|
||||
codeAttr := semconv.HTTPResponseStatusCode(code)
|
||||
attrs = append(attrs, codeAttr)
|
||||
span.SetAttributes(attrs...)
|
||||
}
|
||||
attrOpt := metric.WithAttributes(attrs...)
|
||||
|
||||
// Increment request counter.
|
||||
s.requests.Add(ctx, 1, attrOpt)
|
||||
|
||||
// Use floating point division here for higher precision (instead of Millisecond method).
|
||||
s.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), attrOpt)
|
||||
}()
|
||||
|
||||
var (
|
||||
recordError = func(stage string, err error) {
|
||||
span.RecordError(err)
|
||||
|
||||
// https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
|
||||
// Span Status MUST be left unset if HTTP status code was in the 1xx, 2xx or 3xx ranges,
|
||||
// unless there was another error (e.g., network error receiving the response body; or 3xx codes with
|
||||
// max redirects exceeded), in which case status MUST be set to Error.
|
||||
code := statusWriter.status
|
||||
if code < 100 || code >= 500 {
|
||||
span.SetStatus(codes.Error, stage)
|
||||
}
|
||||
|
||||
attrSet := labeler.AttributeSet()
|
||||
attrs := attrSet.ToSlice()
|
||||
if code != 0 {
|
||||
attrs = append(attrs, semconv.HTTPResponseStatusCode(code))
|
||||
}
|
||||
|
||||
s.errors.Add(ctx, 1, metric.WithAttributes(attrs...))
|
||||
}
|
||||
err error
|
||||
opErrContext = ogenerrors.OperationContext{
|
||||
Name: ConfirmEmailByCodeOperation,
|
||||
ID: "ConfirmEmailByCode",
|
||||
}
|
||||
)
|
||||
params, err := decodeConfirmEmailByCodeParams(args, argsEscaped, r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeParamsError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeParams", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
var rawBody []byte
|
||||
request, rawBody, close, err := s.decodeConfirmEmailByCodeRequest(r)
|
||||
if err != nil {
|
||||
err = &ogenerrors.DecodeRequestError{
|
||||
OperationContext: opErrContext,
|
||||
Err: err,
|
||||
}
|
||||
defer recordError("DecodeRequest", err)
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if err := close(); err != nil {
|
||||
recordError("CloseRequest", err)
|
||||
}
|
||||
}()
|
||||
|
||||
var response ConfirmEmailByCodeRes
|
||||
if m := s.cfg.Middleware; m != nil {
|
||||
mreq := middleware.Request{
|
||||
Context: ctx,
|
||||
OperationName: ConfirmEmailByCodeOperation,
|
||||
OperationSummary: "Verify an email with the six-digit code from the verification message.",
|
||||
OperationID: "ConfirmEmailByCode",
|
||||
Body: request,
|
||||
RawBody: rawBody,
|
||||
Params: middleware.Parameters{
|
||||
{
|
||||
Name: "Origin",
|
||||
In: "header",
|
||||
}: params.Origin,
|
||||
},
|
||||
Raw: r,
|
||||
}
|
||||
|
||||
type (
|
||||
Request = *EmailVerificationCodeRequest
|
||||
Params = ConfirmEmailByCodeParams
|
||||
Response = ConfirmEmailByCodeRes
|
||||
)
|
||||
response, err = middleware.HookMiddleware[
|
||||
Request,
|
||||
Params,
|
||||
Response,
|
||||
](
|
||||
m,
|
||||
mreq,
|
||||
unpackConfirmEmailByCodeParams,
|
||||
func(ctx context.Context, request Request, params Params) (response Response, err error) {
|
||||
response, err = s.h.ConfirmEmailByCode(ctx, request, params)
|
||||
return response, err
|
||||
},
|
||||
)
|
||||
} else {
|
||||
response, err = s.h.ConfirmEmailByCode(ctx, request, params)
|
||||
}
|
||||
if err != nil {
|
||||
if errRes, ok := errors.Into[*ProblemStatusCode](err); ok {
|
||||
if err := encodeErrorResponse(errRes, w, span); err != nil {
|
||||
defer recordError("Internal", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ht.ErrNotImplemented) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
return
|
||||
}
|
||||
if err := encodeErrorResponse(s.h.NewError(ctx, err), w, span); err != nil {
|
||||
defer recordError("Internal", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeConfirmEmailByCodeResponse(response, w, span); err != nil {
|
||||
defer recordError("EncodeResponse", err)
|
||||
if !errors.Is(err, ht.ErrInternalServerErrorResponse) {
|
||||
s.cfg.ErrorHandler(ctx, w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleCreateMyAssessmentAttemptRequest handles CreateMyAssessmentAttempt operation.
|
||||
//
|
||||
// Requires the session cookie of a candidate account, the CSRF token of that session in X-CSRF-Token,
|
||||
|
||||
Reference in new issue
Block a user