as

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package as implements an OAuth 2.1 / OIDC Authorization Server.

Embed it in your application by passing a Config to New, then mounting the returned handler under /oauth and wiring the well-known endpoints at the host root.

Index

Constants

This section is empty.

Variables

View Source
var ErrIssuerRequired = errors.New("as: Issuer required")

ErrIssuerRequired indicates Config.Issuer was not set.

View Source
var ErrKeyManagerRequired = errors.New("as: KeyManager required")

ErrKeyManagerRequired indicates Config.KeyManager was not set.

View Source
var ErrStorageRequired = errors.New("as: Storage required")

ErrStorageRequired indicates Config.Storage was not set.

View Source
var ErrUpstreamRequired = errors.New("as: Upstream required")

ErrUpstreamRequired indicates Config.Upstream was not set.

View Source
var ErrUserResolverRequired = errors.New("as: UserResolver required")

ErrUserResolverRequired indicates Config.UserResolver was not set.

Functions

This section is empty.

Types

type AS

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

AS is the assembled authorization server.

func New

func New(cfg Config) (*AS, error)

New validates the config and returns an AS.

func (*AS) Config

func (a *AS) Config() *Config

Config returns the live config (read-only).

func (*AS) Handler

func (a *AS) Handler() http.Handler

Handler returns an http.Handler covering all AS endpoints under PathPrefix.

Routes (relative to PathPrefix):

GET  /authorize
POST /token
POST /register
GET  /idp/callback
POST /revoke
GET  /userinfo

The host-root well-known endpoints (oauth-authorization-server, openid-configuration, jwks.json) are exposed as separate handler methods: MetadataHandler, OIDCMetadataHandler, JWKSHandler.

func (*AS) JWKSHandler

func (a *AS) JWKSHandler(w http.ResponseWriter, r *http.Request)

JWKSHandler serves /.well-known/jwks.json — currently active public keys.

func (*AS) MetadataHandler

func (a *AS) MetadataHandler(w http.ResponseWriter, _ *http.Request)

MetadataHandler serves /.well-known/oauth-authorization-server (RFC 8414).

func (*AS) OIDCMetadataHandler

func (a *AS) OIDCMetadataHandler(w http.ResponseWriter, _ *http.Request)

OIDCMetadataHandler serves /.well-known/openid-configuration.

func (*AS) RunCleanup

func (a *AS) RunCleanup(ctx context.Context, every time.Duration) <-chan struct{}

RunCleanup starts the periodic cleanup loop in a background goroutine and returns a channel that closes when the goroutine exits after ctx is canceled. The loop purges expired auth codes, refresh tokens, and idle DCR clients, retires the in-memory authorize state map, and rotates the signing key when RotationInterval has elapsed.

Callers that want to wait for a clean exit on shutdown can select on the returned channel; callers that don't care can simply discard it.

API note: prior versions returned no value and were invoked as `go server.RunCleanup(ctx, dur)`. Existing call-sites still compile because the new return value is silently discarded.

type ASMetadata

type ASMetadata struct {
	// Issuer is the public base URL.
	Issuer string `json:"issuer"`
	// AuthorizationEndpoint is the absolute URL of /authorize.
	AuthorizationEndpoint string `json:"authorization_endpoint"`
	// TokenEndpoint is the absolute URL of /token.
	TokenEndpoint string `json:"token_endpoint"`
	// RegistrationEndpoint is the absolute URL of /register.
	RegistrationEndpoint string `json:"registration_endpoint"`
	// RevocationEndpoint is the absolute URL of /revoke.
	RevocationEndpoint string `json:"revocation_endpoint"`
	// UserinfoEndpoint is the absolute URL of /userinfo.
	UserinfoEndpoint string `json:"userinfo_endpoint,omitempty"`
	// JWKSURI is the absolute URL of /.well-known/jwks.json.
	JWKSURI string `json:"jwks_uri"`
	// ScopesSupported lists scopes the server understands.
	ScopesSupported []string `json:"scopes_supported"`
	// ResponseTypesSupported lists response_type values accepted at /authorize.
	ResponseTypesSupported []string `json:"response_types_supported"`
	// GrantTypesSupported lists grant_type values accepted at /token.
	GrantTypesSupported []string `json:"grant_types_supported"`
	// CodeChallengeMethodsSupported lists PKCE methods accepted at /authorize.
	CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
	// TokenEndpointAuthMethodsSupported lists client auth methods at /token.
	TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported"`
	// IDTokenSigningAlgValuesSupported lists ID token signing algorithms.
	IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported,omitempty"`
	// SubjectTypesSupported lists OIDC subject types.
	SubjectTypesSupported []string `json:"subject_types_supported,omitempty"`
	// AuthorizationResponseIssParameterSupported advertises that the
	// authorization response carries the RFC 9207 `iss` parameter, so
	// clients can detect AS mix-up / code injection.
	AuthorizationResponseIssParameterSupported bool `json:"authorization_response_iss_parameter_supported"`
}

