Documentation
¶
Overview ¶
Package auth is the transport-free coordinator for the azugo authentication library.
Index ¶
- Constants
- Variables
- func NewOAuthAuthenticateError(code ErrorCode, description, realm, scope string, opts ...OAuthErrorOption) error
- func NewOAuthError(status int, code ErrorCode, description string, opts ...OAuthErrorOption) error
- func NewOAuthErrorFrom(err error) error
- func NewThrottledError(retryAfter time.Duration) error
- type Auth
- func (a *Auth) AuthenticateClient(ctx context.Context, creds ClientCredentials, ...) (*client.Client, error)
- func (a *Auth) AuthorizationCodeGrant(ctx context.Context, in AuthorizationCodeGrantRequest) (TokenResult, error)
- func (a *Auth) Authorize(ctx context.Context, in AuthorizeRequest) (AuthorizeResult, error)
- func (a *Auth) ClientCredentialsGrant(ctx context.Context, in ClientCredentialsGrantRequest) (TokenResult, error)
- func (a *Auth) Clients() client.Registry
- func (a *Auth) Codes() code.Store
- func (a *Auth) Config() *Configuration
- func (a *Auth) Introspect(ctx context.Context, in IntrospectRequest) (IntrospectionResponse, error)
- func (a *Auth) IntrospectToken(ctx context.Context, tok string) (UserInfo, *session.Session, error)
- func (a *Auth) JTI() jti.Store
- func (a *Auth) Keys() token.KeyProvider
- func (a *Auth) ListSessions(ctx context.Context, userID string, filter *session.Filter, ...) ([]*session.Session, *paginator.Paginator, error)
- func (a *Auth) Login(ctx context.Context, in LoginRequest) (LoginResult, error)
- func (a *Auth) Logout(ctx context.Context, in LogoutRequest) (LogoutResult, error)
- func (a *Auth) ReadSessionToken(ctx *azugo.Context) string
- func (a *Auth) Refresh(ctx context.Context, in RefreshRequest) (LoginResult, error)
- func (a *Auth) RevokeSession(ctx context.Context, userID, sessionID string) error
- func (a *Auth) RevokeToken(ctx context.Context, in RevokeTokenRequest) error
- func (a *Auth) Sessions() session.Store
- func (a *Auth) Users() UserProvider
- func (a *Auth) ValidateJWTAccessToken(ctx context.Context, tok string) (UserInfo, error)
- func (a *Auth) WriteCookie(ctx *azugo.Context, d *CookieDirective)
- type AuthorizationCodeGrantRequest
- type AuthorizeRequest
- type AuthorizeResult
- type ClaimMapper
- type ClaimMapperFunc
- type ClientCredentials
- type ClientCredentialsGrantRequest
- type Configuration
- type CookieCtx
- type CookieDirective
- type ErrorCode
- type ExternalUserProvider
- type IntrospectRequest
- type IntrospectionResponse
- type IssuerCtx
- type LoginRequest
- type LoginResult
- type LogoutRequest
- type LogoutResult
- type OAuthError
- type OAuthErrorOption
- type Option
- type PasswordChanger
- type PasswordResetter
- type ProfileManager
- type RefreshRequest
- type Registerer
- type RegistrationRequest
- type RevokeTokenRequest
- type TokenResult
- type TransactionCtx
- type TransactorFunc
- type TxRunner
- type UserInfo
- type UserProvider
Constants ¶
const AssertionTypeJWTBearer = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
AssertionTypeJWTBearer is the RFC 7523 client_assertion_type for private_key_jwt.
Variables ¶
var ( // ErrInvalidCredentials is returned for a bad username and/or password. ErrInvalidCredentials = contract.ErrInvalidCredentials // ErrUserAlreadyExists is returned when the account is taken. ErrUserAlreadyExists = contract.ErrUserAlreadyExists // ErrUserNotFound is returned when the user is unknown. ErrUserNotFound = contract.ErrUserNotFound // ErrUnsupportedGrantType is returned for grant_type this library does not // support. ErrUnsupportedGrantType = errors.New("unsupported grant type") // ErrLoginRequired is returned by Refresh when the presented session cookie is missing, // expired, or otherwise cannot be silently re-authenticated. ErrLoginRequired = errors.New("login required") )
Functions ¶
func NewOAuthAuthenticateError ¶
func NewOAuthAuthenticateError(code ErrorCode, description, realm, scope string, opts ...OAuthErrorOption) error
NewOAuthAuthenticateError creates a 401 Unauthorized OAuthError that emits an RFC 6750 WWW-Authenticate: Bearer challenge.
func NewOAuthError ¶
func NewOAuthError(status int, code ErrorCode, description string, opts ...OAuthErrorOption) error
NewOAuthError creates an OAuthError with the given HTTP status, error code and description. Optional attributes (e.g. OAuthErrorURI option).
func NewOAuthErrorFrom ¶
NewOAuthErrorFrom maps a authentication specifc errors to OAuth 2.0 error and status code.
func NewThrottledError ¶
NewThrottledError creates a 429 Too Many Requests OAuthError carrying a Retry-After hint.
Types ¶
type Auth ¶
type Auth struct {
// Cookie provides session cookie attribute helpers.
Cookie CookieCtx
// Issuer provides OIDC issuer resolution helpers.
Issuer IssuerCtx
// Transaction provides multi-write transaction helpers.
Transaction TransactionCtx
// contains filtered or unexported fields
}
Auth is the transport-free authentication service.
func New ¶
func New(app *core.App, config *Configuration, users UserProvider, sessions session.Store, clients client.Registry, opts ...Option) (*Auth, error)
New creates an Auth instance.
func (*Auth) AuthenticateClient ¶
func (a *Auth) AuthenticateClient(ctx context.Context, creds ClientCredentials, baseURL, mountPath, tokenEndpoint string) (*client.Client, error)
AuthenticateClient resolves client credentials and enforces the client's registered TokenEndpointAuthMethod.
func (*Auth) AuthorizationCodeGrant ¶
func (a *Auth) AuthorizationCodeGrant(ctx context.Context, in AuthorizationCodeGrantRequest) (TokenResult, error)
AuthorizationCodeGrant redeems a single-use authorization code for tokens. A replayed code revokes the bound session.
func (*Auth) Authorize ¶
func (a *Auth) Authorize(ctx context.Context, in AuthorizeRequest) (AuthorizeResult, error)
Authorize handles the authorization-code flow: it validates the request, establishes the user from the session cookie, mints a single-use code and returns the redirect.
func (*Auth) ClientCredentialsGrant ¶
func (a *Auth) ClientCredentialsGrant(ctx context.Context, in ClientCredentialsGrantRequest) (TokenResult, error)
ClientCredentialsGrant issues a JWT access token to a confidential client without a user or session.
func (*Auth) Introspect ¶
func (a *Auth) Introspect(ctx context.Context, in IntrospectRequest) (IntrospectionResponse, error)
Introspect implements RFC 7662 for confidential clients: opaque PASETO tokens are validated through the in-process issuer path, JWT access tokens by signature + deny-list.
func (*Auth) IntrospectToken ¶
IntrospectToken validates a Bearer access token or session-cookie token and returns the resolved user and session.
func (*Auth) Keys ¶
func (a *Auth) Keys() token.KeyProvider
Keys returns the configured key provider, or nil in introspect-only mode.
func (*Auth) ListSessions ¶
func (a *Auth) ListSessions(ctx context.Context, userID string, filter *session.Filter, page *paginator.Paginator) ([]*session.Session, *paginator.Paginator, error)
ListSessions returns userID's sessions, ordered by LastSeen descending.
func (*Auth) Login ¶
func (a *Auth) Login(ctx context.Context, in LoginRequest) (LoginResult, error)
Login authenticates a password-grant request, creates an active session and returns the directives the caller should apply.
func (*Auth) Logout ¶
func (a *Auth) Logout(ctx context.Context, in LogoutRequest) (LogoutResult, error)
Logout is the authoritative server-side logout.
func (*Auth) ReadSessionToken ¶
ReadSessionToken extracts the presented credential.
func (*Auth) Refresh ¶
func (a *Auth) Refresh(ctx context.Context, in RefreshRequest) (LoginResult, error)
Refresh performs the portal's silent re-authentication.
func (*Auth) RevokeSession ¶
RevokeSession revokes sessionID after verifying it belongs to userID.
func (*Auth) RevokeToken ¶
func (a *Auth) RevokeToken(ctx context.Context, in RevokeTokenRequest) error
RevokeToken implements RFC 7009: an opaque token revokes its session and JTI when it belongs to the calling client.
func (*Auth) Users ¶
func (a *Auth) Users() UserProvider
Users returns the configured user provider.
func (*Auth) ValidateJWTAccessToken ¶
ValidateJWTAccessToken verifies a signed JWT bearer token (signature, expiry, deny-list) and resolves its subject through the UserProvider.
func (*Auth) WriteCookie ¶
func (a *Auth) WriteCookie(ctx *azugo.Context, d *CookieDirective)
WriteCookie applies a CookieDirective to the response.
type AuthorizationCodeGrantRequest ¶
type AuthorizationCodeGrantRequest struct {
Credentials ClientCredentials
Code string
RedirectURI string
CodeVerifier string
BaseURL string
MountPath string
TokenEndpoint string
IP string
}
AuthorizationCodeGrantRequest carries an authorization_code redemption.
type AuthorizeRequest ¶
type AuthorizeRequest struct {
ResponseType string
ClientID string
RedirectURI string
Scope string
State string
Nonce string
ACRValues string
CodeChallenge string
CodeChallengeMethod string
// SessionToken is the presented session cookie establishing the user's identity.
SessionToken string
}
AuthorizeRequest carries an OIDC authorization request (GET /authorize, response_type=code).
type AuthorizeResult ¶
type AuthorizeResult struct {
RedirectURI string
}
AuthorizeResult carries the redirect the caller should perform.
type ClaimMapper ¶
type ClaimMapper = contract.ClaimMapper
ClaimMapper maps a UserInfo into token / id_token claims.
type ClaimMapperFunc ¶
type ClaimMapperFunc = contract.ClaimMapperFunc
ClaimMapperFunc adapts a plain function to the ClaimMapper interface.
type ClientCredentials ¶
type ClientCredentials struct {
ClientID string
Secret string
AssertionType string
Assertion string
}
ClientCredentials carries the authentication material presented at a client-authenticated endpoint (/token, /introspect, /revoke).
type ClientCredentialsGrantRequest ¶
type ClientCredentialsGrantRequest struct {
Credentials ClientCredentials
Scope string
BaseURL string
MountPath string
TokenEndpoint string
IP string
}
ClientCredentialsGrantRequest carries a client_credentials request.
type Configuration ¶
type Configuration = contract.Configuration
Configuration is the authentication configuration section.
type CookieCtx ¶
type CookieCtx struct {
// contains filtered or unexported fields
}
CookieCtx provides session cookie attribute helpers derived from the auth configuration and app environment.
type CookieDirective ¶
type CookieDirective struct {
Name, Value, Path, Domain string
MaxAge int
Secure, HTTPOnly bool
SameSite string
}
CookieDirective describes how to set or clear the session cookie.
A negative MaxAge deletes the cookie.
type ErrorCode ¶
type ErrorCode string
ErrorCode is an RFC 6749 / RFC 6750 / RFC 9470 OAuth 2.0 error code.
const ( ErrCodeLoginRequired ErrorCode = "login_required" ErrCodeSessionRevoked ErrorCode = "session_revoked" ErrCodeInvalidRequest ErrorCode = "invalid_request" ErrCodeInvalidClient ErrorCode = "invalid_client" ErrCodeInvalidGrant ErrorCode = "invalid_grant" ErrCodeUnsupportedGrantType ErrorCode = "unsupported_grant_type" ErrCodeAccessDenied ErrorCode = "access_denied" ErrCodeUnmetAuthenticationRequirements ErrorCode = "unmet_authentication_requirements" ErrCodeInvalidDPoPProof ErrorCode = "invalid_dpop_proof" ErrCodeUseDPoPNonce ErrorCode = "use_dpop_nonce" ErrCodeInvalidToken ErrorCode = "invalid_token" ErrCodeInsufficientScope ErrorCode = "insufficient_scope" ErrCodeServerError ErrorCode = "server_error" ErrCodeUnsupportedResponseType ErrorCode = "unsupported_response_type" ErrCodeInvalidScope ErrorCode = "invalid_scope" ErrCodeSlowDown ErrorCode = "slow_down" )
OAuth error codes.
type ExternalUserProvider ¶
type ExternalUserProvider = contract.ExternalUserProvider
ExternalUserProvider resolves identities for external IdP logins and passwordless authenticators.
type IntrospectRequest ¶
type IntrospectRequest struct {
Credentials ClientCredentials
Token string
BaseURL string
MountPath string
TokenEndpoint string
}
IntrospectRequest carries an RFC 7662 introspection request. The token_type_hint parameter is ignored.
type IntrospectionResponse ¶
type IntrospectionResponse struct {
Active bool `json:"active"`
Scope string `json:"scope,omitempty"`
ClientID string `json:"client_id,omitempty"`
Username string `json:"username,omitempty"`
TokenType string `json:"token_type,omitempty"`
Subject string `json:"sub,omitempty"`
Audience string `json:"aud,omitempty"`
Issuer string `json:"iss,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
SessionID string `json:"sid,omitempty"`
}
IntrospectionResponse is the RFC 7662 introspection response.
type IssuerCtx ¶
type IssuerCtx struct {
// contains filtered or unexported fields
}
IssuerCtx provides OIDC issuer resolution helpers derived from the auth configuration.
type LoginRequest ¶
type LoginRequest struct {
ClientID string
Username string
Password string
ReturnTo string
RequestTLS bool
BaseURL string
MountPath string
// IP is the caller's remote address.
IP string
}
LoginRequest carries the password-grant credentials and the request-derived values.
type LoginResult ¶
type LoginResult struct {
Status session.Status
Cookie *CookieDirective
AccessToken string
// IDToken is set only for a client.ResponseModeJSON client whose granted scope contains
// "openid".
IDToken string
ExpiresIn int
Redirect string
}
LoginResult is returned by Login and Refresh based on client mode and configuration.
type LogoutRequest ¶
LogoutRequest carries the presented token and the request-derived values.
type LogoutResult ¶
type LogoutResult struct {
ClearCookie *CookieDirective
}
LogoutResult is returned by Logout.
type OAuthError ¶
type OAuthError struct {
// Code is the RFC 6749 error code (e.g. "invalid_token", "insufficient_scope").
Code ErrorCode
// Description is the human-readable error_description.
Description string
// RetryAfter, when non-zero, is surfaced as a Retry-After header (throttled requests).
RetryAfter time.Duration
// contains filtered or unexported fields
}
OAuthError represents an OAuth 2.0 error response.
func (*OAuthError) ErrorHeaders ¶
func (e *OAuthError) ErrorHeaders() iter.Seq2[string, string]
ErrorHeaders sets the WWW-Authenticate: Bearer header and, when throttled, Retry-After.
func (*OAuthError) MarshalError ¶
func (e *OAuthError) MarshalError(contentType string) ([]byte, string, bool)
MarshalError renders the RFC 6749 JSON response.
func (*OAuthError) SafeError ¶
func (e *OAuthError) SafeError() string
SafeError returns error description.
func (*OAuthError) StatusCode ¶
func (e *OAuthError) StatusCode() int
StatusCode to set for error response.
func (*OAuthError) Unwrap ¶
func (e *OAuthError) Unwrap() error
Unwrap exposes the wrapped cause (set by NewOAuthErrorFrom) so the server can log/inspect the original error with errors.Is/As.
type OAuthErrorOption ¶
type OAuthErrorOption func(*OAuthError)
OAuthErrorOption customizes an OAuthError at construction.
func OAuthErrorURI ¶
func OAuthErrorURI(uri string) OAuthErrorOption
OAuthErrorURI sets the optional error_uri pointing to error documentation.
type Option ¶
type Option func(*Auth)
Option configures an Auth instance at construction.
func CodeStore ¶
CodeStore replaces the default cache-backed authorization-code store with a custom.
func CookieScopeToBasePath ¶
func CookieScopeToBasePath() Option
CookieScopeToBasePath makes the default session cookie Path resolve to the app's base path.
func Events ¶
Events replaces the default audit event sink. The default writes each event as a structured log record via the request logger.
func KeyProvider ¶
func KeyProvider(p token.KeyProvider) Option
KeyProvider replaces the default ConfigKeyProvider with a custom.
func Throttle ¶
Throttle replaces the default ThrottleConfig-driven brute-force guard with a custom.
func Transactor ¶
Transactor to allow to run multi-write handler sequences so they can be made atomic.
type PasswordChanger ¶
type PasswordChanger = contract.PasswordChanger
PasswordChanger is an optional UserProvider extension for changing a password.
type PasswordResetter ¶
type PasswordResetter = contract.PasswordResetter
PasswordResetter is an optional UserProvider extension for resetting a forgotten password.
type ProfileManager ¶
type ProfileManager = contract.ProfileManager
ProfileManager is an optional UserProvider extension for user profile data.
type RefreshRequest ¶
type RefreshRequest struct {
Token string
ReturnTo string
RequestTLS bool
BaseURL string
MountPath string
}
RefreshRequest carries the presented refresh token and the request-derived values.
type Registerer ¶
type Registerer = contract.Registerer
Registerer is an optional UserProvider extension for registering new accounts.
type RegistrationRequest ¶
type RegistrationRequest = contract.RegistrationRequest
RegistrationRequest carries the data for a new user registration.
type RevokeTokenRequest ¶
type RevokeTokenRequest struct {
Credentials ClientCredentials
Token string
BaseURL string
MountPath string
TokenEndpoint string
IP string
}
RevokeTokenRequest carries an RFC 7009 revocation request. The token_type_hint parameter is ignored.
type TokenResult ¶
type TokenResult struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
IDToken string `json:"id_token,omitempty"`
Scope string `json:"scope,omitempty"`
}
TokenResult is the RFC 6749 §5.1 access token response for the authorization_code and client_credentials grants.
type TransactionCtx ¶
type TransactionCtx struct {
// contains filtered or unexported fields
}
TransactionCtx provides multi-write transaction helpers.
type TransactorFunc ¶
TransactorFunc adapts a plain function to the TxRunner interface.
type TxRunner ¶
TxRunner allows to run the multi-write handler sequences so they can be made atomic.
type UserProvider ¶
type UserProvider = contract.UserProvider
UserProvider validates credentials and loads user data.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client defines the OAuth client model and the Registry that resolves client metadata.
|
Package client defines the OAuth client model and the Registry that resolves client metadata. |
|
Package code implements the single-use authorization codes minted by GET /authorize and redeemed by the authorization_code grant at POST /token.
|
Package code implements the single-use authorization codes minted by GET /authorize and redeemed by the authorization_code grant at POST /token. |
|
Package contract holds the cross-cutting types and interfaces shared between the base auth package and the plugin packages.
|
Package contract holds the cross-cutting types and interfaces shared between the base auth package and the plugin packages. |
|
Package event defines the audit event sink for security-relevant auth events.
|
Package event defines the audit event sink for security-relevant auth events. |
|
Package jti implements the JTI (JWT/token ID) allowlist that backs token revocation and session-bound replay detection.
|
Package jti implements the JTI (JWT/token ID) allowlist that backs token revocation and session-bound replay detection. |
|
Package middleware provides azugo HTTP middleware over the transport-free auth service.
|
Package middleware provides azugo HTTP middleware over the transport-free auth service. |
|
Package routes is the optional HTTP adapter tier over the transport-free auth service.
|
Package routes is the optional HTTP adapter tier over the transport-free auth service. |
|
Package session defines the server-side session model and the Store interface that persists it.
|
Package session defines the server-side session model and the Store interface that persists it. |
|
Package throttle guards credential-checking endpoints against brute force.
|
Package throttle guards credential-checking endpoints against brute force. |
|
Package token implements the PASETO v4.local tokens used by the auth library: short-lived access tokens, long-lived session cookies, and API-key tokens.
|
Package token implements the PASETO v4.local tokens used by the auth library: short-lived access tokens, long-lived session cookies, and API-key tokens. |