Documentation
¶
Index ¶
- Constants
- Variables
- func AllowedIDTokenAlgorithms(advertised []string, pinned string, allowed []string) ([]string, error)
- func ApplyClientAuth(config map[string]any, key any, body map[string]string, ...) error
- func B64URLDecode(value, what string) ([]byte, error)
- func B64URLEncode(data []byte) string
- func CodeChallengeS256(verifier string) string
- func DiscoverProvider(metadataURL string, tlsOpt map[string]string) (map[string]any, error)
- func FetchJWKS(jwksURI string, tlsOpt map[string]string) ([]map[string]any, error)
- func HTU(rawURL string) string
- func HashClaim(value, alg string) string
- func IDTokenAlgorithms(advertised []string, pinned string) ([]string, error)
- func IssuerFromMetadataURL(metadataURL string) (string, error)
- func LoadPrivateKey(pemText, alg string) (any, error)
- func PublicKeyFromJWK(jwk map[string]any) (any, error)
- func RandomToken() (string, error)
- func ReadConfigFile(path string) (map[string]any, error)
- func ResolveConfig(raw map[string]any, baseDir string) (map[string]any, error)
- func SignJWS(key any, alg string, input []byte) ([]byte, error)
- func SubjectOf(subID map[string]any, issuer string) map[string]string
- func TrackedFromLogin(localID string, login *Login, expiresAt *float64) map[string]any
- func ValidateIDToken(token string, checks tokenChecks) (map[string]any, error)
- func ValidateProviderMetadata(raw map[string]any, expectedIssuer string) (map[string]any, error)
- func VerifyCompactJWS(token string, keys []map[string]any) (map[string]any, map[string]any, error)
- func VerifySignature(key any, alg string, input, signature []byte) bool
- func WellKnownURLs(issuer, suffix string) []string
- type Access
- type AuthorizeOptions
- type EndSessionOptions
- type Logger
- type Login
- type MemorySessionStore
- type OidcError
- type ReceiverOptions
- type RelyingParty
- func (rp *RelyingParty) Authorize() (*Transaction, error)
- func (rp *RelyingParty) AuthorizeOptions(opts AuthorizeOptions) (*Transaction, error)
- func (rp *RelyingParty) ClientCredentials(scope string) (Access, error)
- func (rp *RelyingParty) Close()
- func (rp *RelyingParty) Complete(query map[string]string, txn *Transaction) (*Login, error)
- func (rp *RelyingParty) CompleteSession(query map[string]string, txn *Transaction, sessionID string) (*Login, error)
- func (rp *RelyingParty) Config() map[string]any
- func (rp *RelyingParty) DPoPEnabled() bool
- func (rp *RelyingParty) DPoPThumbprint() string
- func (rp *RelyingParty) EndSessionURL(opts EndSessionOptions) string
- func (rp *RelyingParty) Metadata() map[string]any
- func (rp *RelyingParty) Refresh(refreshToken, scope string, previous *Login) (*Login, error)
- func (rp *RelyingParty) RemoveSession(sessionID string) error
- func (rp *RelyingParty) ResourceRequest(method, rawURL, accessToken, tokenType string, headers map[string]string, ...) (status int, contentType string, responseBody string, err error)
- func (rp *RelyingParty) Session(sessionID string) (*Login, error)
- func (rp *RelyingParty) SessionAccessToken(sessionID string) (Access, error)
- func (rp *RelyingParty) SessionStore() SessionStore
- func (rp *RelyingParty) SetSessionStore(store SessionStore)
- func (rp *RelyingParty) TLS() map[string]string
- func (rp *RelyingParty) UserInfo(accessToken, expectedSub string) (map[string]any, error)
- type SessionIndex
- func (s *SessionIndex) Add(session map[string]any) map[string]any
- func (s *SessionIndex) ByEmail(email string) []string
- func (s *SessionIndex) ByIDPSession(idpSession string) []string
- func (s *SessionIndex) ByPerson(person string) []string
- func (s *SessionIndex) Live() []map[string]any
- func (s *SessionIndex) Remove(localID string) map[string]any
- type SessionStore
- type SignalEvent
- type SignalsReceiver
- func (r *SignalsReceiver) On(eventType string, handler func(SignalEvent)) *SignalsReceiver
- func (r *SignalsReceiver) PollOnce() (bool, error)
- func (r *SignalsReceiver) Start() error
- func (r *SignalsReceiver) Stop()
- func (r *SignalsReceiver) Stream() map[string]any
- func (r *SignalsReceiver) Track(session map[string]any)
- func (r *SignalsReceiver) Untrack(localID string)
- func (r *SignalsReceiver) Verify(state string) error
- type Transaction
Constants ¶
const DefaultScopes = "openid profile email offline_access ssf.read ssf.manage"
DefaultScopes are the scopes RelyingParty asks for unless the configuration names others.
const SessionRefreshMargin = 30.0
SessionRefreshMargin is how much life a cached access token must have left for SessionAccessToken to return it without a refresh.
Variables ¶
var ( CAEPEvents = eventsWithPrefix(caepPrefix) RISCEvents = eventsWithPrefix(riscPrefix) )
CAEPEvents and RISCEvents follow the order the event names were defined.
var AsymmetricJWSAlgs = []string{
"RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384", "ES512", "EdDSA", "Ed25519",
}
AsymmetricJWSAlgs are the asymmetric JWS algorithms this client verifies. HMAC algorithms are accepted only when an ID token names one explicitly.
var DefaultEndingEvents = []string{ Event["sessionRevoked"], Event["accountDisabled"], Event["accountPurged"], Event["credentialCompromise"], Event["sessionsRevoked"], }
DefaultEndingEvents end local sessions.
var DefaultEventsRequested = []string{ Event["sessionRevoked"], Event["credentialChange"], Event["assuranceLevelChange"], Event["tokenClaimsChange"], Event["accountDisabled"], Event["accountEnabled"], Event["accountPurged"], Event["credentialCompromise"], Event["sessionsRevoked"], Event["accountCredentialChangeRequired"], }
DefaultEventsRequested is the poll stream's events_requested when the caller does not name one.
var DefaultIDTokenSigningAlgs = []string{"RS256"}
DefaultIDTokenSigningAlgs is the ID token algorithm allow-list when the configuration names none: RS256 only.
var Event = map[string]string{
"sessionRevoked": caepPrefix + "session-revoked",
"tokenClaimsChange": caepPrefix + "token-claims-change",
"credentialChange": caepPrefix + "credential-change",
"assuranceLevelChange": caepPrefix + "assurance-level-change",
"deviceComplianceChange": caepPrefix + "device-compliance-change",
"sessionEstablished": caepPrefix + "session-established",
"sessionPresented": caepPrefix + "session-presented",
"riskLevelChange": caepPrefix + "risk-level-change",
"accountCredentialChangeRequired": riscPrefix + "account-credential-change-required",
"accountPurged": riscPrefix + "account-purged",
"accountDisabled": riscPrefix + "account-disabled",
"accountEnabled": riscPrefix + "account-enabled",
"identifierChanged": riscPrefix + "identifier-changed",
"identifierRecycled": riscPrefix + "identifier-recycled",
"credentialCompromise": riscPrefix + "credential-compromise",
"optIn": riscPrefix + "opt-in",
"optOutInitiated": riscPrefix + "opt-out-initiated",
"optOutCancelled": riscPrefix + "opt-out-cancelled",
"optOutEffective": riscPrefix + "opt-out-effective",
"recoveryActivated": riscPrefix + "recovery-activated",
"recoveryInformationChanged": riscPrefix + "recovery-information-changed",
"sessionsRevoked": riscPrefix + "sessions-revoked",
"verification": ssfPrefix + "verification",
"streamUpdated": ssfPrefix + "stream-updated",
}
Event is the CAEP, RISC, and SSF event type each short name refers to.
var SupportedJWSAlgs = []string{
"RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384", "ES512", "EdDSA", "Ed25519",
}
SupportedJWSAlgs are the algorithms a Security Event Token may use.
Functions ¶
func AllowedIDTokenAlgorithms ¶
func AllowedIDTokenAlgorithms(advertised []string, pinned string, allowed []string) ([]string, error)
AllowedIDTokenAlgorithms is the set of ID token algorithms this client accepts: the pinned one when there is one, else the allow-list (RS256 when empty), each kept only when the provider advertises it too. "none" is never accepted, and a symmetric algorithm only when it is named.
func ApplyClientAuth ¶
func ApplyClientAuth(config map[string]any, key any, body map[string]string, headers map[string]string, audience string) error
ApplyClientAuth adds this client's authentication. A client assertion's audience is the issuer.
func B64URLDecode ¶
func B64URLEncode ¶
func CodeChallengeS256 ¶
CodeChallengeS256 is RFC 7636 S256: base64url(SHA-256(verifier)), with no padding.
func DiscoverProvider ¶
func HashClaim ¶
HashClaim is the left half of the digest the algorithm names, base64url, no padding.
func IDTokenAlgorithms ¶
IDTokenAlgorithms is AllowedIDTokenAlgorithms with the default allow-list.
func IssuerFromMetadataURL ¶
func LoadPrivateKey ¶
func RandomToken ¶
RandomToken is 32 random bytes, base64url. A PKCE verifier and a state or nonce.
func ReadConfigFile ¶
ReadConfigFile reads a configuration file. Relative paths start in the file's directory.
func ResolveConfig ¶
ResolveConfig reads a configuration object and fills the defaults. baseDir is where relative paths start. signals is omitted when CAEP and RISC are on, and present as false when they are off.
func SubjectOf ¶
SubjectOf reads an event's subject. A complex subject's user is the person and its session the provider session.
func TrackedFromLogin ¶
TrackedFromLogin is what an OpenID Connect sign-in says about the session, for the index.
func ValidateIDToken ¶
ValidateIDToken verifies an ID token and returns its payload.
func VerifyCompactJWS ¶
VerifyCompactJWS verifies a compact JWS against the keys of a JWKS and returns its header and payload. The key comes from the JWKS, never from the token.
func VerifySignature ¶
VerifySignature reports whether signature matches. A malformed signature is a miss.
func WellKnownURLs ¶
Types ¶
type Access ¶
type Access struct {
AccessToken string
TokenType string
ExpiresAt float64
HasExpires bool
Scope string
}
Access is a client-credentials token.
type AuthorizeOptions ¶
type AuthorizeOptions struct {
RedirectURI string
Scope string
Prompt string
LoginHint string
MaxAge *float64
ACRValues string
ExpectedSub string
}
AuthorizeOptions are the optional Authentication Request parameters. RedirectURI is held to the same rule as the configured one. ACRValues replaces the acrValues setting for this request.
type EndSessionOptions ¶
EndSessionOptions builds an RP-Initiated Logout URL. A nil PostLogout uses the configuration's post-logout redirect.
type Login ¶
type Login struct {
Subject string
Issuer string
Claims map[string]any
AccessToken string
IDToken string
TokenType string
Email string
Name string
PreferredUsername string
SID string
RefreshToken string
ExpiresAt float64
HasExpires bool
Scope string
// Session is the session-cache handle (CompleteSession), or "".
Session string
}
Login is a completed sign-in. Tokens stay in this process.
type MemorySessionStore ¶
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore is the default SessionStore: this process's memory.
func NewMemorySessionStore ¶
func NewMemorySessionStore() *MemorySessionStore
func (*MemorySessionStore) Delete ¶
func (m *MemorySessionStore) Delete(sessionID string) error
func (*MemorySessionStore) Len ¶
func (m *MemorySessionStore) Len() int
Len is the number of sessions held.
type OidcError ¶
OidcError is every failure this module reports. Code is config, discovery, protocol, validation, signature, session, or ssf. OAuthError is the OAuth error code when the provider named one.
type ReceiverOptions ¶
type ReceiverOptions struct {
TLS map[string]string
Client *RelyingParty
Scope string
Headers func() map[string]string
EventsRequested []string
EndOn []string
Index *SessionIndex
Description string
PollIntervalMs int
MaxEvents int
Log Logger
}
ReceiverOptions are the optional parts of a SignalsReceiver. Client makes every call to the transmitter with Client's client-credentials token for Scope (default ssf.manage), with a DPoP proof when that token is DPoP-bound, and clears the session cache entry of every session an event ends. Headers, when Client is nil, supplies the headers instead.
type RelyingParty ¶
type RelyingParty struct {
// contains filtered or unexported fields
}
RelyingParty is a confidential OpenID Connect client.
func Create ¶
func Create(config map[string]any) (*RelyingParty, error)
Create discovers the provider, or uses metadata already loaded, and returns a client.
func FromFile ¶
func FromFile(path string) (*RelyingParty, error)
func (*RelyingParty) Authorize ¶
func (rp *RelyingParty) Authorize() (*Transaction, error)
Authorize builds the Authentication Request. Keep the transaction until Complete.
func (*RelyingParty) AuthorizeOptions ¶
func (rp *RelyingParty) AuthorizeOptions(opts AuthorizeOptions) (*Transaction, error)
func (*RelyingParty) ClientCredentials ¶
func (rp *RelyingParty) ClientCredentials(scope string) (Access, error)
ClientCredentials performs the client-credentials grant and caches the token until shortly before it expires.
func (*RelyingParty) Close ¶ added in v0.2.1
func (rp *RelyingParty) Close()
Close stops the embedded PEP. A client with no pep section has nothing to stop.
func (*RelyingParty) Complete ¶
func (rp *RelyingParty) Complete(query map[string]string, txn *Transaction) (*Login, error)
Complete exchanges the code from the redirect URI. A code is accepted once.
func (*RelyingParty) CompleteSession ¶
func (rp *RelyingParty) CompleteSession(query map[string]string, txn *Transaction, sessionID string) (*Login, error)
CompleteSession is Complete, and keeps the sign-in's tokens in the session store under sessionID. An empty sessionID makes a new handle. The handle is Login.Session either way.
func (*RelyingParty) Config ¶
func (rp *RelyingParty) Config() map[string]any
func (*RelyingParty) DPoPEnabled ¶
func (rp *RelyingParty) DPoPEnabled() bool
DPoPEnabled reports whether this client sends DPoP proofs.
func (*RelyingParty) DPoPThumbprint ¶
func (rp *RelyingParty) DPoPThumbprint() string
DPoPThumbprint is the RFC 7638 thumbprint of the DPoP key (dpop_jkt), or "".
func (*RelyingParty) EndSessionURL ¶
func (rp *RelyingParty) EndSessionURL(opts EndSessionOptions) string
EndSessionURL is the RP-Initiated Logout URL, or "" when the provider has no end-session endpoint.
func (*RelyingParty) Metadata ¶
func (rp *RelyingParty) Metadata() map[string]any
func (*RelyingParty) Refresh ¶
func (rp *RelyingParty) Refresh(refreshToken, scope string, previous *Login) (*Login, error)
Refresh performs the refresh-token grant. A new ID token's subject is checked against previous.
func (*RelyingParty) RemoveSession ¶
func (rp *RelyingParty) RemoveSession(sessionID string) error
RemoveSession clears a session's tokens: sign-out, or a CAEP session-revoked. It takes the session's lock, so a refresh already in flight finishes first and cannot write the session back afterwards.
func (*RelyingParty) ResourceRequest ¶
func (rp *RelyingParty) ResourceRequest(method, rawURL, accessToken, tokenType string, headers map[string]string, body *string) (status int, contentType string, responseBody string, err error)
ResourceRequest calls a protected resource with an access token. A token whose type is DPoP is sent as "DPoP <token>" with a proof carrying ath, and a 401 asking for a DPoP nonce is retried once with the nonce the response gave. Any other token is sent as a Bearer token.
func (*RelyingParty) Session ¶
func (rp *RelyingParty) Session(sessionID string) (*Login, error)
Session is the sign-in a session holds, or nil.
func (*RelyingParty) SessionAccessToken ¶
func (rp *RelyingParty) SessionAccessToken(sessionID string) (Access, error)
SessionAccessToken returns a valid access token for a session: the cached one while it has more than 30 seconds left, otherwise one obtained with the refresh token (with a DPoP proof when DPoP is in use), and the cache is updated. A failed refresh removes the session and is refused.
func (*RelyingParty) SessionStore ¶
func (rp *RelyingParty) SessionStore() SessionStore
SessionStore is the store CompleteSession writes to.
func (*RelyingParty) SetSessionStore ¶
func (rp *RelyingParty) SetSessionStore(store SessionStore)
SetSessionStore replaces the session store. Call it before the first sign-in.
func (*RelyingParty) TLS ¶
func (rp *RelyingParty) TLS() map[string]string
type SessionIndex ¶
type SessionIndex struct {
// contains filtered or unexported fields
}
SessionIndex is one process's memory of local sessions, bounded: expired sessions go first, then the oldest.
func NewSessionIndex ¶
func NewSessionIndex(maxSessions int) *SessionIndex
func (*SessionIndex) ByEmail ¶
func (s *SessionIndex) ByEmail(email string) []string
func (*SessionIndex) ByIDPSession ¶
func (s *SessionIndex) ByIDPSession(idpSession string) []string
func (*SessionIndex) ByPerson ¶
func (s *SessionIndex) ByPerson(person string) []string
func (*SessionIndex) Live ¶ added in v0.2.1
func (s *SessionIndex) Live() []map[string]any
Live lists the sessions still in force, oldest first.
type SessionStore ¶
type SessionStore interface {
Get(sessionID string) (*Login, error)
Set(sessionID string, login *Login) error
Delete(sessionID string) error
}
SessionStore holds the tokens of completed sign-ins, keyed by a session identifier the application chooses. Get answers nil and no error for a session it does not hold. The default is MemorySessionStore; an application that runs several processes replaces it with a shared store.
type SignalEvent ¶
type SignalEvent struct {
Type string
Payload map[string]any
Subject map[string]string
JTI string
Sessions []string
Ended bool
IAT *float64
Txn string
}
SignalEvent is one event from a Security Event Token.
type SignalsReceiver ¶
type SignalsReceiver struct {
// contains filtered or unexported fields
}
SignalsReceiver finds or creates this application's poll stream and turns events into local sessions.
func NewSignalsReceiver ¶
func NewSignalsReceiver(transmitter string, onEnd func([]string, SignalEvent), opts ReceiverOptions) (*SignalsReceiver, error)
func (*SignalsReceiver) On ¶
func (r *SignalsReceiver) On(eventType string, handler func(SignalEvent)) *SignalsReceiver
func (*SignalsReceiver) PollOnce ¶
func (r *SignalsReceiver) PollOnce() (bool, error)
PollOnce is one poll. A call while a poll is in flight joins it.
func (*SignalsReceiver) Start ¶
func (r *SignalsReceiver) Start() error
Start finds the transmitter and the stream, then polls until Stop.
func (*SignalsReceiver) Stop ¶
func (r *SignalsReceiver) Stop()
Stop stops polling. Acknowledgements not yet sent go out on the next Start's first poll. It waits until the poll loop has left, so a later PollOnce is not the poll Start already had in flight.
func (*SignalsReceiver) Stream ¶
func (r *SignalsReceiver) Stream() map[string]any
Stream is a copy of the stream the receiver is polling, once started.
func (*SignalsReceiver) Track ¶
func (r *SignalsReceiver) Track(session map[string]any)
func (*SignalsReceiver) Untrack ¶
func (r *SignalsReceiver) Untrack(localID string)
func (*SignalsReceiver) Verify ¶
func (r *SignalsReceiver) Verify(state string) error
Verify asks the transmitter for a verification event.
type Transaction ¶
type Transaction struct {
State string
Nonce string
CodeVerifier string
RedirectURI string
URL string
MaxAge *float64
ExpectedSub string
// ACRValues are the acr_values requested, space-separated. Complete
// requires the ID token's acr to be one of them.
ACRValues string
}
Transaction is the sign-in kept until the browser returns.