ASMetadata is the RFC 8414 document.

type Config

type Config struct {
	// Issuer is the public base URL, e.g. https://hilo.eliminyro.me.
	// All emitted JWTs use this as `iss`. Must not have a trailing slash.
	Issuer string

	// PathPrefix is the mount path under Issuer where the AS lives,
	// e.g. "/oauth". Must start with "/" and have no trailing slash.
	PathPrefix string

	// Upstream is the upstream OIDC provider used to authenticate users.
	Upstream *idp.OIDCProvider

	// UserResolver maps upstream OIDC claims to an internal user ID.
	UserResolver idp.UserResolver

	// Storage provides the four sub-stores.
	Storage storage.Storage

	// KeyManager owns the active signing key.
	KeyManager *jwt.Manager

	// AccessTokenTTL defaults to 1h.
	AccessTokenTTL time.Duration

	// RefreshTokenTTL defaults to 30d.
	RefreshTokenTTL time.Duration

	// AuthCodeTTL defaults to 10m.
	AuthCodeTTL time.Duration

	// ClientTTL defaults to 90d sliding (DeleteExpired honors LastUsedAt).
	ClientTTL time.Duration

	// StateCookieTTL defaults to 10m.
	StateCookieTTL time.Duration

	// AdditionalClaims is called when minting an access token. It returns
	// app-specific claims to merge into the JWT (e.g. tenant_id for Memory).
	// May be nil.
	//
	// Prefer AdditionalClaimsCtx for new code so request-scoped timeouts
	// and tracing propagate into the hook's I/O. When both are set, the
	// ctx-aware variant wins.
	AdditionalClaims func(userID, clientID, resource string) map[string]any

	// AdditionalClaimsCtx is the request-context-aware variant of
	// AdditionalClaims. When set, it takes precedence over AdditionalClaims
	// and receives the /token request's context, so any DB or RPC call
	// inside the hook is bound by request-scoped deadlines and cancellation.
	AdditionalClaimsCtx func(ctx context.Context, userID, clientID, resource string) map[string]any

	// IDTokenClaims is called when the AS needs to mint an ID token (i.e.
	// when the original auth request scope contained "openid"). It returns
	// the standard OIDC ID token claims for the given user. If nil, the ID
	// token is minted with only sub + iss + aud + iat + exp.
	//
	// Prefer IDTokenClaimsCtx for new code so request-scoped timeouts and
	// tracing propagate into the hook's I/O. When both are set, the
	// ctx-aware variant wins.
	IDTokenClaims func(userID string) (email string, emailVerified bool, name, picture string)

	// IDTokenClaimsCtx is the request-context-aware variant of IDTokenClaims.
	// When set, it takes precedence over IDTokenClaims and receives the
	// /token request's context.
	IDTokenClaimsCtx func(ctx context.Context, userID string) (email string, emailVerified bool, name, picture string)

	// Logger receives diagnostic logs for every 500-class error path
	// (signer-unavailable, sign-failed, storage failures, etc.) and
	// security events (refresh-token reuse, claim-hook panics). When nil,
	// defaults() sets it to slog.Default().
	Logger *slog.Logger
}

Config configures an authorization server.

Jump to

Keyboard shortcuts

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