Documentation
¶
Overview ¶
Package sdk is the ergonomic Go SDK for IAM resource servers.
Server applications use this package to verify tokens received from clients. The SDK calls IAM's token verification endpoint with the server's own service/admin credential and stores the verified principal in request contexts for downstream handlers.
Index ¶
- Variables
- func BearerToken(r *http.Request) (string, bool)
- func HTTPMiddleware(auth Authenticator, next http.Handler) http.Handler
- func HTTPMiddlewareWithOptions(auth Authenticator, opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
- func Refresh(ctx context.Context, auth Authenticator) error
- func RequireAAL(min int, opts ...GuardOption) func(http.Handler) http.Handler
- func RequireAnyScope(scopes []string, opts ...GuardOption) func(http.Handler) http.Handler
- func RequireScopes(scopes []string, opts ...GuardOption) func(http.Handler) http.Handler
- func Warm(ctx context.Context, auth Authenticator) error
- func WithPrincipal(ctx context.Context, principal *Principal) context.Context
- type Authenticator
- type AuthenticatorConfig
- type Claims
- type Client
- type Config
- type GuardOption
- type HTTPErrorHandler
- type HTTPMiddlewareOptions
- type HTTPTokenExtractor
- type HybridVerifier
- func (v *HybridVerifier) Authenticate(ctx context.Context, token string) (*Principal, error)
- func (v *HybridVerifier) Middleware(next http.Handler) http.Handler
- func (v *HybridVerifier) MiddlewareWithOptions(opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
- func (v *HybridVerifier) Refresh(ctx context.Context) error
- func (v *HybridVerifier) Verify(ctx context.Context, token string) (*VerifyResult, error)
- func (v *HybridVerifier) Warm(ctx context.Context) error
- type LocalConfig
- type LocalVerifier
- func (v *LocalVerifier) Authenticate(ctx context.Context, token string) (*Principal, error)
- func (v *LocalVerifier) Middleware(next http.Handler) http.Handler
- func (v *LocalVerifier) MiddlewareWithOptions(opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
- func (v *LocalVerifier) Refresh(ctx context.Context) error
- func (v *LocalVerifier) Verify(ctx context.Context, token string) (*VerifyResult, error)
- func (v *LocalVerifier) Warm(ctx context.Context) error
- type Principal
- type Refresher
- type ValidationMode
- type Verifier
- func (v *Verifier) Authenticate(ctx context.Context, token string) (*Principal, error)
- func (v *Verifier) Introspect(ctx context.Context, token string) (*VerifyResult, error)
- func (v *Verifier) Middleware(next http.Handler) http.Handler
- func (v *Verifier) MiddlewareWithOptions(opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
- func (v *Verifier) Refresh(context.Context) error
- func (v *Verifier) Verify(ctx context.Context, token string) (*VerifyResult, error)
- func (v *Verifier) Warm(context.Context) error
- type VerifierOption
- type VerifyResult
- type Warmer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingToken is returned when an incoming request has no bearer token. ErrMissingToken = errors.New("iam sdk: missing bearer token") // ErrInvalidToken is returned when IAM rejects the incoming bearer token. ErrInvalidToken = errors.New("iam sdk: invalid bearer token") )
var ErrForbidden = errors.New("iam sdk: forbidden")
ErrForbidden is returned when an authenticated principal lacks a required claim (scope or assurance level).
Functions ¶
func BearerToken ¶
BearerToken extracts an Authorization: Bearer token from an HTTP request.
func HTTPMiddleware ¶
func HTTPMiddleware(auth Authenticator, next http.Handler) http.Handler
HTTPMiddleware authenticates requests and stores Principal in request context.
func HTTPMiddlewareWithOptions ¶
func HTTPMiddlewareWithOptions(auth Authenticator, opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
HTTPMiddlewareWithOptions returns configurable HTTP authentication middleware.
func Refresh ¶
func Refresh(ctx context.Context, auth Authenticator) error
Refresh forces authenticator verification state refresh when supported.
func RequireAAL ¶ added in v1.1.0
RequireAAL admits a request only when the principal's assurance level is at least min (e.g. RequireAAL(2) demands a step-up/MFA session). Chain after an authentication middleware.
func RequireAnyScope ¶ added in v1.1.0
RequireAnyScope admits a request when the principal holds at least one of the listed scopes. Chain after an authentication middleware.
func RequireScopes ¶ added in v1.1.0
RequireScopes admits a request only when the principal holds every listed scope. Chain after an authentication middleware.
Types ¶
type Authenticator ¶
type Authenticator interface {
Authenticate(ctx context.Context, token string) (*Principal, error)
}
Authenticator verifies a bearer token and returns a Principal on success.
func NewAuthenticator ¶
func NewAuthenticator(config AuthenticatorConfig) (Authenticator, error)
NewAuthenticator builds a remote, local, or hybrid Authenticator from config.
type AuthenticatorConfig ¶
type AuthenticatorConfig struct {
Mode ValidationMode `mapstructure:"mode" default:"remote" validate:"oneof=remote local hybrid"`
BaseURL string `mapstructure:"base_url" default:"" validate:"omitempty,url"`
Credential string `mapstructure:"credential" default:""`
ProjectID string `mapstructure:"project_id" default:""`
Environment string `mapstructure:"environment" default:"live"`
Issuer string `mapstructure:"issuer" default:""`
Audience string `mapstructure:"audience" default:""`
JWKSURL string `mapstructure:"jwks_url" default:""`
JWKSCacheTTLSec int `mapstructure:"jwks_cache_ttl_sec" default:"300" validate:"min=1"`
TokenType string `mapstructure:"token_type" default:"access" validate:"omitempty,oneof=access id_token"`
HTTPClient *http.Client `mapstructure:"-" validate:"-"`
}
AuthenticatorConfig is the high-level SDK wiring config for resource servers. It is intentionally tagged like internal/config service structs, so callers can load it with the same structconf/mapstructure pipeline.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the generated IAM OpenAPI client with server-side conveniences.
type Config ¶
type Config struct {
// BaseURL is the IAM API base URL, for example https://iam.example.com.
BaseURL string
// Credential is the server's IAM credential used to call verification APIs.
// In production this is usually a service token or project-admin token.
Credential string
// HTTPClient optionally overrides the HTTP client used for IAM API calls.
HTTPClient *http.Client
}
Config configures a server-side IAM SDK client.
type GuardOption ¶ added in v1.1.0
type GuardOption func(*guardConfig)
GuardOption customizes a claim guard's failure handlers.
func WithForbiddenHandler ¶ added in v1.1.0
func WithForbiddenHandler(h HTTPErrorHandler) GuardOption
WithForbiddenHandler overrides the response when the Principal lacks a required claim (defaults to 403).
func WithUnauthenticatedHandler ¶ added in v1.1.0
func WithUnauthenticatedHandler(h HTTPErrorHandler) GuardOption
WithUnauthenticatedHandler overrides the response when no Principal is present in the request context (defaults to 401).
type HTTPErrorHandler ¶
type HTTPErrorHandler func(http.ResponseWriter, *http.Request, error)
HTTPErrorHandler handles middleware authentication failures.
type HTTPMiddlewareOptions ¶
type HTTPMiddlewareOptions struct {
TokenExtractor HTTPTokenExtractor
ErrorHandler HTTPErrorHandler
}
HTTPMiddlewareOptions customizes HTTP middleware behavior.
type HTTPTokenExtractor ¶
HTTPTokenExtractor extracts an IAM token from an HTTP request.
type HybridVerifier ¶
type HybridVerifier struct {
// contains filtered or unexported fields
}
HybridVerifier verifies locally first and falls back to remote verification when the local path cannot authenticate a token.
func NewHybridVerifier ¶
func NewHybridVerifier(local *LocalVerifier, remote *Verifier) *HybridVerifier
NewHybridVerifier wires local and remote verification paths together.
func (*HybridVerifier) Authenticate ¶
Authenticate verifies token and returns a Principal on success.
func (*HybridVerifier) Middleware ¶
func (v *HybridVerifier) Middleware(next http.Handler) http.Handler
Middleware authenticates HTTP requests with hybrid verification.
func (*HybridVerifier) MiddlewareWithOptions ¶
func (v *HybridVerifier) MiddlewareWithOptions(opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
MiddlewareWithOptions returns configurable HTTP authentication middleware.
func (*HybridVerifier) Refresh ¶
func (v *HybridVerifier) Refresh(ctx context.Context) error
Refresh forces local JWKS refresh.
func (*HybridVerifier) Verify ¶
func (v *HybridVerifier) Verify(ctx context.Context, token string) (*VerifyResult, error)
Verify tries local JWKS verification first, then falls back to IAM remote verification when local verification fails with a generic invalid_token.
type LocalConfig ¶
type LocalConfig struct {
// BaseURL is the IAM public base URL. It is used to build JWKSURL when
// JWKSURL is not provided.
BaseURL string
// JWKSURL optionally overrides the public JWKS endpoint.
JWKSURL string
// ProjectID scopes verification to a single IAM project.
ProjectID string
// Environment scopes verification to a single IAM environment.
Environment string
// Issuer optionally overrides the expected iss claim. When empty and
// ProjectID/Environment are set, it defaults to /p/{project_id}/e/{env}.
Issuer string
// Audience optionally validates aud/client_id.
Audience string
// TokenType validates typ. Empty defaults to access.
TokenType string
// CacheTTL controls how long fetched JWKS keys are reused.
CacheTTL time.Duration
// HTTPClient optionally overrides the HTTP client used for JWKS fetches.
HTTPClient *http.Client
}
LocalConfig configures local JWT verification using IAM's public JWKS.
type LocalVerifier ¶
type LocalVerifier struct {
// contains filtered or unexported fields
}
LocalVerifier verifies IAM JWTs locally using the project's JWKS.
func NewLocalVerifier ¶
func NewLocalVerifier(config LocalConfig) (*LocalVerifier, error)
NewLocalVerifier creates a local JWT verifier that does not call IAM token verification APIs.
func (*LocalVerifier) Authenticate ¶
Authenticate verifies token and returns a Principal on success.
func (*LocalVerifier) Middleware ¶
func (v *LocalVerifier) Middleware(next http.Handler) http.Handler
Middleware authenticates HTTP requests with local JWT verification.
func (*LocalVerifier) MiddlewareWithOptions ¶
func (v *LocalVerifier) MiddlewareWithOptions(opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
MiddlewareWithOptions returns configurable HTTP authentication middleware.
func (*LocalVerifier) Refresh ¶
func (v *LocalVerifier) Refresh(ctx context.Context) error
Refresh forces JWKS cache refresh.
func (*LocalVerifier) Verify ¶
func (v *LocalVerifier) Verify(ctx context.Context, token string) (*VerifyResult, error)
Verify verifies token locally against the cached JWKS.
type Principal ¶
type Principal struct {
Subject string
AccountID string
UserID string
ProjectID string
Environment string
SessionID string
ClientID string
Scopes []string
AAL int
AMR []string
Claims Claims
}
Principal is the resource-server view of an authenticated IAM subject.
func PrincipalFrom ¶
PrincipalFrom returns the principal stored by SDK HTTP middleware or gRPC interceptors.
func (*Principal) HasAMR ¶ added in v1.1.0
HasAMR reports whether the principal authenticated with the given method (e.g. "pwd", "otp", "webauthn").
func (*Principal) HasAllScopes ¶ added in v1.1.0
HasAllScopes reports whether the principal holds every one of scopes.
func (*Principal) HasAnyScope ¶ added in v1.1.0
HasAnyScope reports whether the principal holds at least one of scopes. With no scopes it returns true (no constraint).
type ValidationMode ¶
type ValidationMode string
ValidationMode selects how resource-server tokens are verified.
const ( // ValidationModeRemote calls IAM /v1/tokens/verify for every token. ValidationModeRemote ValidationMode = "remote" // ValidationModeLocal verifies JWTs in-process using IAM's public JWKS. ValidationModeLocal ValidationMode = "local" // ValidationModeHybrid verifies locally first and falls back to remote verify // when local verification cannot authenticate the token. ValidationModeHybrid ValidationMode = "hybrid" )
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier verifies end-user/client tokens received by a resource server.
func NewVerifier ¶
func NewVerifier(config Config, opts ...VerifierOption) (*Verifier, error)
NewVerifier creates a token verifier from Config.
func (*Verifier) Authenticate ¶
Authenticate verifies token and returns a Principal on success.
func (*Verifier) Introspect ¶
Introspect verifies token activity via IAM /v1/tokens/introspect.
func (*Verifier) Middleware ¶
Middleware authenticates requests and stores Principal in request context.
func (*Verifier) MiddlewareWithOptions ¶
func (v *Verifier) MiddlewareWithOptions(opts HTTPMiddlewareOptions) func(http.Handler) http.Handler
MiddlewareWithOptions returns configurable HTTP authentication middleware.
func (*Verifier) Refresh ¶
Refresh is a no-op for remote verification; IAM holds verification state.
type VerifierOption ¶
type VerifierOption func(*Verifier)
VerifierOption customizes a Verifier.
func WithAudience ¶
func WithAudience(audience string) VerifierOption
WithAudience asks IAM to validate the token audience.
type VerifyResult ¶
VerifyResult is IAM's token-verification response after decoding raw claims.