package v1 import ( "context" "errors" "lincodex/backend/internal/adapter/primary/http/api" ) // errSessionsNotImplemented rejects every session-protected request until the // session boundary exists. Its text stays in the server: the generated server // wraps it in *ogenerrors.SecurityError, which the problem responder answers // with a fixed 401 unauthenticated problem. var errSessionsNotImplemented = errors.New("v1.DenyAllSecurity - sessions are not implemented") // DenyAllSecurity implements api.SecurityHandler by rejecting every session // cookie and CSRF token, so no operation that requires a session is reachable // before server sessions exist. Operations that declare `security: []` do not // call it and keep their own behavior, which is 501 not_implemented until // their business logic exists. // // Session authentication will replace it. The generated server (ogen v1.24.0) evaluates the // schemes of an operation as follows; TestGeneratedServerSecurityOrder pins // the order and the context propagation: // // - It evaluates sessionCookie before sessionCsrf, in the order of the // generated type names, and each step receives the context returned by // the previous successful step. // - A scheme whose value is absent (no cookie, or an empty X-CSRF-Token // header) does not call its Handle method and counts as unsatisfied. // - A Handle method that returns ogenerrors.ErrSkipServerSecurity also counts // as unsatisfied, and evaluation continues with the next scheme. // - A Handle method that returns any other error stops evaluation: the // server passes &ogenerrors.SecurityError{Security: "SessionCookie" or // "SessionCsrf", Err: err} to Handler.NewError with the context that the // failing step received. HandleSessionCsrf is therefore called after a // missing cookie or a skipped session, with the original context, but // never after HandleSessionCookie returned another error. // - When every scheme was evaluated and a requirement is still unsatisfied, // the server passes &ogenerrors.SecurityError{Err: // ogenerrors.ErrSecurityRequirementIsNotSatisfied}, without a Security // field, to Handler.NewError with the context of the last successful step. // // The intended mapping on top of that behavior: // // - HandleSessionCookie returns a context with the principal for a valid // session and ogenerrors.ErrSkipServerSecurity for a missing, unknown, // expired, or revoked one. // - HandleSessionCsrf returns ogenerrors.ErrSkipServerSecurity when the // context has no principal, so the result is 401 unauthenticated, and a // typed error when the token does not belong to the session. // - NewError answers a SecurityError with Security "SessionCsrf" with 403 // csrf_check_failed, an unsatisfied requirement whose context carries a // principal (an empty X-CSRF-Token) with 403 csrf_check_failed, and every // other security failure with 401 unauthenticated. type DenyAllSecurity struct{} var _ api.SecurityHandler = (*DenyAllSecurity)(nil) func NewDenyAllSecurity() *DenyAllSecurity { return &DenyAllSecurity{} } func (*DenyAllSecurity) HandleSessionCookie(ctx context.Context, _ api.OperationName, _ api.SessionCookie) (context.Context, error) { return ctx, errSessionsNotImplemented } func (*DenyAllSecurity) HandleSessionCsrf(ctx context.Context, _ api.OperationName, _ api.SessionCsrf) (context.Context, error) { return ctx, errSessionsNotImplemented }