token

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

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
	// 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 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) (time.Time, error)

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

Jump to

Keyboard shortcuts

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