Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OpenAPIAuth ¶ added in v0.11.0
func OpenAPIAuth( authMiddleware func(http.Handler) http.Handler, scopesContextKey any, ) func(http.Handler) http.Handler
OpenAPIAuth wraps an authentication middleware to respect OpenAPI security definitions.
When using oapi-codegen generated servers, the generated code sets a scopes context key for endpoints that require authentication. Public endpoints (with `security: []` in the OpenAPI spec) do not have this key set.
This wrapper checks for the presence of the scopes context key:
- If nil: endpoint is public, skip authentication
- If present (even empty []string{}): endpoint requires authentication
Parameters:
- authMiddleware: the underlying authentication middleware (e.g., jwt.Middleware)
- scopesContextKey: the context key used by oapi-codegen (e.g., gen.BearerAuthScopes)
Example usage:
jwtMW := jwt.Middleware(jwtConfig)
authMW := auth.OpenAPIAuth(jwtMW, gen.BearerAuthScopes)
handler := gen.HandlerWithOptions(server, gen.StdHTTPServerOptions{
Middlewares: []gen.MiddlewareFunc{authMW},
})
func SetSubjectContext ¶
func SetSubjectContext(ctx context.Context, subjectCtx *SubjectContext) context.Context
SetSubjectContext stores the SubjectContext in the request context
Types ¶
type Middleware ¶
type Middleware interface {
// Handler returns an HTTP middleware handler that:
// 1. Authenticates the request (validates credentials)
// 2. Resolves the SubjectContext (user type and entitlements)
// 3. Stores the SubjectContext in the request context
Handler(next http.Handler) http.Handler
}
Middleware defines the interface that all authentication middlewares must implement
type SubjectContext ¶
type SubjectContext struct {
ID string // Unique identifier for the subject
Type string // Type of subject (user, service_account, etc.)
EntitlementClaim string // The claim name used for entitlements (e.g., "groups", "scopes")
EntitlementValues []string // The entitlement values extracted from the claim
}
SubjectContext contains the authenticated subject's type and entitlements
func GetSubjectContext ¶
func GetSubjectContext(r *http.Request) (*SubjectContext, bool)
GetSubjectContext retrieves the SubjectContext from the request context
func GetSubjectContextFromContext ¶
func GetSubjectContextFromContext(ctx context.Context) (*SubjectContext, bool)
GetSubjectContextFromContext retrieves the SubjectContext from a context.Context