jose.jws

index · jose

Overview

JSON Web Signatures (RFC 7515) in the compact serialization, over the keys of jose.jwk: EdDSA, ES256 and HS256. sign takes the algorithm from the key and never from a header; verify refuses none, any algorithm it does not carry, any algorithm outside the caller's allowlist, and any header with crit or b64. Every segment is read as strict base64url, and every length is checked before the bytes reach the crypto bindings.

A key set selects its key by the header's kid; a kid the set lacks fails with the exact string unknown kid, so a relying party knows to refetch its JWKS.

Functions

NameSignature
signsign(payload, key, header) -> compact, err
verifyverify(compact, keys, opts) -> payload, header
decodedecode(compact) -> header, payload, signature

sign(payload, key, header) -> compact, err

Sign a payload in the compact serialization

The protected header carries alg from the key, kid from the key when the key has one and header does not, and every member of header as given: typ, nonce, url, jwk, kid. A header.alg that is not the key's is refused. Nothing else is added, so a header of one member encodes the same bytes every time. The payload is any string, empty included. Raises on a payload that is not a string or a key that is not a table.

verify(compact, keys, opts) -> payload, header

Verify a compact serialization and return its payload

keys is one key or a key set. The checks run in this order: the size cap, three segments, strict base64url, a header object with a string alg, an alg this module carries (none is not one), crit and b64 absent, alg in opts.algs when given, the key selected by kid, alg equal to the key's own, the signature's length, and at last the signature itself. An HS256 tag is compared in constant time. Header jwk, x5c and typ are returned and never acted on. On success the second value is the decoded header.

decode(compact) -> header, payload, signature

Decode a compact serialization without verifying it

Runs the structural checks of verify and none of the cryptographic ones: for reading a kid or a typ before a key is chosen. Nothing it returns is trusted.