jwtsigning

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRSAKeyLength              = errors.New("RSA key is too small")
	ErrUndefinedHashingAlgorithm = errors.New("undefined hashing algorithm")
	ErrUndefinedSigningAlgorithm = errors.New("undefined signing algorithm")
	ErrNilKeyProvider            = errors.New("keyProvider cannot be nil")
)
View Source
var (
	ErrUnexpectedClaimsType     = errors.New("unexpected claims type")
	ErrUnexpectedSigningMethod  = errors.New("unexpected signing method")
	ErrMissingIssOrKid          = errors.New("missing iss or kid in token")
	ErrUntrustedIssuer          = errors.New("untrusted issuer")
	ErrJWTParseFailed           = errors.New("jwt parse failed")
	ErrUnsupportedHashAlgorithm = errors.New("unsupported hash algorithm")
	ErrSignatureInvalid         = errors.New("jwt signature invalid")
	ErrHashClaimMissing         = errors.New("missing hash claim")
	ErrMessageHashMismatch      = errors.New("message hash mismatch")
	ErrNilPublicKeyProvider     = errors.New("publicKeyProvider cannot be nil")
	ErrNoTrustedIssuers         = errors.New("trusted issuers cannot be nil")
)

Functions

This section is empty.

Types

type Hasher

type Hasher interface {
	HashMessage(body []byte) string
	ToString() string
}

type KeyMetadata

type KeyMetadata struct {
	Iss string // typically the cluster URL that exposes .well-known/jwks.json
	Kid string // uniquely identifies the signing key under that issuer
}

KeyMetadata describes the logical identity of a signing key. It maps directly to the JWT "iss" (issuer) and "kid" (key ID) used by verifiers to look up the matching public key via the configured trust mechanism.

type PrivateKeyProvider

type PrivateKeyProvider interface {
	// CurrentSigningKey returns the key+metadata to use for signing.
	CurrentSigningKey(ctx context.Context) (*rsa.PrivateKey, KeyMetadata, error)
}

PrivateKeyProvider supplies the current RSA private key and its metadata for signing outgoing messages.

type PublicKeyProvider

type PublicKeyProvider interface {
	// VerificationKey returns the public key for given issuer and kid.
	VerificationKey(ctx context.Context, iss, kid string) (*rsa.PublicKey, error)
}

PublicKeyProvider resolves RSA public keys for verification of incoming message signatures.

type SHA256Hasher

type SHA256Hasher struct{}

func (*SHA256Hasher) HashMessage

func (s *SHA256Hasher) HashMessage(body []byte) string

func (*SHA256Hasher) ToString

func (s *SHA256Hasher) ToString() string

type Signer

type Signer struct {
	Hasher
	// contains filtered or unexported fields
}

Signer signs message bodies into JWS (JWT) tokens.

func NewSigner

func NewSigner(keyProvider PrivateKeyProvider, hasher Hasher) (*Signer, error)

func (*Signer) Sign

func (s *Signer) Sign(ctx context.Context, body []byte) (string, error)

Sign creates a compact JWS (JWT) for the given message body using PS256.

The returned string is suitable for use as a value of an HTTP or message header (e.g. "X-Message-Signature"). The token will contain the following claims and headers:

  • claims: iss: issuer from KeyMetadata kid: key ID from KeyMetadata hash: base64url(SHA-256(body)) hash-alg: "SHA256"
  • headers: typ: "JWT" alg: "PS256"

Sign obtains the private key and metadata from the configured PrivateKeyProvider and enforces the minimum RSA key size before signing. It returns an error if the provider fails, if the key is too small, or if the token cannot be signed.

type Verifier

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

Verifier verifies signed messages represented as compact JWS (JWT) tokens.

func NewVerifier

func NewVerifier(publicKeyProvider PublicKeyProvider, hasher Hasher, trustedIssuers map[string]struct{}) (*Verifier, error)

func (*Verifier) Verify

func (v *Verifier) Verify(ctx context.Context, tokenStr string, body []byte) error

Verify checks that the given compact JWS (JWT) token is a valid signature for the provided message body.

Verify performs the following steps:

  1. Parses the token and enforces the PS256 signing method.
  2. Extracts "iss" and "kid" claims and validates "iss" against TrustedIssuers if configured.
  3. Resolves the corresponding RSA public key via the PublicKeyProvider.
  4. Enforces the minimum RSA key size.
  5. Verifies the JWS signature using the resolved public key.
  6. Validates that the "hash-alg" claim is "SHA256".
  7. Recomputes base64url(SHA-256(body)) and compares it to the "hash" claim in constant time.

If any step fails, Verify returns a non-nil error and the caller should treat the message as untrusted.

Jump to

Keyboard shortcuts

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