adapters-chi-security

command
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 18 Imported by: 0

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:

  1. 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.

  2. 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL