feat(identity): add server sessions and audited mail recovery
This commit is contained in:
1 parent
20c03288d6
commit
4ee53d9a74
58 files changed
+5359
-567
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/ogen-go/ogen/ogenerrors"
|
||||
|
||||
"lincodex/backend/internal/adapter/primary/http/api"
|
||||
"lincodex/backend/internal/adapter/primary/http/problem"
|
||||
"lincodex/backend/internal/usecase/identity"
|
||||
)
|
||||
|
||||
// Handler is the composite implementation of the generated api.Handler.
|
||||
// Unimplemented operations stay on ogen's embedded fallback until a business
|
||||
// handler is added for them.
|
||||
type Handler struct {
|
||||
api.UnimplementedHandler
|
||||
|
||||
health *HealthHandler
|
||||
identity *IdentityHandler
|
||||
problems *problem.Responder
|
||||
}
|
||||
|
||||
var _ api.Handler = (*Handler)(nil)
|
||||
|
||||
// HandlerOption adds a business-area handler to Handler.
|
||||
type HandlerOption func(*Handler)
|
||||
|
||||
// WithIdentity serves the configured identity operations.
|
||||
func WithIdentity(identity *IdentityHandler) HandlerOption {
|
||||
return func(handler *Handler) { handler.identity = identity }
|
||||
}
|
||||
|
||||
func NewHandler(health *HealthHandler, problems *problem.Responder, options ...HandlerOption) *Handler {
|
||||
handler := &Handler{health: health, problems: problems}
|
||||
for _, option := range options {
|
||||
option(handler)
|
||||
}
|
||||
return handler
|
||||
}
|
||||
|
||||
// NewError maps security failures to fixed public problems. Other errors are
|
||||
// passed to the responder, which logs them and does not expose their text.
|
||||
func (handler *Handler) NewError(ctx context.Context, err error) *api.ProblemStatusCode {
|
||||
var securityError *ogenerrors.SecurityError
|
||||
if errors.As(err, &securityError) {
|
||||
switch {
|
||||
case errors.Is(securityError.Err, errCsrfCheckFailed):
|
||||
return problem.New(ctx, api.ProblemCodeCsrfCheckFailed)
|
||||
case errors.Is(securityError.Err, ogenerrors.ErrSecurityRequirementIsNotSatisfied):
|
||||
if _, ok := PrincipalFromContext(ctx); ok {
|
||||
return problem.New(ctx, api.ProblemCodeCsrfCheckFailed)
|
||||
}
|
||||
return problem.New(ctx, api.ProblemCodeUnauthenticated)
|
||||
case errors.Is(securityError.Err, ogenerrors.ErrSkipServerSecurity),
|
||||
errors.Is(securityError.Err, identity.ErrUnauthenticated),
|
||||
errors.Is(securityError.Err, errSessionsNotImplemented):
|
||||
return problem.New(ctx, api.ProblemCodeUnauthenticated)
|
||||
default:
|
||||
return handler.problems.HandlerError(ctx, securityError.Err)
|
||||
}
|
||||
}
|
||||
return handler.problems.HandlerError(ctx, err)
|
||||
}
|
||||
Reference in new issue
Block a user