Documentation
¶
Index ¶
- Constants
- func AuthorizationServerMetadataHandler(cfg AuthorizationServerMetadataConfig) http.Handler
- func Bearer(cfg BearerConfig) func(http.Handler) http.Handler
- func ClassifyLoginError(err error) string
- func ClassifyResetError(err error) string
- func ClassifySignupError(err error) string
- func ClassifyVerifyError(err error) string
- func GetAuthSource(ctx context.Context) string
- func GetScopeTarget(ctx context.Context) string
- func GetScopeType(ctx context.Context) string
- func GetScopes(ctx context.Context) fosite.Arguments
- func GetUserID(ctx context.Context) string
- func HashPassword(password string) (string, error)
- func HashSecret(secret string) (string, error)
- func IsAuthDisabled(ctx context.Context) bool
- func NeedsRehash(hash string) bool
- func NewEmptySession() *openid.DefaultSession
- func NewRegistrationHandler(cfg RegistrationConfig) http.Handler
- func NewSession(subject Subject) *openid.DefaultSession
- func PKCEChallenge(verifier string) string
- func ProtectedResourceMetadataHandler(cfg ProtectedResourceMetadataConfig) http.Handler
- func RandomState() (string, error)
- func RequireScope(scope string) func(http.Handler) http.Handler
- func RequireScopeForTarget(scope string, resolve TargetResolver) func(http.Handler) http.Handler
- func VerifyPassword(hash string, password string) error
- func VerifySecret(hash string, secret string) error
- func WithAuthDisabled(ctx context.Context) context.Context
- func WithScopes(ctx context.Context, scopes fosite.Arguments) context.Context
- type AuthorizationServerMetadataConfig
- type BearerConfig
- type ClientRegistrar
- type Config
- type PATAuthResult
- type PATEntity
- type PATValidator
- type PKCEPair
- type ProtectedResourceMetadataConfig
- type Provider
- func (p *Provider) AuthorizeHandler(resolve SubjectResolver) http.Handler
- func (p *Provider) OAuth2Provider() fosite.OAuth2Provider
- func (p *Provider) RegisterHandler() http.Handler
- func (p *Provider) RegisterRoutes(mux *http.ServeMux, prefix string, resolve SubjectResolver)
- func (p *Provider) RevokeHandler() http.Handler
- func (p *Provider) TokenHandler() http.Handler
- type RegistrationConfig
- type RegistrationHandler
- type Servicer
- type Subject
- type SubjectResolver
- type Target
- type TargetResolver
- type TokenIntrospector
- type TokenValidator
Constants ¶
const ( // AuthSourceOAuth2 indicates a Fosite-issued bearer token authenticated the request. AuthSourceOAuth2 = "oauth2" // AuthSourcePAT indicates a personal access token authenticated the request. AuthSourcePAT = "pat" )
const DefaultBcryptCost = 12
DefaultBcryptCost is the bcrypt work factor used for new hashes.
Variables ¶
This section is empty.
Functions ¶
func AuthorizationServerMetadataHandler ¶ added in v0.4.0
func AuthorizationServerMetadataHandler(cfg AuthorizationServerMetadataConfig) http.Handler
AuthorizationServerMetadataHandler returns OAuth authorization server metadata.
func Bearer ¶
func Bearer(cfg BearerConfig) func(http.Handler) http.Handler
Bearer validates OAuth bearer tokens and optional PATs.
func ClassifyLoginError ¶
ClassifyLoginError reduces authentication failures to a fixed vocabulary.
func ClassifyResetError ¶
ClassifyResetError reduces password-reset failures to a fixed vocabulary.
func ClassifySignupError ¶
ClassifySignupError reduces registration failures to a fixed vocabulary.
func ClassifyVerifyError ¶
ClassifyVerifyError reduces email-verification failures to a fixed vocabulary.
func GetAuthSource ¶
GetAuthSource returns the auth mechanism that authenticated the request.
func GetScopeTarget ¶
GetScopeTarget returns the PAT scope target, when present.
func GetScopeType ¶
GetScopeType returns the PAT scope type, when present.
func HashPassword ¶
HashPassword returns a bcrypt hash of password.
func HashSecret ¶
HashSecret returns a bcrypt hash of secret.
func IsAuthDisabled ¶
IsAuthDisabled reports whether auth is disabled for this request.
func NeedsRehash ¶
NeedsRehash reports whether hash was produced below DefaultBcryptCost.
func NewEmptySession ¶
func NewEmptySession() *openid.DefaultSession
NewEmptySession creates an empty session for token exchange deserialization.
func NewRegistrationHandler ¶ added in v0.4.0
func NewRegistrationHandler(cfg RegistrationConfig) http.Handler
NewRegistrationHandler creates an RFC 7591 dynamic client registration handler.
func NewSession ¶
func NewSession(subject Subject) *openid.DefaultSession
NewSession creates an OIDC session for subject.
func PKCEChallenge ¶
PKCEChallenge returns the RFC 7636 S256 challenge for verifier.
func ProtectedResourceMetadataHandler ¶ added in v0.4.0
func ProtectedResourceMetadataHandler(cfg ProtectedResourceMetadataConfig) http.Handler
ProtectedResourceMetadataHandler returns an RFC 9728 metadata handler.
func RandomState ¶
RandomState returns a URL-safe random OAuth state parameter.
func RequireScope ¶
RequireScope denies requests that lack scope.
func RequireScopeForTarget ¶
RequireScopeForTarget denies requests that lack scope or whose PAT boundary does not cover the resolved target. OAuth bearer tokens are checked by scope only; PATs with an empty boundary are treated as full access.
func VerifyPassword ¶
VerifyPassword reports whether password matches hash.
func VerifySecret ¶
VerifySecret reports whether secret matches hash.
func WithAuthDisabled ¶
WithAuthDisabled marks a request as intentionally unauthenticated because auth is disabled.
Types ¶
type AuthorizationServerMetadataConfig ¶ added in v0.4.0
type AuthorizationServerMetadataConfig struct {
Issuer string
AuthorizationEndpoint string
TokenEndpoint string
RegistrationEndpoint string
ScopesSupported []string
}
AuthorizationServerMetadataConfig configures OAuth authorization server metadata.
type BearerConfig ¶
type BearerConfig struct {
Introspector TokenIntrospector
SessionFactory func() fosite.Session
ResourceMetadataURL string
TokenValidator TokenValidator
Now func() time.Time
RecordUsageTimeout time.Duration
MaxUsageRecorders int
AllowUnauthenticated bool
ExpectedAudience string
}
BearerConfig configures Bearer middleware.
type ClientRegistrar ¶ added in v0.4.0
ClientRegistrar persists dynamically registered OAuth clients.
type Config ¶
type Config struct {
Secret []byte //nolint:gosec // OAuth HMAC secret supplied by the consumer.
Issuer string
Audience string
Store storage.Store
KeyManager *keys.Manager
AllowedScopes []string
DefaultScopes []string
AccessTokenLifespan time.Duration
RefreshTokenLifespan time.Duration
AuthorizeCodeLifespan time.Duration
IDTokenLifespan time.Duration
}
Config holds settings for the OAuth/OIDC provider.
type PATAuthResult ¶
type PATAuthResult struct {
UserID string
TokenID string
ScopeType string
ScopeTarget string
Scopes []string
}
PATAuthResult contains the identity and scopes resolved from a PAT.
type PATValidator ¶
type PATValidator struct {
// contains filtered or unexported fields
}
PATValidator adapts a PATServicer to Bearer middleware.
func NewPATValidator ¶
func NewPATValidator(service Servicer) *PATValidator
NewPATValidator creates a PAT validator.
func (*PATValidator) RecordUsage ¶
func (v *PATValidator) RecordUsage(ctx context.Context, tokenID string)
RecordUsage updates last_used_at. Bearer calls this asynchronously.
func (*PATValidator) ValidateAndResolve ¶
func (v *PATValidator) ValidateAndResolve(ctx context.Context, rawToken string) (*PATAuthResult, error)
ValidateAndResolve validates a raw PAT and resolves its scope restriction.
type PKCEPair ¶
PKCEPair is a freshly generated PKCE verifier + challenge pair.
func NewPKCEPair ¶
NewPKCEPair generates an RFC 7636 S256 verifier/challenge pair.
type ProtectedResourceMetadataConfig ¶ added in v0.4.0
type ProtectedResourceMetadataConfig struct {
Resource string
AuthorizationServers []string
ScopesSupported []string
}
ProtectedResourceMetadataConfig configures RFC 9728 protected resource metadata.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider owns OAuth provider state and HTTP handlers.
func (*Provider) AuthorizeHandler ¶
func (p *Provider) AuthorizeHandler(resolve SubjectResolver) http.Handler
AuthorizeHandler returns an OAuth authorization endpoint that grants requested scopes immediately to whatever SubjectResolver returns.
This is a demo handler. It does not authenticate the browser session, does not collect explicit user consent, and does not bind the canonical resource audience server-side per RFC 8707. Production servers should replace it with oauth/consent.NewHandler.
func (*Provider) OAuth2Provider ¶
func (p *Provider) OAuth2Provider() fosite.OAuth2Provider
OAuth2Provider exposes the underlying Fosite provider for advanced consumers.
func (*Provider) RegisterHandler ¶
RegisterHandler returns the dynamic client registration handler.
func (*Provider) RegisterRoutes ¶
func (p *Provider) RegisterRoutes(mux *http.ServeMux, prefix string, resolve SubjectResolver)
RegisterRoutes mounts OAuth handlers on mux with prefix, for example prefix "/oauth" mounts "/oauth/authorize", "/oauth/token", "/oauth/revoke", and "/oauth/register".
func (*Provider) RevokeHandler ¶
RevokeHandler returns the OAuth token revocation endpoint.
func (*Provider) TokenHandler ¶
TokenHandler returns the OAuth token endpoint.
type RegistrationConfig ¶ added in v0.4.0
type RegistrationConfig struct {
Store ClientRegistrar
AllowedScopes []string
DefaultScopes []string
Audience string
DefaultTokenEndpointAuthMethod string
DefaultGrantTypes []string
DefaultResponseTypes []string
ClientIDPrefix string
}
RegistrationConfig configures a dynamic client registration handler.
type RegistrationHandler ¶
type RegistrationHandler struct {
// contains filtered or unexported fields
}
RegistrationHandler implements RFC 7591 dynamic client registration.
func (*RegistrationHandler) ServeHTTP ¶
func (h *RegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Servicer ¶
type Servicer interface {
ValidateToken(ctx context.Context, rawToken string) (PATEntity, error)
GetTokenScope(ctx context.Context, tokenID string) (scopeType string, scopeTarget string, err error)
UpdateLastUsed(ctx context.Context, tokenID string) error
}
Servicer is the consumer-owned persistence/service boundary for PATs.
type Subject ¶
type Subject struct {
ID string
Email string
GrantedScopes []string
// Extra carries consumer-specific session data. Values are copied into
// OIDC token claims as-is, so callers should use JSON-serializable values.
Extra map[string]any
}
Subject is the authenticated resource owner authorizing an OAuth client.
type SubjectResolver ¶
SubjectResolver returns the authenticated subject for an authorize request.
type TargetResolver ¶
TargetResolver resolves the target resource for the current request.
type TokenIntrospector ¶
type TokenIntrospector interface {
IntrospectToken(context.Context, string, fosite.TokenType, fosite.Session, ...string) (
fosite.TokenType,
fosite.AccessRequester,
error,
)
}
TokenIntrospector validates OAuth bearer tokens. Fosite providers implement this interface.
type TokenValidator ¶
type TokenValidator interface {
ValidateAndResolve(ctx context.Context, rawToken string) (*PATAuthResult, error)
RecordUsage(ctx context.Context, tokenID string)
}
TokenValidator validates personal access tokens.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package consent provides one shared OAuth 2.1 authorization-endpoint handler that performs browser-based login, optional 2FA challenge, explicit user consent, and produces a fosite authorization response with the canonical MCP audience bound server-side per RFC 8707.
|
Package consent provides one shared OAuth 2.1 authorization-endpoint handler that performs browser-based login, optional 2FA challenge, explicit user consent, and produces a fosite authorization response with the canonical MCP audience bound server-side per RFC 8707. |
|
consenttest
Package consenttest provides test fixtures for consent.Handler consumers.
|
Package consenttest provides test fixtures for consent.Handler consumers. |
|
hmacstore
Package hmacstore implements consent.ApprovalTokenStore using a stateless HMAC-signed payload plus an in-memory replay map.
|
Package hmacstore implements consent.ApprovalTokenStore using a stateless HMAC-signed payload plus an in-memory replay map. |
|
sessionstore
Package sessionstore implements consent.ApprovalTokenStore using a consumer-supplied session backend.
|
Package sessionstore implements consent.ApprovalTokenStore using a consumer-supplied session backend. |
|
Package keys manages OAuth JWT signing keys.
|
Package keys manages OAuth JWT signing keys. |
|
Package storage adapts consumer persistence to Fosite's OAuth storage interfaces.
|
Package storage adapts consumer persistence to Fosite's OAuth storage interfaces. |