adapters-nethttp-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: 17 Imported by: 0

Documentation

Overview

Package adapters-nethttp-security demonstrates authentication and authorization for REST APIs built with go-codex and the net/http adapter.

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/nethttp): the adapter extracts credentials from the request (Authorization header for Bearer), runs the optional Codec format-validation (e.g. validate.JWT), then calls SecurityFunc for application-level verification (signature check, scope check, etc.).

Routes:

  • POST /login — public (no security), returns a mock token
  • GET /profile — secured with bearerAuth (JWT format + user scope)
  • POST /admin/action — secured with bearerAuth + admin scope

SecurityFunc in this example performs a mock check (no real crypto):

  • Accepts any token that starts with "valid-"
  • Checks that the required scopes are present in a mock token store

A SecurityObserver is wired to log every credential rejection with the route path and scheme name.

Run with: go run ./examples/adapters-nethttp-security

Jump to

Keyboard shortcuts

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