Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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") )
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 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 (*Signer) Sign ¶
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 ¶
Verify checks that the given compact JWS (JWT) token is a valid signature for the provided message body.
Verify performs the following steps:
- Parses the token and enforces the PS256 signing method.
- Extracts "iss" and "kid" claims and validates "iss" against TrustedIssuers if configured.
- Resolves the corresponding RSA public key via the PublicKeyProvider.
- Enforces the minimum RSA key size.
- Verifies the JWS signature using the resolved public key.
- Validates that the "hash-alg" claim is "SHA256".
- 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.