budgets

package
v0.0.0-...-1bd2d46 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package budgets implements the AAuth Budgets protocol companion.

Index

Constants

View Source
const MaximumAmount int64 = 999_999_999_999_999

MaximumAmount is the 15-digit limit shared by Budget JSON values and RFC 9651 Integer members.

Variables

This section is empty.

Functions

func FormatAuthTokenRequirement

func FormatAuthTokenRequirement(value aauth.AuthTokenRequirement, reason Reason) (string, error)

FormatAuthTokenRequirement formats the Core auth-token requirement with one typed Budget reason parameter.

func FormatFeedback

func FormatFeedback(feedback Feedback) (string, error)

FormatFeedback emits the canonical RFC 9651 Dictionary form.

func IsTokenClaimError

func IsTokenClaimError(err error) bool

IsTokenClaimError reports whether Core token verification succeeded but the Budget companion claims were malformed. Token endpoints use this distinction to return the Budgets protocol's registered invalid_budget error.

func MintAuthToken

func MintAuthToken(key jose.SigningKey, claims aauth.AuthTokenClaims, grant Value) (string, error)

MintAuthToken adds one validated Budget grant to Core Auth-token claims.

func MintResourceToken

func MintResourceToken(
	key jose.SigningKey,
	claims aauth.ResourceTokenClaims,
	offer Value,
	consumed []ConsumptionRecord,
) (string, error)

MintResourceToken adds one validated Budget offer and optional recent consumption records to Core Resource-token claims.

func TokenCarriesClaims

func TokenCarriesClaims(compact string) (bool, error)

TokenCarriesClaims reports whether an unverified compact token names a Budget claim, including partial and case-variant forms that must route to strict Budget verification.

func TokenClaimError

func TokenClaimError(err error) error

TokenClaimError marks an error found after Core token verification while consuming Budget companion claims. Endpoints use it to return the registered invalid_budget response for composed token profiles too.

func ValidateDeclaredValue

func ValidateDeclaredValue(declarations []UnitDeclaration, value Value) error

ValidateDeclaredValue checks that a signed offer or grant uses one declared denomination and does not exceed its advertised maximum when present.

func ValidateFeedback

func ValidateFeedback(grant Value, feedback Feedback) error

ValidateFeedback checks unsigned informational feedback against the signed Auth-token budget. An omitted unit pair remains valid because the pair is optional; a present pair must match exactly.

func ValidateFeedbackUnit

func ValidateFeedbackUnit(unit Unit) error

ValidateFeedbackUnit checks that a JSON Budget unit is representable as an RFC 9651 String in AAuth-Budget. The token object permits arbitrary UTF-8, but a metering profile that emits the response field must establish this narrower carrier before any charge.

func ValidateNarrowing

func ValidateNarrowing(offered, granted Value) error

ValidateNarrowing requires the same denomination and an amount no greater than the resource's offer.

func ValidateUnitDeclarations

func ValidateUnitDeclarations(declarations []UnitDeclaration) error

ValidateUnitDeclarations requires valid entries and one scale per unit identifier.

func VerifyResourceToken

func VerifyResourceToken(
	ctx context.Context,
	compact string,
	resolver signaturekey.KeyResolver,
	clock aauth.Clock,
) (aauth.ResourceTokenClaims, Value, []ConsumptionRecord, error)

VerifyResourceToken verifies Core trust and consumes the complete Budget companion claim set.

Types

type ConsumptionRecord

type ConsumptionRecord struct {
	// contains filtered or unexported fields
}

ConsumptionRecord reports metered consumption against one prior Auth token.

func NewConsumptionRecord

func NewConsumptionRecord(jti string, consumed int64) (ConsumptionRecord, error)

NewConsumptionRecord validates one prior Auth-token consumption record.

func (ConsumptionRecord) Consumed

func (record ConsumptionRecord) Consumed() int64

Consumed returns the amount metered against the prior Auth token.

func (ConsumptionRecord) JTI

func (record ConsumptionRecord) JTI() string

JTI returns the prior Auth token identifier.

func (ConsumptionRecord) MarshalJSON

func (record ConsumptionRecord) MarshalJSON() ([]byte, error)

MarshalJSON emits the exact Budget consumption-record object.

func (*ConsumptionRecord) UnmarshalJSON

func (record *ConsumptionRecord) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts only the exact Budget consumption-record object.

type Feedback

type Feedback struct {
	// contains filtered or unexported fields
}

Feedback is the parsed AAuth-Budget response field for the fixed-price success and refusal paths.

func NewRefusalFeedback

func NewRefusalFeedback(grant Value, remaining int64, required *int64) (Feedback, error)

NewRefusalFeedback constructs feedback for an exhausted or insufficient fixed-price request. Required is omitted when the Resource does not state a maximum cost for the refused request.

func NewSuccessFeedback

func NewSuccessFeedback(grant Value, cost, remaining int64) (Feedback, error)

NewSuccessFeedback constructs feedback for a completed fixed-price request.

func ParseFeedback

func ParseFeedback(values []string) (Feedback, error)

