Documentation
¶
Overview ¶
Package oidc verifies an OpenID Connect token into an identity.
v, err := oidc.New(ctx, oidc.Config{
Issuer: "https://login.example.org",
Audience: "fileshare",
})
tok, err := v.Verify(ctx, raw) // from an Authorization: Bearer header
fmt.Println(tok.Subject, tok.Username, tok.Groups)
A token is a signed statement by somebody else about who is asking. Almost all of the work is refusing the ones that are not, so the refusals are the package:
⛔ alg: none. A token that says it is unsigned is not a token; it is a JSON object somebody typed.
⛔ An HMAC algorithm against a public key. HS256 takes a shared secret, and a verifier that treats the issuer's PUBLIC key as that secret can be handed a token anybody forged with it -- the key is public. Only the signature algorithms the issuer actually published are accepted, and HMAC is never one of them here.
⛔ An issuer that merely starts with the right string. "iss" is compared whole: https://login.example.org.evil.test starts with the right thing.
⛔ A missing audience. A token minted for another service is a valid token; it is simply not addressed to this one, and a server that skips the check accepts every token the issuer ever signed.
⛔ A key fetched over cleartext HTTP, except from loopback. The JWKS is what decides every signature; over a link somebody can rewrite, so is everything else.
What it is not ¶
There is no login flow here: no redirect, no code exchange, no client secret, no cookies. This is the resource-server half -- something arrives with a token, and this says who that is. The half that gets people a token belongs to whatever is talking to them, and putting both in one package makes the security question twice as large for everybody who needed one of them.
Index ¶
- Constants
- Variables
- func ASCIILower(s string) string
- func ClaimValues(t *Token, name string) []string
- func EqualASCIIFold(a, b string) bool
- func FoldsCase(claim string) bool
- func Normalize(claim, v string) string
- func PeekIssuer(raw string) (string, bool)
- func ScopedName(name string, allowed []string, defaultScope string) (string, error)
- type Accept
- type Config
- type Refusal
- type Set
- type Token
- func (t *Token) Audience() []string
- func (t *Token) Claim(name string, v any) error
- func (t *Token) Email() string
- func (t *Token) EmailVerified() bool
- func (t *Token) Expiry() (time.Time, bool)
- func (t *Token) Groups() []string
- func (t *Token) Has(name string) bool
- func (t *Token) IssuedAt() (time.Time, bool)
- func (t *Token) Issuer() string
- func (t *Token) NotBefore() (time.Time, bool)
- func (t *Token) ScopedUsername(allowed []string, defaultScope string) (string, error)
- func (t *Token) Subject() string
- func (t *Token) Username() string
- type Verifier
Constants ¶
const MaxTokenSize = 16 << 10
MaxTokenSize bounds the compact token a Set reads the issuer of: a token longer than this is refused before any decoding.
Variables ¶
var ErrBadName = errors.New("oidc: the name is not a well-formed local@scope")
ErrBadName is a name that is not a well-formed local@scope.
var ErrNoScope = errors.New("oidc: the name carries no scope (want local@scope)")
ErrNoScope is a name that carries no scope and for which no default scope was given: it is not an identity.
var ErrScopeNotAllowed = errors.New("oidc: the name's scope is not one this issuer may assert")
ErrScopeNotAllowed is a name whose scope the issuer may not assert.
var ErrUnknownIssuer = errors.New("oidc: the token's issuer is not one this service accepts")
ErrUnknownIssuer is a token whose "iss" no verifier of the set is configured for (or that carries none readable).
Functions ¶
func ASCIILower ¶ added in v0.7.0
ASCIILower folds A-Z only: the fold the schema's caseIgnoreMatch is read as for the names this package returns, and the one every consumer of those names must use. strings.ToLower and strings.EqualFold fold Unicode, which equates the Kelvin sign U+212A with "k": "Kate" and "kate" are two accounts at an issuer, and one to a consumer that folds Unicode.
func ClaimValues ¶ added in v0.5.0
ClaimValues reads a claim as strings: a string, or a list of strings. A claim that is absent, or has any other shape (a number, an object, a list with a non-string in it), has no values. The names "" and "groups" are the token's groups claim (Token.Groups).
func EqualASCIIFold ¶ added in v0.7.0
EqualASCIIFold reports whether a and b are the same name under ASCIILower.
func Normalize ¶ added in v0.5.0
Normalize is the form values of claim are compared in: ASCII-lower-cased (and without one trailing dot) for the claims that fold, as they are otherwise.
func PeekIssuer ¶ added in v0.5.0
PeekIssuer reads "iss" from the payload of a compact JWT without verifying anything. Use it to choose a key set, never to trust the token. It reports false for a token over MaxTokenSize, one that is not three dot-separated segments, a payload that is not base64url JSON, or an "iss" that is not a non-empty string.
func ScopedName ¶ added in v0.6.0
ScopedName returns the person's name as "local@scope", ASCII-lower-cased, after checking the scope against allowed (compared as Normalize does for the schac_home_organization claim: ASCII case folded, one trailing dot dropped).
A name without "@" takes defaultScope when it is not empty (the scope of an institution whose accounts predate scoped names: "alice" at "polytechnique.edu" is "alice@polytechnique.edu"), and is ErrNoScope otherwise. A defaultScope must itself be allowed. More than one "@" is ErrBadName: the rule is exactly one local@scope, so that a consumer that splits at the first "@" and one that splits at the last read the same scope. An empty local part or scope, a control character, a space, or a name over the bounds is ErrBadName; a scope that is not in allowed is ErrScopeNotAllowed. An empty allowed list allows no scope.
Types ¶
type Accept ¶ added in v0.5.0
An Accept is "Claim holds one of Values". Several Accepts all have to hold.
func (Accept) Holds ¶ added in v0.5.0
Holds reports whether the token's claim holds one of a.Values. An empty Values accepts nobody.
func (Accept) HoldsValues ¶ added in v0.5.0
HoldsValues reports whether one of got is one of a.Values.
type Config ¶
type Config struct {
// Issuer is the "iss" a token must carry, exactly. It is also where
// discovery starts, unless JWKSURL is given.
Issuer string
// Audience is the "aud" a token must be addressed to -- this service's
// client id at the provider.
Audience string
// JWKSURL skips discovery, for a provider that does not publish
// /.well-known/openid-configuration or one behind something that does not
// forward it.
JWKSURL string
// UsernameClaim is which claim names the person. Default
// "preferred_username", falling back to "email" when email_verified is
// true, and then "sub" -- see [Token.Username], where the fallback is
// explained.
UsernameClaim string
// GroupsClaim is which claim carries their groups. Default "groups".
GroupsClaim string
// Type is the "typ" a token's header must carry, when it is set: "at+jwt"
// for a resource server taking access tokens (RFC 9068 4: it MUST check
// that typ is at+jwt), or the media type of whatever else an issuer
// signs. Compared without case and without an "application/" prefix (RFC
// 7515 4.1.9). Empty takes no typ, "JWT" or "at+jwt", as an ID token may
// carry any of them.
//
// ⛔ Without it a resource server takes an ID token for an access token
// whenever the audiences agree -- and an ID token is addressed to the
// client, which is exactly the audience a resource server is often
// configured with.
Type string
// ClockSkew is how much a clock may differ before a token is early or
// late. Default one minute; a provider and a server that disagree by more
// than that have a problem worth fixing rather than tolerating.
ClockSkew time.Duration
// MinRefresh is how long to wait before fetching the key set again after
// a key was not found. Default one minute: keys rotate, and a token with
// an unknown kid must not be a way to make this server hammer the
// issuer.
MinRefresh time.Duration
// Client is the HTTP client for discovery and the key set. A caller with
// a proxy, a private CA or a timeout of its own passes one.
Client *http.Client
// Now overrides the clock, for tests.
Now func() time.Time
}
Config is what a verifier needs to know. Issuer and Audience are required: a verifier without them is one that accepts every token anybody ever signed.
type Refusal ¶ added in v0.5.0
type Refusal struct {
Claim string
// Value is the first value the token stated, cleaned to printable ASCII
// and bounded, so it can be shown back; "" when the token stated none.
Value string
}
A Refusal says which Accept a token failed.
type Set ¶ added in v0.5.0
type Set struct {
// contains filtered or unexported fields
}
A Set verifies tokens from several issuers, each with its own audience and key set.
The token is routed by its UNVERIFIED "iss" to the one verifier configured for exactly that issuer, and verified there: signature against that issuer's keys, "iss" again by exact comparison, audience, times. Reading "iss" first chooses which key set to use and nothing else, so it grants nothing: a forged token fails the verification that follows. What it must never do is try every key set -- one issuer's key could then vouch for another issuer's name -- or fetch anything for an issuer nobody configured: an unknown issuer costs no network access at all.
With a single verifier there is nothing to route: the token goes straight to it and its own exact "iss" check refuses a stranger, at no extra cost.
func NewSet ¶ added in v0.5.0
NewSet is the set of verifiers. Two verifiers for one issuer are refused: a token could not be routed to either of them.
func (*Set) For ¶ added in v0.5.0
For returns the verifier a token is routed to, or ErrUnknownIssuer.
type Token ¶
type Token struct {
// contains filtered or unexported fields
}
A Token is a verified token: what the issuer said about who is asking.
Every method reads the claims that arrived. Nothing here re-checks anything -- a Token exists only because Verifier.Verify returned it -- and nothing here reaches the network.
func (*Token) Audience ¶
Audience is every "aud" in the token. It is a list in the specification and a string in most tokens, and both spellings mean the same thing.
func (*Token) Claim ¶
Claim reads any other claim into v, which is how a deployment gets at something this package has no opinion about.
func (*Token) Email ¶
Email is "email", which a provider may or may not send and may or may not have verified -- see Token.EmailVerified.
func (*Token) EmailVerified ¶
EmailVerified is "email_verified".
⛔ An unverified email is a string the person typed. Matching people by one lets somebody claim to be anybody whose address they know, at any provider that does not check.
func (*Token) Groups ¶
Groups is the groups claim, which providers spell differently and some do not send at all.
func (*Token) Has ¶
Has reports whether a claim is present at all, which is different from present and empty.
func (*Token) ScopedUsername ¶ added in v0.6.0
ScopedUsername is ScopedName of Token.Username: configure the claim that carries the scoped name (UsernameClaim "eduperson_principal_name") or take the default fallbacks and a defaultScope for unscoped ones.
func (*Token) Subject ¶
Subject is "sub": the issuer's own identifier for the person, stable across name changes and the only one that is promised to be.
func (*Token) Username ¶
Username is the name to call this person, from the configured claim.
The default order is preferred_username, then email -- only when email_verified is true -- then sub. It is a fallback rather than a requirement because providers differ about which they send, and it ends at sub, which every token has and which is the one the issuer promises is stable (OIDC Core §5.7).
⛔ An email the issuer did not verify is skipped, not used: it is a string the person typed (OIDC Core §5.1), and a correctly signed token carrying somebody else's address would otherwise be admitted under that person's name. Before v0.2.0 an unverified email was used.
A deployment that maps people by name should say which claim it means rather than take what arrives. A configured UsernameClaim is read as named, with no fallback and no email_verified check: naming "email" there is a decision about that issuer, and this does not second-guess it.
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
A Verifier checks tokens from one issuer for one audience.
It is safe for concurrent use, and it holds the key set: build one at startup and keep it, rather than one per request.
func New ¶
New reads the provider's configuration and prepares to verify.
Discovery happens HERE rather than at the first token, for the same reason a database is pinged at startup: a provider that is not answering is a server that cannot authenticate anybody, and it should say so before it listens.
func (*Verifier) Issuer ¶ added in v0.5.0
Issuer is the "iss" this verifier requires, exactly (trailing slashes removed, as discovery does).
func (*Verifier) Verify ¶
Verify checks a token and says who it is about.
The error says which check refused the token. That detail is for the server's own log -- this package logs nothing -- and the server answers the client with one refusal whatever the check: a client that sent a token it should not have is not owed an explanation of which check caught it.