Documentation
¶
Overview ¶
Package keycloakauth validates Keycloak-issued JWTs and protects net/http handlers. It knows the Keycloak claim layout (realm_access, resource_access) and handles JWKS key rotation with rate-limited, deduplicated refetches.
It deliberately does NOT obtain or refresh tokens and is not an admin API client — it is the small piece you need to put a Keycloak realm in front of an HTTP API, nothing more.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingToken = errors.New("keycloakauth: missing bearer token") ErrMalformedToken = errors.New("keycloakauth: malformed token") ErrInvalidSignature = errors.New("keycloakauth: invalid signature") ErrTokenExpired = errors.New("keycloakauth: token expired") ErrTokenNotYetValid = errors.New("keycloakauth: token not valid yet") ErrWrongIssuer = errors.New("keycloakauth: wrong issuer") ErrWrongAudience = errors.New("keycloakauth: wrong audience") ErrUnknownKey = errors.New("keycloakauth: token signed with unknown key") )
Sentinel errors for matching with errors.Is. Verify wraps them with detail, the middleware maps them to HTTP responses.
Functions ¶
func NewContext ¶
NewContext returns ctx carrying the claims. The middleware calls this; exported for tests and custom middlewares.
func RequireClientRole ¶
RequireClientRole rejects with 403 unless the client role is present. Must run inside Middleware.
Types ¶
type Claims ¶
type Claims struct {
Subject string
Email string
PreferredUsername string
RealmRoles []string
Expiry time.Time
Raw map[string]any
}
Claims is the validated token content. Raw holds every claim as decoded JSON for anything not mapped to a field.
func FromContext ¶
FromContext returns the claims stored by the middleware.
func (*Claims) ClientRoles ¶
ClientRoles returns the roles granted for one client (resource_access).
func (*Claims) HasClientRole ¶
HasClientRole reports whether the client role is present.
func (*Claims) HasRealmRole ¶
HasRealmRole reports whether the realm role is present.
type Config ¶
type Config struct {
// BaseURL is the Keycloak root, e.g. "https://sso.example.com".
// Legacy (<17) installations include the /auth prefix.
BaseURL string
Realm string
}
Config identifies the Keycloak realm to trust.
type Option ¶
type Option func(*Verifier)
Option configures a Verifier.
func WithAudience ¶
WithAudience additionally requires the token's aud to contain the value. Off by default because Keycloak does not put the client in aud unless an audience mapper is configured on the client scope.
func WithErrorHandler ¶
WithErrorHandler replaces the middleware's 401 response writer.
func WithHTTPClient ¶
WithHTTPClient injects a custom *http.Client for JWKS fetches.
func WithIssuer ¶
WithIssuer overrides the expected iss claim, for setups where the public issuer URL differs from the URL the service uses to reach Keycloak.
func WithLeeway ¶
WithLeeway sets the clock-skew tolerance for exp/nbf/iat (default 30s).
func WithRefetchInterval ¶
WithRefetchInterval sets the minimum time between JWKS refetches triggered by unknown kids (default 1m). Zero disables the rate limit; concurrent refetches are still deduplicated.
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier validates tokens issued by one Keycloak realm. It is safe for concurrent use; construct once and share.
func New ¶
New builds a Verifier and eagerly loads the realm's JWKS, so a wrong BaseURL or realm fails here instead of on the first request.
func (*Verifier) Middleware ¶
Middleware validates the Authorization bearer token and stores the claims in the request context. Failures go through the error handler (default: 401 with a WWW-Authenticate header per RFC 6750).