oidc

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package oidc authenticates requests with JWT access tokens of an external OpenID Connect provider (ADR 0027). Signature and standard claims are checked by go-oidc's verifier with our own key set; the extra claims, the tenant and the roles are checked here. The verifier never calls the provider per request.

Index

Constants

View Source
const (
	AlgRS256 = "RS256"
	AlgES256 = "ES256"
	AlgEdDSA = "EdDSA"
)

Signature algorithms the verifier can be configured with (ADR 0027).

Variables

View Source
var ErrNoKeys = errors.New("identity provider keys are not available yet")

ErrNoKeys means the key set has never been loaded: no cache file and no successful fetch. Requests are answered with 503 until keys are present.

Functions

This section is empty.

Types

type Authenticator

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

Authenticator implements auth.Authenticator for OIDC access tokens.

func NewAuthenticator

func NewAuthenticator(opts Options) (*Authenticator, error)

NewAuthenticator checks the options and returns the authenticator.

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(r *http.Request) (auth.Principal, error)

Authenticate implements auth.Authenticator. Errors wrap auth.ErrUnauthenticated (401), auth.ErrForbidden (403) or auth.ErrAuthUnavailable (503). Reasons for 401 are logged, not returned.

type Generic

type Generic struct {
	// TenantID is the single tenant (default).
	TenantID string
	// RolesClaim is a dot path into the claims, e.g. realm_access.roles.
	RolesClaim string
	// RoleMap maps provider values to roles. If empty, only values equal to
	// a role name count.
	RoleMap map[string]auth.Role
	// DefaultRole is given when the claim yields no valid role; empty means
	// none. It is never platform-admin (ADR 0028).
	DefaultRole auth.Role
}

Generic is the profile for any OIDC provider: one tenant, roles from a configurable claim.

func (Generic) Map

func (g Generic) Map(_ context.Context, claims map[string]json.RawMessage) (Identity, error)

Map implements Profile.

type Identity

type Identity struct {
	TenantID string
	Roles    []auth.Role
}

Identity is what a profile derives from verified claims.

type KeySet

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

KeySet holds the identity provider's signing keys. It implements the go-oidc KeySet interface: it verifies the signature only; header and claim checks are done by the verifier. Keys survive provider outages: a failed fetch keeps the last good set, and the set is persisted so a restart during an outage can still verify tokens (FA-AUTH-07).

func NewKeySet

func NewKeySet(opts KeySetOptions) (*KeySet, error)

NewKeySet returns an empty key set. Call Load before use.

func (*KeySet) Load

func (k *KeySet) Load(ctx context.Context) error

Load reads the cache file and then fetches the current keys. It fails only if neither produced keys; the caller may still start and retry later.

func (*KeySet) Ready

func (k *KeySet) Ready() bool

Ready reports whether keys are available.

func (*KeySet) Run

func (k *KeySet) Run(ctx context.Context)

Run refreshes the keys until ctx is cancelled.

func (*KeySet) UserInfoEndpoint

func (k *KeySet) UserInfoEndpoint() string

UserInfoEndpoint returns the userinfo endpoint from discovery, "" if unknown.

func (*KeySet) VerifySignature

func (k *KeySet) VerifySignature(ctx context.Context, raw string) ([]byte, error)

VerifySignature implements the go-oidc KeySet interface.

type KeySetOptions

type KeySetOptions struct {
	// Issuer is the configured issuer, compared exactly with the discovery
	// document and the cache file.
	Issuer string
	// Algorithms is the allow-list of signature algorithms.
	Algorithms []string
	// CacheFile is where fetched keys are persisted; empty disables the file.
	CacheFile string
	// AllowHTTP permits http URLs for discovery and JWKS (development only).
	AllowHTTP bool
	// Client is the HTTP client; nil uses a client with timeouts. Redirects
	// are never followed, whatever the client says.
	Client *http.Client
	// RefreshInterval is the regular refresh period (default 5 min); a
	// shorter Cache-Control max-age of the JWKS response wins.
	RefreshInterval time.Duration
	// MinRefetch is the minimum time between two fetches triggered by an
	// unknown key ID (default 30 s).
	MinRefetch time.Duration
	// OnFetchFailure is called after every failed fetch (metrics).
	OnFetchFailure func()
	Now            func() time.Time
	Log            *slog.Logger
}

