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 consent *ConsentHandler profile *ProfileHandler organization *OrganizationHandler 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 } } // WithConsent serves the published texts and ledger operations. func WithConsent(consent *ConsentHandler) HandlerOption { return func(handler *Handler) { handler.consent = consent } } // WithProfile serves the candidate's private profile. func WithProfile(profile *ProfileHandler) HandlerOption { return func(handler *Handler) { handler.profile = profile } } // WithOrganization serves company profiles and owner operations. func WithOrganization(organization *OrganizationHandler) HandlerOption { return func(handler *Handler) { handler.organization = organization } } 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) }