Help us shape the future of Go! Take the 2026 Go Developer Survey

jwkutil

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package jwkutil converts between JSON Web Keys and Go public keys, and computes RFC 7638 thumbprints.

Both directions live here on purpose. The encode direction serves the JWKS endpoint and the parse direction serves DPoP proof validation, and keeping them together makes the round trip testable as a single property.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedKey     = errors.New("jwkutil: unsupported key type")
	ErrPrivateKeyMaterial = errors.New("jwkutil: jwk contains private key material")
	ErrWeakKey            = errors.New("jwkutil: key below minimum strength")
)

Sentinel errors.

Functions

func Thumbprint

func Thumbprint(j *JWK) (string, error)

Thumbprint returns the base64url-encoded SHA-256 RFC 7638 thumbprint.

The hash covers only the required members, in lexicographic order, with no whitespace. Optional members such as kid must not affect the result, or a key that later gains a kid would stop matching tokens bound to it.

Types

type JWK

type JWK struct {
	KTY string `json:"kty"`
	Use string `json:"use,omitempty"`
	KID string `json:"kid,omitempty"`
	ALG string `json:"alg,omitempty"`
	N   string `json:"n,omitempty"`   // RSA modulus
	E   string `json:"e,omitempty"`   // RSA exponent
	CRV string `json:"crv,omitempty"` // EC or OKP curve
	X   string `json:"x,omitempty"`   // EC x coordinate, or OKP public key
	Y   string `json:"y,omitempty"`   // EC y coordinate
}

JWK is a JSON Web Key. Only the public members are modelled; private members are detected during parsing and rejected.

func Encode

func Encode(pub crypto.PublicKey, kid, alg string) (*JWK, error)

Encode converts a public key into a JWK. kid and alg are optional.

func ParseJSON

func ParseJSON(raw json.RawMessage) (crypto.PublicKey, *JWK, error)

ParseJSON converts a raw JWK into a public key, rejecting anything that carries private key material or fails a strength or well-formedness check. It returns the decoded JWK alongside the key so callers can compute a thumbprint without unmarshalling twice.

Jump to

Keyboard shortcuts

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