KeySetOptions configures a KeySet.

type Options

type Options struct {
	Issuer      string
	Audience    string
	ClientIDs   []string
	RequireAZP  bool
	Algorithms  []string
	MaxTokenTTL time.Duration
	Profile     Profile
	Tenants     TenantStore
	Keys        *KeySet
	// UserInfo, if set, supplies the claims the profile maps instead of the
	// access token (OIDC_ROLES_SOURCE=userinfo, ADR 0028).
	UserInfo *UserInfo
	Now      func() time.Time
	Log      *slog.Logger
}

Options configures the Authenticator.

type Profile

type Profile interface {
	Map(ctx context.Context, claims map[string]json.RawMessage) (Identity, error)
}

Profile maps verified claims to tenant and roles (ADR 0027). Errors wrap auth.ErrForbidden or auth.ErrAuthUnavailable.

type Provisioner

type Provisioner interface {
	Provision(ctx context.Context, orgID, orgName string) (string, error)
}

Provisioner creates the tenant of an organization (ADR 0029). It returns the tenant ID; if the organization was provisioned concurrently, the ID of that tenant. Errors wrapping auth.ErrForbidden are shown to the caller.

type TenantByOrg

type TenantByOrg interface {
	GetTenantByOrg(ctx context.Context, orgID string) (store.Tenant, error)
}

TenantByOrg finds the tenant bound to an organization.

type TenantStore

type TenantStore interface {
	GetTenant(ctx context.Context, id string) (store.Tenant, error)
	GetTenantByOrg(ctx context.Context, orgID string) (store.Tenant, error)
}

TenantStore is the read access the verifier needs.

type UserInfo

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

UserInfo fetches the claims of an access token from the provider's userinfo endpoint and caches them per token until it expires (ADR 0028). It is only called for tokens that passed every other check.

func NewUserInfo

func NewUserInfo(opts UserInfoOptions) *UserInfo

NewUserInfo returns a userinfo client.

func (*UserInfo) Claims

func (u *UserInfo) Claims(ctx context.Context, token, sub string, expires time.Time) (map[string]json.RawMessage, error)

Claims returns the userinfo claims for token, whose verified subject is sub and which expires at expires. Errors wrap auth.ErrAuthUnavailable when the provider cannot be asked; other errors mean the token must be rejected.

type UserInfoOptions

type UserInfoOptions struct {
	// Keys supplies the userinfo endpoint from discovery and the HTTP client
	// with its restrictions (no redirects, size limit).
	Keys *KeySet
	// OnCall and OnFailure count lookups and failed lookups (metrics).
	OnCall    func()
	OnFailure func()
	Now       func() time.Time
}

UserInfoOptions configures UserInfo.

type Zitadel

type Zitadel struct {
	ProjectID     string
	PlatformOrgID string
	// Multi selects OIDC_TENANCY=multi; otherwise only the platform
	// organization is admitted and maps to DefaultTenantID.
	Multi           bool
	DefaultTenantID string
	Tenants         TenantByOrg
	// Provisioner creates the tenant of an unknown organization on its first
	// login (FA-TEN-03, ADR 0029). Nil disables automatic provisioning; it is
	// only used with Multi.
	Provisioner Provisioner
}

Zitadel is the profile for Zitadel: the tenant comes from the user's organization, roles from the project roles claim and only where granted in that organization (FA-AUTH-06, PF-31).

func (Zitadel) Map

func (z Zitadel) Map(ctx context.Context, claims map[string]json.RawMessage) (Identity, error)

Map implements Profile.

Jump to

Keyboard shortcuts

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