ParseFeedback parses one or more AAuth-Budget field lines as an RFC 9651 Dictionary. Unknown members are ignored.

func (Feedback) Cost

func (feedback Feedback) Cost() (int64, bool)

Cost returns the cost of this request when reported.

func (Feedback) Remaining

func (feedback Feedback) Remaining() int64

Remaining returns the balance reported for the presented Auth token.

func (Feedback) Required

func (feedback Feedback) Required() (int64, bool)

Required returns the computed maximum cost of a refused request when reported.

func (Feedback) Unit

func (feedback Feedback) Unit() (Unit, bool)

Unit returns the informational denomination pair when present.

type Reason

type Reason string

Reason is one Budget-specific reason on an auth-token requirement.

const (
	ReasonBudgetExhausted    Reason = "budget-exhausted"
	ReasonInsufficientBudget Reason = "insufficient-budget"
)

func ParseAuthTokenRequirement

func ParseAuthTokenRequirement(values []string) (aauth.AuthTokenRequirement, Reason, error)

ParseAuthTokenRequirement parses the Core auth-token requirement and its required Budget reason parameter. Unrelated extension parameters remain ignored as required by the base extension model.

func ParseOptionalAuthTokenRequirement

func ParseOptionalAuthTokenRequirement(values []string) (aauth.AuthTokenRequirement, Reason, bool, error)

ParseOptionalAuthTokenRequirement parses the Core auth-token requirement and, when present, one typed Budget reason. A missing reason is the ordinary Core step-up shape; an unknown or malformed present reason fails.

func ParseReason

func ParseReason(value string) (Reason, error)

ParseReason validates a Budget-specific requirement reason.

func (Reason) String

func (reason Reason) String() string

String returns the registered reason token.

type Unit

type Unit struct {
	// contains filtered or unexported fields
}

Unit is one resource-selected Budget denomination and scale.

func NewUnit

func NewUnit(name string, decimals int64) (Unit, error)

NewUnit validates a Budget denomination and scale.

func (Unit) Decimals

func (unit Unit) Decimals() int64

Decimals returns the resource-declared scale.

func (Unit) Equal

func (unit Unit) Equal(other Unit) bool

Equal reports whether two units have the same identifier and scale.

func (Unit) Name

func (unit Unit) Name() string

Name returns the resource-declared unit identifier.

type UnitDeclaration

type UnitDeclaration struct {
	// contains filtered or unexported fields
}

UnitDeclaration is one budget_units Resource-metadata entry. Unit and scale identify the denomination. Maximum is optional in the protocol; a host may require it for a bounded pricing profile.

func FindUnitDeclaration

func FindUnitDeclaration(declarations []UnitDeclaration, unit Unit) (UnitDeclaration, bool)

FindUnitDeclaration returns the declaration for one exact denomination.

func NewBoundedUnitDeclaration

func NewBoundedUnitDeclaration(unit Unit, maximum int64, description string) (UnitDeclaration, error)

NewBoundedUnitDeclaration constructs a declaration with an advertised maximum amount for one Auth token.

func NewUnitDeclaration

func NewUnitDeclaration(unit Unit, description string) (UnitDeclaration, error)

NewUnitDeclaration constructs a declaration without an advertised maximum.

func (UnitDeclaration) Description

func (declaration UnitDeclaration) Description() string

Description returns the optional Markdown description.

func (UnitDeclaration) MarshalJSON

func (declaration UnitDeclaration) MarshalJSON() ([]byte, error)

MarshalJSON emits the flat budget_units metadata object.

func (UnitDeclaration) Maximum

func (declaration UnitDeclaration) Maximum() (int64, bool)

Maximum returns the advertised maximum when present.

func (UnitDeclaration) Unit

func (declaration UnitDeclaration) Unit() Unit

Unit returns the declared denomination and scale.

func (*UnitDeclaration) UnmarshalJSON

func (declaration *UnitDeclaration) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts one strict budget_units metadata object.

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is one non-negative Budget amount in a resource-selected unit.

func NewValue

func NewValue(amount int64, unit Unit) (Value, error)

NewValue validates a Budget amount and denomination.

func ParseValueJSON

func ParseValueJSON(data []byte) (Value, error)

ParseValueJSON parses exactly one strict Budget object.

func VerifyAuthToken

func VerifyAuthToken(
	ctx context.Context,
	compact string,
	resolver signaturekey.KeyResolver,
	clock aauth.Clock,
) (aauth.AuthTokenClaims, Value, error)

VerifyAuthToken verifies Core trust and consumes the complete Budget companion claim set.

func (Value) Amount

func (value Value) Amount() int64

Amount returns the integer Budget amount in Unit().Decimals() scale.

func (Value) MarshalJSON

func (value Value) MarshalJSON() ([]byte, error)

MarshalJSON emits the exact Budget object.

func (Value) Unit

func (value Value) Unit() Unit

Unit returns the Budget denomination and scale.

func (*Value) UnmarshalJSON

func (value *Value) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts only the exact Budget object.

Jump to

Keyboard shortcuts

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