jwt

package
v0.72.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 2 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 jwt issues self-signed JSON Web Tokens. It parses a private signing key from a JSON Web Key (JWK) once and mints compact-serialized tokens on demand, stamping the key's id into the token header so verifiers can locate the matching public key.

The signing algorithm is derived from the key type for key types that map to a single unambiguous algorithm:

ed25519.PrivateKey  -> EdDSA
*ecdsa.PrivateKey   -> ES256 / ES384 / ES512 (by curve: P-256 / P-384 / P-521)
*mldsa.PrivateKey   -> ML-DSA-44 / ML-DSA-65 / ML-DSA-87 (from the JWK "alg")

ML-DSA keys use the RFC 9964 "AKP" key type, whose key material does not identify a parameter set: "kty":"AKP" with opaque "pub"/"priv" bytes may be any of ML-DSA-44, ML-DSA-65 or ML-DSA-87. RFC 9964 therefore requires the "alg" member on every AKP key, and for AKP it is the only source of the algorithm. It is still never trusted on its own: it must name an ML-DSA algorithm and the key must be an ML-DSA key, so it cannot disagree with the key.

RSA is intentionally unsupported: an RSA key does not determine a single algorithm (RS256/384/512, PS256/384/512), so signing one would require the library to pick on the caller's behalf.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SigningKey

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

SigningKey is a private signing key, parsed from a JWK, that mints signed JWTs using the algorithm determined by the key.

func ParseJWK

func ParseJWK(jwk string) (*SigningKey, error)

ParseJWK parses jwk, a single JSON Web Key, and returns its private signing key. The key must be of a type that maps to a single signing algorithm: an Ed25519 private key (kty "OKP", crv "Ed25519"), an ECDSA private key (kty "EC", crv "P-256", "P-384", or "P-521"), or an ML-DSA private key (kty "AKP", RFC 9964), all carrying the private component. RSA keys are rejected because their algorithm is ambiguous.

An AKP key carries no "crv", so its "alg" member is required and selects the ML-DSA parameter set. For every other key type "alg" is ignored.

func (*SigningKey) Issue

func (k *SigningKey) Issue(iss, sub string, auds []string, ttl time.Duration) (string, error)

Issue mints a compact-serialized JWT signed with the key, using the algorithm determined by the key. The signing key's id is set in the "kid" header field. The token carries all seven registered claims (RFC 7519): iss, sub, and every audience in auds, iat at the current time, nbf backdated by a small clock-skew leeway, exp ttl after issuance, and a random jti. aud is always encoded as a JSON array, which RFC 7519 permits for any number of audiences.

Jump to

Keyboard shortcuts

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