Documentation
¶
Overview ¶
Package adapters-chi-security demonstrates authentication and authorization for REST APIs built with go-codex and the chi router adapter.
Architecture mirrors adapters-nethttp-security exactly — the only difference is that the chi adapter is used instead of net/http, and chi path variable extraction works alongside security enforcement.
Security is declared in two layers:
Spec layer (api/rest + route): security schemes and per-route requirements are declared on the Builder and RouteHandle. The OpenAPI spec output includes components/securitySchemes and per-operation security.
Runtime layer (adapters/chi): the adapter extracts credentials from the request (Authorization header for Bearer), runs the optional Codec format-validation (e.g. validate.BearerToken), then calls SecurityFunc.
Routes:
- POST /login — public (no security), returns a mock token
- GET /users/{id}/profile — secured with bearerAuth + "profile" scope; id via chi path var
- POST /admin/action — secured with bearerAuth + "admin" scope
SecurityFunc performs a mock check (no real crypto):
- Accepts tokens in the mock tokenScopes store
- Checks all required scopes from the SecurityRequirement
Run with: go run ./examples/adapters-chi-security