package v1 import ( "context" "errors" "lincodex/backend/internal/adapter/primary/http/api" ) // errSessionsNotImplemented is used by DenyAllSecurity, a test helper. Its text // stays in the server and maps to 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. Production bootstrap uses SessionSecurity instead; // tests use this helper when they need to verify a denied session. The // generated server evaluates schemes in type-name order. The contract test // pins the order and 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. // // SessionSecurity and Handler.NewError implement this 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 }