apitoken

package
v0.34.3 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: 17 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 LegacyRevocationClass = "apitoken-revocation"

LegacyRevocationClass is the cycled class revocations lived in up to v0.27.x, where every host reconfiguration abandoned them. TokenManager still reads and writes it (WithLegacyRevocationCache) so that, during a rolling upgrade, a replica on v0.27.x and one on v0.28.0 honour each other's revocations, and a rollback below v0.28.0 keeps the ones made since.

LEGACY(#259): remove with every LEGACY(#259) site, once every host has run v0.28.0 or later and a rollback below it is no longer supported.

View Source
const RevocationClass = "apitoken-revocation-v2"

RevocationClass is the cache class revocations live in, in the scope that survives host generations: a revocation is state a request created, and losing it makes a revoked token valid again. See kdex-tech/host-manager#245.

View Source
const RevocationHorizon = 10 * 365 * 24 * time.Hour

RevocationHorizon is how long a revocation lasts on a host with no maximum API token lifetime. A revocation must outlive every token it revokes, and it only revokes tokens issued up to its own timestamp, so a long one blocks no key minted afterwards under the same name.

Variables

View Source
var ErrTTLExceedsMax = errors.New("ttl exceeds the host's maximum API token lifetime")

ErrTTLExceedsMax is returned when a mint asks for a lifetime longer than the host's KDexHost.spec.auth.apiToken.maxTTL. See kdex-tech/host-manager#238.

View Source
var ErrUnknownSubjectKind = errors.New("api token subject kind is not a known kind")

ErrUnknownSubjectKind is returned when a mint names no subject kind, or one this release does not know: a PAT's kind decides which KDexRoleBindings it gets, so it is recorded only when it can be enforced. See kdex-tech/host-manager#255.

Functions

func GenerateJTI added in v0.2.30

func GenerateJTI(aud, sub, act string) string

func GenerateKID

func GenerateKID(pubBytes []byte) string

func RevocationCaches added in v0.28.0

func RevocationCaches(cm cache.CacheManager) (store, legacy cache.Cache)

RevocationCaches returns the revocation store and the legacy class, created with the options every caller must agree on. MaxItems 0 means unbounded: the in-memory backend would otherwise evict a revocation to make room, which un-revokes a token.

Types

type KeyPair

type KeyPair struct {
	ActiveKey bool
	KeyId     string
	PublicKey *paseto.V4AsymmetricPublicKey
	SecretKey *paseto.V4AsymmetricSecretKey
}

func LoadKeysFromSecret

func LoadKeysFromSecret(secret *corev1.Secret, isActive bool) (*KeyPair, error)

LoadKeysFromSecret loads an key pair from a Kubernetes Secret.

type KeyPairs

type KeyPairs []*KeyPair

func GenerateDevmodeKeyPair

func GenerateDevmodeKeyPair() *KeyPairs

func (*KeyPairs) ActiveKey

func (p *KeyPairs) ActiveKey() *KeyPair

func (*KeyPairs) GetKey

func (p *KeyPairs) GetKey(kid string) (*KeyPair, bool)

type TokenData

type TokenData struct {
	Action     string `json:"act"`
	Audience   string `json:"aud"`
	Expiration int64  `json:"exp"`
	IssuedAt   int64  `json:"iat"`
	Issuer     string `json:"iss"`
	JTI        string `json:"jti"`
	KID        string `json:"kid"`
	NotBefore  int64  `json:"nbf"`
	Scope      string `json:"scp"`
	Subject    string `json:"sub"`
	// SubjectKind is the kind of Subject the mint recorded (sign.ClaimSubjectKind).
	// It is "" for a PAT minted before PATs recorded one (LEGACY(#268)).
	SubjectKind kdexv1alpha1.SubjectKind `json:"knd,omitempty"`
	// ClientID is the OAuth client the PAT was issued to (sign.ClaimClientID),
	// recorded only by MintClientKey: an RFC 8707 resource PAT minted at the
	// token endpoint for an authorization_code or refresh_token grant. It is
	// "" for every other PAT (a user-minted API key), which acts as its own
	// client. See kdex-tech/host-manager#295.
	ClientID string `json:"client_id,omitempty"`
}

type TokenManager

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

func APITokenManagerLoader

func APITokenManagerLoader(
	issuer string,
	secrets kdexv1alpha1.Secrets,
	revocationCache cache.Cache,
	devMode bool,
) (*TokenManager, error)

func NewTokenManager

func NewTokenManager(issuer string, keyPairs *KeyPairs, revocationCache cache.Cache) (*TokenManager, error)

func (*TokenManager) AdoptStrandedRevocations added in v0.28.0

func (tm *TokenManager) AdoptStrandedRevocations(ctx context.Context, cm cache.CacheManager) (int, error)

AdoptStrandedRevocations merges into the revocation store every revocation LegacyRevocationClass holds under any host generation, so the ones host reconfigurations stranded are in force again. Each key keeps the later (latestRevocation) of what the store has and what any generation holds, atomically against concurrent revocations, so nothing moves back in time. It returns how many entries it strengthened, and is idempotent and safe to run on every replica at once. See kdex-tech/host-manager#245.

LEGACY(#259): remove with LegacyRevocationClass.

func (*TokenManager) CapTTL added in v0.25.0

func (tm *TokenManager) CapTTL(ttl time.Duration) time.Duration

CapTTL shortens ttl to the host's maximum. It is for lifetimes the server chooses itself; a lifetime a caller asked for is refused, not shortened, so the caller is never handed a token that expires sooner than it requested.

func (*TokenManager) KeyPairs

func (tm *TokenManager) KeyPairs() KeyPairs

func (*TokenManager) MaxTTL added in v0.25.0

func (tm *TokenManager) MaxTTL() time.Duration

MaxTTL returns the host's maximum API token lifetime, zero when unbounded.

func (*TokenManager) MintClientKey added in v0.33.0

func (tm *TokenManager) MintClientKey(aud, sub string, kind kdexv1alpha1.SubjectKind, action, scope, clientID string, ttl time.Duration) (string, error)

MintClientKey is MintStatelessKey for a PAT issued to an OAuth client: it also records clientID in sign.ClaimClientID, so the FAT minted for the PAT names that client rather than the key (#295). Only the token endpoint's resource-PAT mint calls it, with the client the grant authenticated; no caller-facing mint surface takes a client, so a caller cannot choose one. The client is not part of the jti, so revocation keys do not change with it.

func (*TokenManager) MintStatelessKey

func (tm *TokenManager) MintStatelessKey(aud, sub string, kind kdexv1alpha1.SubjectKind, action, scope string, ttl time.Duration) (string, error)

MintStatelessKey mints a PASETO API token for sub, recording kind, the kind of sub, in sign.ClaimSubjectKind. The PAT's roles are resolved with that kind for its whole life, so a kind this release does not know (or none) is refused with ErrUnknownSubjectKind rather than recorded.

func (*TokenManager) RevocationTTL added in v0.26.0

func (tm *TokenManager) RevocationTTL() time.Duration

RevocationTTL is the shortest lifetime of a revocation: never shorter than the longest token this host can mint, so a revoked token cannot come back.

func (*TokenManager) RevokeByMetadata added in v0.2.30

func (tm *TokenManager) RevokeByMetadata(ctx context.Context, aud, sub, act string, ttl time.Duration) error

func (*TokenManager) RevokeSubject added in v0.28.0

func (tm *TokenManager) RevokeSubject(ctx context.Context, sub string, ttl time.Duration) error

RevokeSubject revokes, as of now, every API token this host issued for sub on any audience, whatever its act, so it reaches keys nobody recorded. SubjectRevokedSince lets the paths that issue new credentials refuse one whose authentication predates it. A token minted afterwards from a fresh authentication (a re-signup under the same address, or a re-login, including a silent one through an upstream IdP, whose session this does not end) is valid. Like a per-token revocation it lasts at least RevocationTTL and never moves back.

It is written only to the revocation store: a replica on v0.27.x or earlier cannot read it, so it takes effect host-wide once every replica runs v0.28.0, and a rollback below v0.28.0 drops it. See kdex-tech/host-manager#257.

func (*TokenManager) RevokeToken added in v0.2.30

func (tm *TokenManager) RevokeToken(ctx context.Context, signed string) error

func (*TokenManager) SubjectRevokedSince added in v0.28.0

func (tm *TokenManager) SubjectRevokedSince(ctx context.Context, sub string, issuedAt time.Time) (bool, error)

SubjectRevokedSince reports whether sub was revoked (RevokeSubject) at or after issuedAt, i.e. whether a credential for sub authenticated at issuedAt is cut off. A store that cannot be read is an error, never a no.

func (*TokenManager) TokenPrefix added in v0.3.0

func (tm *TokenManager) TokenPrefix() string

TokenPrefix returns this host's white-label API token prefix (empty when prefixing is off). Callers that need to discriminate a brand-prefixed PAT on the wire (e.g. the Bearer auth path) use this to recognize the prefix without reaching into the manager's internals.

func (*TokenManager) ValidateToken

func (tm *TokenManager) ValidateToken(ctx context.Context, signed, expectedAudience string) (*TokenData, error)

ValidateToken parses and verifies a PASETO API token.

expectedAudience is REQUIRED in spirit but technically optional in signature:

  • non-empty: a paseto.ForAudience rule is added so a token minted for a different audience is rejected. This is the correct mode for any handler that uses ValidateToken's success as proof the bearer should be granted access on the receiving party.
  • empty: the audience check is skipped. This is ONLY appropriate when the token is being INSPECTED (e.g. to extract its subject for a revocation flow) — not when validating for use. Passing "" in any other context is the kdex-tech/host-manager#69 confused-deputy regression.

func (*TokenManager) WithLegacyRevocationCache added in v0.28.0

func (tm *TokenManager) WithLegacyRevocationCache(legacy cache.Cache) *TokenManager

WithLegacyRevocationCache also reads and writes per-token revocations in LegacyRevocationClass, for the rolling upgrade and rollback it describes. Returns the receiver for chaining. LEGACY(#259).

func (*TokenManager) WithMaxTTL added in v0.25.0

func (tm *TokenManager) WithMaxTTL(maxTTL time.Duration) *TokenManager

WithMaxTTL sets the longest lifetime this host mints an API token for; zero (the default) leaves mint lifetimes unbounded. The controller sources it from KDexHost.spec.auth.apiToken.maxTTL. Returns the receiver for chaining.

func (*TokenManager) WithTokenPrefix added in v0.2.94

func (tm *TokenManager) WithTokenPrefix(prefix string) *TokenManager

WithTokenPrefix sets this host's white-label API token prefix. Prefixing is off by default (empty), in which case tokens are emitted as bare "v4.public." PASETO strings. When set, the prefix REPLACES the PASETO header on the wire (see wrap/unwrap). Returns the receiver for chaining. This is the single configuration point for the prefix; the controller sources it from KDexHost.spec.auth.apiToken.tokenPrefix (falling back to the NexusConfiguration default) and calls this once per host.

Jump to

Keyboard shortcuts

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