authorizationdetails

package
v0.70.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 that go into a signed token with their bytes unchanged.

Index

Constants

View Source
const IDJAGIssuance = "urn:ietf:params:oauth:token-type:id-jag"

IDJAGIssuance is the key a Type lists in GrantTypes to be accepted on an RFC 8693 token exchange that mints an ID-JAG, and so to be signed into the ID-JAG's authorization_details claim. It is not a grant type: its value is the ID-JAG token-type URN, the requested_token_type of such a request, which no grant type can equal. Listing the token-exchange grant type alone does not opt a type in, so a type written for exchanged access tokens is never signed into an ID-JAG that another authorization server trusts unless its author says so. Use Registry.ParseIDJAGIssuance for such a request.

Variables

View Source
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

type Error struct {
	Code   ErrorCode
	Reason string
}

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.

func (Error) Error

func (e Error) Error() string

Error implements error.

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. On a token
	// exchange that mints an ID-JAG for a client with an actor profile, the
	// actor token is validated only after the hooks run, so Actor is empty
	// and ActorTokenPresent is true.
	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

func (p *Pending) Grant(ctx context.Context, req Request) (json.RawMessage, error)

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

func (p *Pending) GrantSubset(ctx context.Context, req Request) (Subset, error)

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 entries that go into a signed token unchanged: a hook may only keep or drop whole entries, and the result carries the parsed bytes of each kept entry, never the hook's. ToolHive's authorization server uses it on both sides of an ID-JAG: when it mints one, for the entries the client asked to have signed, and when it redeems one, for the entries the issuer signed. 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

func New(types map[string]Type) (*Registry, error)

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

func (r *Registry) Parse(raw, grantType string) (*Pending, error)

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.

func (*Registry) ParseIDJAGIssuance added in v0.70.0

func (r *Registry) ParseIDJAGIssuance(raw string) (*Pending, error)

ParseIDJAGIssuance is Parse for an RFC 8693 token exchange that mints an ID-JAG: every type must list IDJAGIssuance, and a type that lists only the token-exchange grant type is ErrUnknownType. The hooks still see the token-exchange grant type in Request.GrantType.

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.

On a token exchange that mints an ID-JAG, the subject_token is the opaque refresh token of an exchange grant, so Subject describes the grant: Issuer is this server, Subject is the user the grant acts for (the ID-JAG's sub), ClientID is the client that holds the grant, IssuedAt is the grant's creation, Expiry is the earliest bound the grant still mints under, and the other fields are empty.

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, and may list
	// IDJAGIssuance. 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.
	//
	// Listing the token-exchange grant type
	// (urn:ietf:params:oauth:grant-type:token-exchange) opts the type into a
	// token exchange for an access token, which only echoes the granted
	// entries. Listing IDJAGIssuance opts it into a token exchange for an
	// ID-JAG, which signs the entries the hook keeps into the ID-JAG under
	// Pending.GrantSubset's rules; without it, an entry of the type on such a
	// request is refused as an unknown type. The two are independent.
	GrantTypes []string

	// Grant decides what to grant for all of this type's entries in a request.
	// It is called after the other checks passed, with every entry of the type
	// in request order: once per request, except on a token exchange for an
	// ID-JAG, which decides in both of the token endpoint's handler phases and
	// so calls it twice. What it may return
	// depends on how the server asks: see Pending.Grant (token exchange) and
	// Pending.GrantSubset (a token exchange that mints an ID-JAG, and 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, and it may be called
	// again for the same request. 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL