Infra decoded real tokens from both clients, and two of the three facts changed this code. A machine token carries NO sub at all: it names its caller by client_id, and the identity is that client. A person's token carries sub, the provider's stable identifier for the person. The subject is now derived explicitly -- sub when present, else client_id -- and a verified token asserting neither is refused rather than accepted as an anonymous caller, because accepting it would let any token the provider ever signed act as whoever the binding happened to hold. A machine identity is therefore per client registration, one identity per client, which is what makes per-agent identity a configuration decision rather than a code change. Scopes are read from either claim shape. The standard is a space-delimited scope string and this provider also emits an scp array; reading only one shape yields no scopes at all, which is worse than reading none deliberately. The audience-at-authorisation-endpoint trap is pinned by a test: a token minted without the requested audience arrives with aud empty, and the audience check refuses it. That refusal is correct and the fix is the request the human flow builds, not a weaker check here -- a test now fails if anyone weakens it. The PRD records both as constraints: a person's client and an agent's client are registered separately because a shared registration collapses the distinction the attribution rests on, and a person's token requests the audience at the authorisation endpoint as well as the token endpoint. Verified: task check exit 0; internal/infrastructure/oidc passes, including subject derivation for both token shapes and every refusal.
108 lines
4.0 KiB
Go
108 lines
4.0 KiB
Go
// Package oidc verifies identity-provider tokens. It is a resource server: it
|
|
// validates a token the provider issued and never issues one, holds no client
|
|
// secret, and mints no session.
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
gooidc "github.com/coreos/go-oidc/v3/oidc"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
)
|
|
|
|
// Config identifies the provider and the audience archie accepts tokens for.
|
|
// Audience is required rather than optional: without it a token the provider
|
|
// minted for a different service would authenticate here, which is the
|
|
// confused-deputy failure this boundary exists to prevent.
|
|
type Config struct {
|
|
Issuer string
|
|
Audience string
|
|
}
|
|
|
|
// Verifier validates tokens against the provider's published signing keys.
|
|
type Verifier struct {
|
|
verifier *gooidc.IDTokenVerifier
|
|
}
|
|
|
|
// New discovers the provider and builds a verifier from its published keys.
|
|
// Discovery happens once, at construction: an unreachable provider is a
|
|
// startup failure the operator sees, not a request-time surprise.
|
|
func New(ctx context.Context, cfg Config) (*Verifier, error) {
|
|
issuer := strings.TrimSpace(cfg.Issuer)
|
|
audience := strings.TrimSpace(cfg.Audience)
|
|
if issuer == "" {
|
|
return nil, fmt.Errorf("oidc: issuer is required")
|
|
}
|
|
if audience == "" {
|
|
return nil, fmt.Errorf("oidc: audience is required")
|
|
}
|
|
provider, err := gooidc.NewProvider(ctx, issuer)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("oidc: discover %q: %w", issuer, err)
|
|
}
|
|
return &Verifier{verifier: provider.Verifier(&gooidc.Config{ClientID: audience})}, nil
|
|
}
|
|
|
|
// Verify checks the token's signature, issuer, audience and expiry against the
|
|
// provider's keys, and returns what it proved. Every check is the library's:
|
|
// signature comparison, claim validation and key rotation are not archie's code.
|
|
func (v *Verifier) Verify(ctx context.Context, rawToken string) (identity.Credential, error) {
|
|
if v == nil || v.verifier == nil {
|
|
return identity.Credential{}, fmt.Errorf("oidc: no verifier is configured")
|
|
}
|
|
token, err := v.verifier.Verify(ctx, strings.TrimSpace(rawToken))
|
|
if err != nil {
|
|
return identity.Credential{}, fmt.Errorf("oidc: verify: %w", err)
|
|
}
|
|
// Claims are read after verification and are the only source of the
|
|
// identity: an unverified token selects nothing.
|
|
var claims struct {
|
|
Subject string `json:"sub"`
|
|
ClientID string `json:"client_id"`
|
|
Scope string `json:"scope"`
|
|
Scopes []string `json:"scp"`
|
|
}
|
|
_ = token.Claims(&claims)
|
|
subject, err := subjectFor(token.Issuer, claims.Subject, claims.ClientID)
|
|
if err != nil {
|
|
return identity.Credential{}, err
|
|
}
|
|
return identity.Credential{
|
|
Subject: subject,
|
|
Scopes: scopesFor(claims.Scope, claims.Scopes),
|
|
Expires: token.Expiry,
|
|
}, nil
|
|
}
|
|
|
|
// subjectFor derives the caller a token asserts.
|
|
//
|
|
// A person's token carries sub, the provider's stable identifier for that person.
|
|
// A machine's token carries no sub at all and identifies its caller by client_id,
|
|
// so a machine identity is keyed on the client and one identity exists per client
|
|
// registration. A token asserting neither identifies nobody and is refused rather
|
|
// than being accepted as an anonymous caller.
|
|
func subjectFor(issuer, subject, clientID string) (identity.Subject, error) {
|
|
switch {
|
|
case strings.TrimSpace(subject) != "":
|
|
return identity.Subject{Issuer: issuer, Subject: strings.TrimSpace(subject)}, nil
|
|
case strings.TrimSpace(clientID) != "":
|
|
return identity.Subject{Issuer: issuer, Subject: strings.TrimSpace(clientID)}, nil
|
|
default:
|
|
return identity.Subject{}, fmt.Errorf("oidc: token asserts neither sub nor client_id")
|
|
}
|
|
}
|
|
|
|
// scopesFor reads the granted scopes from either claim shape. Providers differ:
|
|
// the standard is a space-delimited scope string, and this provider also emits an
|
|
// scp array. Reading only one shape silently yields no scopes at all, which is
|
|
// worse than reading none deliberately.
|
|
func scopesFor(scope string, scopes []string) []string {
|
|
if len(scopes) > 0 {
|
|
return scopes
|
|
}
|
|
return strings.Fields(scope)
|
|
}
|