Files
lincodex/backend/internal/adapter/primary/http/v1/identity.go
T

120 lines
3.9 KiB
Go

package v1
import (
"context"
"errors"
"math"
"time"
"lincodex/backend/internal/adapter/primary/http/api"
httpmiddleware "lincodex/backend/internal/adapter/primary/http/middleware"
"lincodex/backend/internal/adapter/primary/http/problem"
"lincodex/backend/internal/usecase/identity"
)
// AccountRegistration is what IdentityHandler needs from the registration
// usecase.
type AccountRegistration interface {
Register(ctx context.Context, command identity.RegisterCommand) error
ResendVerification(ctx context.Context, command identity.ResendCommand) error
}
// IdentityHandler serves the anonymous registration operations. Both answer
// the same neutral 202 whatever the state of the address; see the contract.
type IdentityHandler struct {
registration AccountRegistration
}
func NewIdentityHandler(registration AccountRegistration) *IdentityHandler {
return &IdentityHandler{registration: registration}
}
func (handler *IdentityHandler) RegisterAccount(
ctx context.Context,
request *api.RegistrationRequest,
params api.RegisterAccountParams,
) (api.RegisterAccountRes, error) {
err := handler.registration.Register(ctx, identity.RegisterCommand{
IdempotencyKey: params.IdempotencyKey,
Email: string(request.Email),
Password: string(request.Password),
Scenario: string(request.Scenario),
ConsentTextVersion: string(request.ConsentC1.TextVersion),
ClientKey: clientKey(ctx),
})
switch {
case err == nil:
return accepted(), nil
case errors.Is(err, identity.ErrInvalidField):
return (*api.RegisterAccountBadRequest)(&invalidFieldProblem(ctx, err).Response), nil
case errors.Is(err, identity.ErrIdempotencyKeyReused):
return (*api.RegisterAccountUnprocessableEntity)(&problem.New(ctx, api.ProblemCodeIdempotencyKeyReused).Response), nil
}
if limited, ok := rateLimited(ctx, err); ok {
return limited, nil
}
return nil, err
}
func (handler *IdentityHandler) ResendEmailVerification(
ctx context.Context,
request *api.EmailVerificationResendRequest,
) (api.ResendEmailVerificationRes, error) {
err := handler.registration.ResendVerification(ctx, identity.ResendCommand{
Email: string(request.Email),
ClientKey: clientKey(ctx),
})
switch {
case err == nil:
return accepted(), nil
case errors.Is(err, identity.ErrInvalidField):
return (*api.ResendEmailVerificationBadRequest)(&invalidFieldProblem(ctx, err).Response), nil
}
if limited, ok := rateLimited(ctx, err); ok {
return limited, nil
}
return nil, err
}
func accepted() *api.IdentityRequestAccepted {
return &api.IdentityRequestAccepted{Status: api.IdentityRequestAcceptedStatusAccepted}
}
// clientKey is the limiter key of the client network address that the
// ClientIP middleware resolved. Without it, for example when the middleware
// is not mounted, the per-client limit is skipped and only the per-address
// limits apply: a shared bucket would let one client lock everyone out.
func clientKey(ctx context.Context) string {
address, ok := httpmiddleware.ClientAddr(ctx)
if !ok {
return ""
}
return httpmiddleware.LimiterKey(address)
}
func invalidFieldProblem(ctx context.Context, err error) *api.ProblemStatusCode {
field, ok := errors.AsType[*identity.InvalidFieldError](err)
if !ok {
return problem.InvalidField(ctx, api.ProblemFieldLocationBody, "")
}
location := api.ProblemFieldLocationBody
if field.Pointer == "/Idempotency-Key" {
location = api.ProblemFieldLocationHeader
}
return problem.InvalidField(ctx, location, field.Pointer)
}
func rateLimited(ctx context.Context, err error) (*api.TooManyRequestsHeaders, bool) {
limited, ok := errors.AsType[*identity.RateLimitedError](err)
if !ok {
return nil, false
}
return problem.RateLimited(ctx, wholeSeconds(limited.RetryAfter)), true
}
// wholeSeconds rounds a wait up, so a client that waits the stated time is
// not refused again for the remainder.
func wholeSeconds(wait time.Duration) int32 {
return int32(min(math.Ceil(wait.Seconds()), math.MaxInt32))
}