token

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 18 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

Overview

Package token checks access tokens. With them, a server admits only clients that a token service has admitted.

A token is a JWT signed with EdDSA (Ed25519) or ES256. Its cnf claim (RFC 7800) holds the client's Ed25519 public key, which must equal the key the client logs in with. A token is therefore useless without the private key. The server learns the token service's public keys from a JWKS, fetched from a URL or read from a file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// JWKSURL is the URL of the token service's JWKS. Exactly one of JWKSURL and JWKSFile is required.
	JWKSURL string
	// JWKSFile is a file with the JWKS, read once, for tests and for networks without access to the token service.
	JWKSFile string
	// Issuer is the required iss claim.
	Issuer string
	// Audience must be the aud claim, or one of its values.
	Audience string
	// LabelClaim names the claim that holds the label of the client's slot, e.g. the id of its device. Empty means
	// slots have no label.
	LabelClaim string
	// Leeway allows for clock differences in exp and nbf.
	Leeway time.Duration
	// HTTPClient fetches the JWKS. Nil means a client with a timeout of 10 s.
	HTTPClient *http.Client
	// Now returns the current time. Nil means time.Now.
	Now func() time.Time
}

Config configures a Verifier.

type DeniedError

type DeniedError struct {
	Reason string
}

DeniedError reports why a token was rejected.

func (*DeniedError) Error

func (e *DeniedError) Error() string

type Grant added in v0.3.0

type Grant struct {
	Expires time.Time
	// Owner is the token's sub: the owner of the client's key and of its slot. Empty if the token has no sub.
	Owner string
	// Label is the value of Config.LabelClaim, empty if that is not configured or the token has no such string claim.
	Label string
}

Grant is what a valid token says about its client.

type Verifier

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

Verifier checks tokens. It is safe for concurrent use.

func New

func New(cfg Config, log *slog.Logger) (*Verifier, error)

New returns a Verifier. It reads JWKSFile right away. A JWKS URL is fetched on the first token.

func (*Verifier) Leeway

func (v *Verifier) Leeway() time.Duration

Leeway returns the allowance for clock differences.

func (*Verifier) Verify

func (v *Verifier) Verify(ctx context.Context, token string, publicKey []byte) (Grant, error)

Verify checks a token for a client that logs in with the Ed25519 key publicKey. A rejected token returns a *DeniedError.

func (*Verifier) VerifyUnbound added in v0.3.0

func (v *Verifier) VerifyUnbound(ctx context.Context, token string) (Grant, error)

VerifyUnbound checks a token without requiring that it is bound to a key. Only requests that need no proof of a key accept such a token, e.g. looking up the owner's slot. A rejected token returns a *DeniedError.

Jump to

Keyboard shortcuts

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