sign

package
v0.34.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 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

Index

Constants

View Source
const ClaimClientID = "client_id"

ClaimClientID is the RFC 9068 client_id claim: the client a token was issued to (an OAuth client's id, or "pat:<jti>" for an API token and "cap:<jti>" for a capability, which each act as their own client). The mint decides it, and a backend partitions per-client state by it. See kdex-tech/host-manager#295.

View Source
const ClaimSubjectKind = "knd"

ClaimSubjectKind is the claim recording the kind of a credential's subject (kdexv1alpha1.SubjectKind: User or Client), decided once, when the credential is issued, and carried from then on. It is defined here, the lowest package that signs it, so the JWT signer, the PASETO mint and the auth context spell it one way. See kdex-tech/host-manager#255.

View Source
const SubjectlessRemedy = "a credential that names nobody is not a supported configuration; " +
	"fix the credential source so it supplies a subject -- add a `sub` key to the subject Secret, " +
	"or return a non-empty `claims.sub` from the credential-check backend"

SubjectlessRemedy is the operator-facing half of every subject-less rejection: what to FIX, not what went wrong. Kept as one exported constant so the JWT mint, the PASETO mint, and both validation paths give an operator reading the logs the same instruction.

Variables

View Source
var ErrNonExpiringToken = errors.New("refusing to sign a token that never expires: `exp` is missing, null, non-numeric or zero")

ErrSubjectlessCredential is the one error for a credential that names nobody. A subject-less credential is NOT a supported configuration: every gate in host-manager is keyed on who the caller is, so a token whose `sub` is empty is attributable to no one, authorizable against nothing, and auditable as nobody.

It is reachable, which is why this is a check and not a comment. jwt.MapClaims.GetSubject reports a MISSING `sub` as ("", nil) -- only a wrong-TYPED `sub` errors (golang-jwt v5 map_claims.go) -- so a credential source that vouches for a login without naming a subject silently produces `sub: ""`. Two real sources do exactly that:

  • a subject Secret keyed only by `email` (internal/auth/roles.go): the matcher accepts `email == subject`, and the claims it returns are the Secret's data keys minus `password`, so no `sub` key exists at all;
  • an http-lookup backend answering `{"ok":true}` or `{"ok":true,"claims":{}}` (internal/auth/lookup_http.go): the backend's `claims` object is returned verbatim, with an EMPTY MapClaims substituted when it is omitted.

LoginLocal never derives a subject from `username`, so nothing downstream repairs it.

It lives HERE, in the mint package, because this is where the empty subject is first manufactured and because internal/sign is the lowest package every token path can reach: internal/auth and internal/auth/apitoken both depend on it, and the dependency cannot run back. One sentinel means an operator grepping for one message finds every site that refused. ErrNonExpiringToken is returned when the final claim set would yield a token that never expires: `exp` missing, null, non-numeric, or zero. golang-jwt reads `exp: 0` as absent, and a verifier without WithExpirationRequired accepts an absent `exp` forever. The signer's default exp can only be lost this way through a projection a caller built and handed to SignProjected; a claimMappings rule cannot write `exp` (it is reserved, #211). A past exp is NOT refused: an already-expired token is harmless. See kdex-tech/host-manager#227.

View Source
var ErrSubjectlessCredential = errors.New(
	"subject-less credential: the credential source resolved without a `sub` claim")

Functions

func DropReservedClaims added in v0.32.0

func DropReservedClaims(mapped map[string]any)

DropReservedClaims deletes every reserved claim from a claimMappings result, so whatever the mapping's input held for it stands. A mapper's result is keyed by each rule's top-level target, so a rule writing beneath a reserved claim (act.sub) is dropped with it.

func MintDecidedClaims added in v0.29.0

func MintDecidedClaims() []string

MintDecidedClaims returns a copy of the claims the mint decides, so a test outside this package can pin each one as reserved and carried through Project; the copy keeps the signer's list out of any caller's reach.

func ReservedClaims added in v0.32.0

func ReservedClaims() []string

ReservedClaims returns a copy of the claims nothing but the mint may set.

func ReservedTargets added in v0.32.0

func ReservedTargets(rules []dmapper.MappingRule) []string

ReservedTargets returns the targetPropPath of every rule that writes a reserved claim or beneath one -- the rules DropReservedClaims will discard.

func WarnReservedTargets added in v0.32.0

func WarnReservedTargets(rules []dmapper.MappingRule, keysAndValues ...any)

WarnReservedTargets logs the rules among these that write a reserved claim, once per process for each distinct configuration and target set. Call it where a mapper is built from configuration: the mint drops such a rule's output on every token, and this is where an operator learns why it has no effect. keysAndValues name the configuration. Mappers are rebuilt on every page change and host reconcile, so without the once a single stored rule would repeat the error on each of them.

Types

type Signer

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

func NewSigner

func NewSigner(
	audience string,
	duration time.Duration,
	issuer string,
	privateKey *crypto.Signer,
	kid string,
	mapper *dmapper.Mapper,
) (*Signer, error)

NewSigner creates a new signer.

func (*Signer) Duration added in v0.2.78

func (s *Signer) Duration() time.Duration

Duration returns the lifetime each signed token is valid for. Cache implementations should subtract a skew from this to ensure a cached token always has meaningful remaining life on hit.

func (*Signer) Project added in v0.2.78

func (s *Signer) Project(signingContext jwt.MapClaims) (jwt.MapClaims, error)

Project derives the deterministic claim set that will be signed for a given signingContext. iat/exp/jti are deliberately omitted: they identify the token, not the identity, and putting them in the projection defeats the caller's ability to use the projection as a cache key.

The signer's audience is the AUTHORITATIVE outbound aud: Project is always called to re-issue a token for a SPECIFIC downstream target (the per-function FAT path in proxy.go), and re-using the inbound aud would make the issued token transferable to a different audience than the signer was configured for. Concretely: a user logs in at the host (aud=["https://<host>"]) and then calls a function; proxy.go builds a signer with audience=fn.Status.URL, but the previous logic reused the inbound host aud, so the function rejected the FAT with "token has invalid audience". Always use s.audience.

func (*Signer) Sign

func (s *Signer) Sign(signingContext jwt.MapClaims) (string, error)

Sign is a convenience wrapper that runs Project then SignProjected. Direct callers that want to cache should call the two steps separately so the cache key reflects the deterministic projection rather than the raw inbound context (which routinely includes per-request headers like Traceparent that defeat the cache).

func (*Signer) SignProjected added in v0.2.78

func (s *Signer) SignProjected(projected jwt.MapClaims) (string, error)

SignProjected attaches the per-token identifiers (iat/exp/jti) to an already-projected claim set and signs it. Use this in tandem with Project when caching to skip the projection step on a cache hit.

The projection has the LAST WORD on every field — including iat/exp/jti. The original Sign emitted mapper output last via maps.Copy, and several call sites depend on that semantic (e.g. dev-mode tests that inject exp=-1 via a ClaimMappings rule to synthesize an expired token). Default iat/exp/jti are written first, then maps.Copy(outboundClaims, projected) overrides anything the projection already carries.

func (*Signer) SignScoped added in v0.4.15

func (s *Signer) SignScoped(signingContext jwt.MapClaims, grantedScopes []string) (string, error)

SignScoped is Sign with OAuth scope confinement: it projects the context (which runs the ClaimMappings mapper and Compact), removes the scope-controlled claim families absent from grantedScopes, then signs. Confinement runs AFTER the mapper, so a ClaimMappings rule that injects into a scope-controlled family cannot smuggle it into a token whose scope did not grant it — regardless of which source claim the rule reads. Used by the primary subject mints (LoginLocal, mintTokensFromCode, mintTokensFromSubject); FAT projection, mint_token, OIDC exchange, and client_credentials keep raw Sign/Project. See kdex-tech/host-manager#140.

Jump to

Keyboard shortcuts

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