auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package auth verifies bearer tokens and resolves them to a Principal that carries roles and permissions for authorization checks. It supports JWKS (OIDC), a static RSA public key, and an ephemeral development key.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewVerifier

func NewVerifier(ctx context.Context, s Settings, development bool) (Verifier, *DevIssuer, error)

NewVerifier selects a verifier from settings. Precedence: JWKS (OIDC), then a static RSA public key, then an ephemeral development key. In any environment other than development, one of the first two must be configured.

Types

type Authenticator

type Authenticator struct {
	// contains filtered or unexported fields
}

Authenticator verifies bearer tokens during OpenAPI request validation and publishes the resulting principal to the request context.

func NewAuthenticator

func NewAuthenticator(v Verifier) *Authenticator

func (*Authenticator) Authenticate

Authenticate satisfies openapi3filter.AuthenticationFunc. The validator calls it for operations that declare the bearerAuth scheme.

func (*Authenticator) SeedContext

func (a *Authenticator) SeedContext(next http.Handler) http.Handler

SeedContext prepares the request context to receive the authenticated principal. It must run before the OpenAPI request-validation middleware, which does not propagate a context modified inside Authenticate.

type Claims

type Claims struct {
	jwt.RegisteredClaims
	Roles []string `json:"roles,omitempty"`
	Scope string   `json:"scope,omitempty"`
}

Claims is the JWT payload. roles drives RBAC; scope is an optional OAuth2 space-delimited scope string that is merged into the principal's scopes.

type DevIssuer

type DevIssuer struct {
	// contains filtered or unexported fields
}

DevIssuer mints tokens with the ephemeral key generated when no verifier is configured in development. It must never be enabled in production.

func (*DevIssuer) Mint

func (d *DevIssuer) Mint(subject string, roles []string, ttl time.Duration) (string, error)

type Principal

type Principal struct {
	Subject string
	Roles   []string
	Scopes  []string
}

Principal is the authenticated caller derived from a verified token.

func PrincipalFrom

func PrincipalFrom(ctx context.Context) (Principal, bool)

PrincipalFrom returns the authenticated principal, or false if the request was not authenticated.

func (Principal) HasPermission

func (p Principal) HasPermission(permission string) bool

HasPermission is true when any of the principal's roles grants the permission or the permission is present as an explicit token scope.

func (Principal) HasRole

func (p Principal) HasRole(role string) bool

type Settings

type Settings struct {
	JWKSURL       string
	PublicKeyPath string
	Issuer        string
	Audience      string
}

Settings configures verifier selection without depending on the config package.

type Verifier

type Verifier interface {
	Verify(ctx context.Context, token string) (*Claims, error)
}

Verifier validates a bearer token and returns its claims.

Jump to

Keyboard shortcuts

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