Documentation
¶
Overview ¶
Package authorizationdetails validates the RFC 9396 authorization_details parameter of a token request and asks per-type hooks what to grant.
The package only parses and delegates. It does not persist the granted details, does not put them in tokens, and does not convey them to resource servers. Register only types whose enforcement the deployment performs itself.
Usage has two phases. Registry.Parse runs early and cheaply, before any expensive checks, and rejects malformed input. Pending.Grant or Pending.GrantSubset runs late, after the request is otherwise accepted, and calls the hooks: Grant when the hooks decide the granted entries, and GrantSubset when they may only keep or drop entries someone else signed.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalid reports that the authorization_details parameter is malformed: // bad JSON, over a size or depth limit, duplicate member names, an entry // that is not an object, or a missing, unknown or wrong-grant "type". // Callers should answer with a generic invalid_authorization_details error // and must not echo request data. ErrInvalid = errors.New("invalid authorization_details") // ErrUnknownType reports that every entry is well formed but at least one // has a "type" that is not registered for the grant type. It wraps // ErrInvalid, so errors.Is(err, ErrInvalid) still holds; a caller that // tells the two apart checks ErrUnknownType first. ErrUnknownType = fmt.Errorf("%w: unknown type", ErrInvalid) )
Functions ¶
This section is empty.
Types ¶
type Error ¶
Error is a client-visible rejection a Grant hook may return. Reason is shown to the client, so it must match [a-z0-9_]{1,64} and must not carry request data. Any other error a hook returns is treated as an internal failure.
type ErrorCode ¶
type ErrorCode string
ErrorCode is the OAuth error code a hook may return to the client.
const ( // CodeInvalidAuthorizationDetails is the RFC 9396 invalid_authorization_details error. CodeInvalidAuthorizationDetails ErrorCode = "invalid_authorization_details" // CodeInvalidRequest is the RFC 6749 invalid_request error. It is for // problems outside the authorization details themselves, such as a missing // or unacceptable related parameter. Problems in the details' content // (unknown fields, wrong types, invalid values, missing required fields) // must use CodeInvalidAuthorizationDetails, per RFC 9396 section 5. CodeInvalidRequest ErrorCode = "invalid_request" )
type ExchangeContext ¶ added in v0.69.1
type ExchangeContext struct {
// Actor is the resolved actor. Without an actor_token it equals the
// client ID, or the verified SPIFFE ID for a SPIFFE client.
Actor string
// ActorTokenPresent reports whether the request carried an actor_token.
ActorTokenPresent bool
// RequestedTokenType is the requested_token_type of the request.
RequestedTokenType string
// RequestedScopes are the scopes the client asked for.
RequestedScopes []string
}
ExchangeContext is the part of a Request only RFC 8693 token exchange has.
type Pending ¶
type Pending struct {
// contains filtered or unexported fields
}
Pending is a parsed, not yet granted authorization_details value. It is single-use: call Grant or GrantSubset at most once.
func (*Pending) Grant ¶
Grant calls each type's hook once, in the order the types first appear in the request, and returns the granted entries as a JSON array: types in that order, each type's entries in the order its hook returned them ("[]" when the request held no entries). The package fills req.Entries and req.GrantType.
A hook must return at least one entry: a type whose hook returns none fails the whole call as a hook defect, and no later hook runs. Every entry is checked as strictly as the request: valid UTF-8, within the size limit, no duplicate member names, and an object of the hook's own type. The combined array must also fit the size limit. The output is built with encoding/json, so entries are compacted and HTML-escaped (<, > and & become \u003c, \u003e and \u0026): equal to the hook's output as JSON, not byte-identical.
Errors: Error is a hook rejection to pass to the client. A plain error means the package rejected hook output, and any other error is an untyped hook failure; both are internal failures whose text must not reach the client.
func (*Pending) GrantSubset ¶ added in v0.69.1
GrantSubset calls each type's hook once, in the order the types first appear in the parsed value, and keeps the entries the hooks returned. It is for a value whose entries a third party signed: a hook may only keep or drop whole entries, and the result carries the parsed bytes of each kept entry, never the hook's. The package fills req.Entries and req.GrantType; each hook gets its own copy of its entries.
Every entry a hook returns must equal a distinct one of its type's entries as a JSON value, with object members in any order and numbers compared as written, so 1 and 1.0 differ. A hook that keeps an entry returns it exactly as received in Request.Entries; re-encoding it can change a number's spelling. A hook that returns no entry drops its type, which is reported in Subset.Dropped and is not an error. Whether a result with no entry left is acceptable is the caller's decision.
Errors are as for Grant: Error is a hook rejection to pass to the client, and any other error, including an entry that matches none of the type's entries, is an internal failure.
type RedemptionContext ¶ added in v0.69.1
type RedemptionContext struct {
// ResolvedUser is the local user the redemption resolved the ID-JAG's
// subject to. It is set for an issuer with subject_mode resolve and empty
// for a namespaced one, whose token subject is <issuer>#<sub>.
ResolvedUser string
}
RedemptionContext is the part of a Request only ID-JAG redemption has.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds the registered types. It is immutable and safe for concurrent use.
func New ¶
New copies and validates the registered types, keyed by exact, case-sensitive type name. It rejects empty or whitespace names, a nil Grant, and an empty or blank GrantTypes list. A registry with no types is valid; it accepts only the empty array.
func (*Registry) Parse ¶
Parse checks the raw authorization_details value for the given grant type. The value must be at most 64 KiB of valid UTF-8 holding one JSON array of objects, nested at most 64 containers deep, in which no object repeats a member name (also not differing only in letter case; that rule is ToolHive policy, not RFC 9396). Every entry needs a non-empty string "type" that is registered for grantType. The empty array "[]" is valid.
Any failure returns ErrInvalid. When every entry is a well-formed object with a non-empty string "type" but some type is not registered for grantType, the error is ErrUnknownType, which wraps ErrInvalid.
type Request ¶
type Request struct {
// Entries holds this type's entries in request order.
Entries []json.RawMessage
// GrantType is the grant type of the token request.
GrantType string
// ClientID is the authenticated client.
ClientID string
// GrantedAudience is the audience the server has already decided to grant:
// on token exchange the audience of the token being issued, and on ID-JAG
// redemption the one resource the issued token is for.
GrantedAudience []string
// Subject describes the validated token the request is about: the
// subject_token on token exchange, and the ID-JAG on ID-JAG redemption.
Subject Subject
// Exchange holds what only token exchange has. It is set on token exchange
// and nil otherwise.
Exchange *ExchangeContext
// Redemption holds what only ID-JAG redemption has. It is set on ID-JAG
// redemption, which rides the JWT-bearer grant, and nil otherwise.
Redemption *RedemptionContext
}
Request is the context a Grant hook receives. The caller supplies every field except Entries and GrantType, which the package fills.
The fields every grant fills are at the top level. What only one grant has is in its own part: Exchange on token exchange and Redemption on ID-JAG redemption, each nil on the other grant. A hook reads a grant's fields only after checking that its part is not nil, so an empty field is never mistaken for one that does not apply.
All hooks of one request share the same Request values: the slices, the parts, Subject and Subject.Claims are not copied per hook, so a hook must not modify them. Only Entries is specific to the hook's type.
type Result ¶
type Result struct {
// Entries are the granted entries. For Pending.Grant the list must be
// non-empty and every entry must be a JSON object whose "type" is the
// hook's own type. For Pending.GrantSubset every entry must be one of
// Request.Entries returned exactly as received, and an empty list drops
// all of the type's entries.
Entries []json.RawMessage
}
Result is what a Grant hook decided.
type Subject ¶
type Subject struct {
Issuer string
// Subject is the token's sub claim, on every grant. It is never replaced by
// a resolved local user; see RedemptionContext.ResolvedUser.
Subject string
ClientID string
JWTID string
Audience []string
IssuedAt time.Time
Expiry time.Time
External bool // validated through a trusted issuer
// Claims holds the verified claims, passed to hooks as given, except
// authorization_details: a hook reads the entries from Request.Entries,
// which keep every digit of a number.
Claims map[string]any
}
Subject describes a validated token: the subject_token on token exchange, the ID-JAG on ID-JAG redemption.
type Subset ¶ added in v0.69.1
type Subset struct {
// Entries is a JSON array of the kept entries, each with the exact bytes
// it had in the parsed value, nil when no hook kept any entry. Types are
// in the order they first appear in the parsed value, and each type's
// entries in their parsed order.
Entries json.RawMessage
// Dropped lists, in the same type order, the types whose hook kept none
// of their entries.
Dropped []string
}
Subset is what GrantSubset kept of the parsed entries.
type Type ¶
type Type struct {
// GrantTypes lists the grant types this type is accepted on. It is required
// and must be non-empty. An entry of this type on any other grant type is
// rejected as invalid.
//
// On ToolHive's authorization server, listing the JWT-bearer grant type
// (urn:ietf:params:oauth:grant-type:jwt-bearer) opts the type into ID-JAG
// redemption only: the server processes the authorization_details claim
// of an ID-JAG and refuses a plain RFC 7523 assertion that carries one.
GrantTypes []string
// Grant decides what to grant for all of this type's entries in a request.
// It is called at most once per request, after the other checks passed,
// with every entry of the type in request order. What it may return
// depends on how the server asks: see Pending.Grant (token exchange) and
// Pending.GrantSubset (ID-JAG redemption).
//
// Grant is decide-only: it may read state but must not commit it, because
// token issuance can still fail after it returns. On ID-JAG redemption the
// ID-JAG is consumed only after the hooks ran, so a hook can run again for
// the same ID-JAG when a redemption of it is later refused as a replay.
// Returning Error rejects the request with that code and reason; any other
// error is an internal failure.
Grant func(context.Context, Request) (Result, error)
}
Type registers one authorization-details type.