Documentation
¶
Overview ¶
Package rpuser is the audited claims -> local-user path for relying parties built on oidclient: it turns the verified claims of a completed login into a provisioned row in the host's own users table, and resolves that row again on each request. Hosts keep their schema and queries; this package owns the policy. See https://github.com/infodancer/oidclient/issues/10.
The policy, in full:
- Users are keyed strictly on (issuer, subject). Accounts are never matched or merged by email: an upstream that recycles or reassigns an email must not grant access to an existing local account.
- Mutable claims (email, email_verified) are synced at login only, on the existing row; a known subject never gets a second row. The display name is set at creation and never overwritten -- hosts may let users edit it.
- The per-request path (Middleware) is strictly read-only.
- Issuer comes from the host's client configuration, not a token claim: oidclient validates the token's iss against the configured issuer during verification, so the configured value is the authoritative string -- the same one authz grants are keyed on.
Authorization is out of scope by design: Identity carries no roles, and this package never reads role claims. Role resolution belongs to the host's authorization layer (see github.com/infodancer/authz).
Index ¶
- Variables
- func ClaimsFromContext(ctx context.Context) *oidclient.Claims
- func WithClaims(ctx context.Context, c *oidclient.Claims) context.Context
- func WithIdentity(ctx context.Context, id *Identity) context.Context
- func WithUser(ctx context.Context, u *User) context.Context
- type Authenticator
- type Identity
- type Middleware
- type Provisioner
- func (p *Provisioner) Provision(ctx context.Context, claims *oidclient.Claims) (User, bool, error)
- func (p *Provisioner) ProvisionAccessToken(ctx context.Context, t *oidclient.AccessToken) (User, bool, error)
- func (p *Provisioner) ProvisionIdentity(ctx context.Context, id Identity) (User, bool, error)
- type User
- type UserStore
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("oidclient/rpuser: user not found")
ErrNotFound is returned by UserStore.LookupBySubject when no user exists for an (issuer, subject) pair. Store implementations must map their driver's no-rows result to it.
Functions ¶
func ClaimsFromContext ¶
ClaimsFromContext returns the request's validated claims, or nil for an anonymous request.
func WithClaims ¶
WithClaims stores validated claims in the context. Exported for host tests; Middleware.Optional is the production writer.
func WithIdentity ¶
WithIdentity stores a resolved identity in the context. Exported for host tests; Middleware.Optional is the production writer.
Types ¶
type Authenticator ¶
Authenticator is the slice of *session.Manager the middleware needs: resolve a request's session to validated claims. The concrete manager satisfies it; tests substitute a fake.
type Identity ¶
type Identity struct {
Issuer string
Subject string
DisplayName string
Email string
EmailVerified bool
}
Identity is the normalized, verified result of a completed login. It mirrors authz.Identity: (Issuer, Subject) is the stable key; DisplayName and Email are presentation data. It deliberately carries no roles -- authorization is resolved by the host against its own grant store, never from claims.
func FromAccessToken ¶ added in v0.9.1
func FromAccessToken(issuer string, t *oidclient.AccessToken) Identity
FromAccessToken normalizes a verified access token into an Identity. It is the counterpart of FromClaims and applies the same display-name fallback.
issuer is the resource server's configured issuer URL, which oidclient has already validated the token's iss against during verification -- so the configured value is the authoritative string, not a claim read back out of the token.
func FromClaims ¶
FromClaims normalizes verified claims into an Identity. issuer is the client's configured issuer URL, which oidclient has already validated the token against. DisplayName falls back to the email address when the token carries no name claim -- the one place that policy lives.
func IdentityFromContext ¶
IdentityFromContext returns the authenticated identity, or nil for an anonymous request. This is what a host's authz identity source should read.
type Middleware ¶
type Middleware struct {
// Authenticator resolves the session cookie to claims; nil (sessions not
// configured) makes every request anonymous, mirroring the optional-auth
// contract.
Authenticator Authenticator
// Store resolves (issuer, subject) to the host's user row.
Store UserStore
// Issuer is the client's configured issuer URL, as in [Provisioner].
Issuer string
// Logf receives diagnostics for faults worth surfacing: a failed session
// resolution (other than the expected no-session case) and a live session
// whose subject has no user row. Nil disables logging.
Logf func(format string, args ...any)
}
Middleware is the per-request half of the policy: resolve the session to claims, the claims to the host's user row (read-only), and stash identity, user, and claims in the request context for downstream handlers -- including the host's authz identity source.
It is optional-auth by design: it never blocks a request. Anonymous, expired, and failed resolutions all proceed without context values, and route-level gating stays with the host (which knows whether a 401 should redirect, render, or JSON). Host-specific context enrichment (module user types, role resolution) composes as an ordinary wrapping middleware reading IdentityFromContext and UserFromContext.
func (*Middleware) Optional ¶
func (m *Middleware) Optional(next http.Handler) http.Handler
Optional wraps next with the per-request resolution described on Middleware. Renewal happens inside Authenticate: the session manager transparently spends the refresh token when the access token nears expiry, so a session lasts its absolute TTL rather than one access-token window.
type Provisioner ¶
type Provisioner struct {
Store UserStore
// Issuer is the client's configured issuer URL; it becomes Identity.Issuer
// and half of the storage key.
Issuer string
// PreCreate, when set, runs on a first login after the lookup misses and
// before Create. Returning handled=true short-circuits creation with the
// returned user, reported as not-created -- the hook for host policies like
// osg's seed-account adoption, where a placeholder row should be claimed
// instead of a fresh one inserted. An error aborts the login.
PreCreate func(ctx context.Context, id Identity) (User, bool, error)
// Logf receives diagnostics for best-effort failures (a claim sync that did
// not stick). Nil disables logging.
Logf func(format string, args ...any)
}
Provisioner runs the login-time half of the policy: look the subject up, sync mutable claims on a hit, create on a miss.
func (*Provisioner) Provision ¶
Provision returns the local user for a completed login's claims, creating one on first login. created is true only for the fresh-Create path -- returning users and PreCreate adoptions report false -- so callers can route first-timers to onboarding.
On a hit, changed mutable claims are synced best-effort: a failed sync is logged and the stored values returned, rather than failing the login.
func (*Provisioner) ProvisionAccessToken ¶ added in v0.9.1
func (p *Provisioner) ProvisionAccessToken(ctx context.Context, t *oidclient.AccessToken) (User, bool, error)
ProvisionAccessToken returns the local user for a verified access token, creating one on first sight of its subject. It is Provisioner.Provision for the resource-server path.
func (*Provisioner) ProvisionIdentity ¶ added in v0.9.1
ProvisionIdentity is Provisioner.Provision over an already-normalized identity. It exists so the resource-server path -- which has an oidclient.AccessToken rather than claims -- runs the identical policy instead of a second copy of it. See accesstoken.go.
id must be fully normalized, with the display-name fallback already applied; FromClaims and FromAccessToken both do that.
type User ¶
User is the store's view of a provisioned local user: the host's stable key plus the two mutable fields the sync policy compares. Host is an optional slot for the host's own user representation -- a store may set it in LookupBySubject and read it back from UserFromContext, saving a second query per request; rpuser never inspects it.
func UserFromContext ¶
UserFromContext returns the resolved local user, or nil for an anonymous request. Hosts typically unwrap their own row from User.Host.
type UserStore ¶
type UserStore interface {
// LookupBySubject returns the user for (issuer, subject), or [ErrNotFound].
// It is called on every authenticated request and must not write.
LookupBySubject(ctx context.Context, issuer, subject string) (User, error)
// SyncEmail persists a changed email / email_verified pair on an existing
// row. Called at login only, when [Provisioner.Provision] detects a change.
SyncEmail(ctx context.Context, userKey, email string, verified bool) error
// Create inserts a row for a first login, keyed on (id.Issuer, id.Subject),
// and returns it. The identity is fully normalized (display-name fallback
// applied) before Create is called.
Create(ctx context.Context, id Identity) (User, error)
}
UserStore is implemented by the host over its own users table. The host owns the schema and the SQL; the methods are deliberately dumb so the provisioning policy stays in this package.
The backing table must key users on (issuer, subject) -- typically a composite unique constraint -- so the store cannot violate the never-match-by-email rule even under a racing double-create.