Standards-driven primitives for validating JSON Web Tokens. More...
Classes | |
| class | sourcemeta::core::JWKParseError |
| class | sourcemeta::core::JWKSParseError |
| class | sourcemeta::core::JWTParseError |
| class | sourcemeta::core::JWEParseError |
| class | sourcemeta::core::JWE |
| class | sourcemeta::core::JWK |
| class | sourcemeta::core::JWKPrivate |
| class | sourcemeta::core::JWKS |
| class | sourcemeta::core::JWKSProvider |
| class | sourcemeta::core::JWT |
| struct | sourcemeta::core::JWTClockSkew |
Functions | |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::to_jws_algorithm (const std::string_view value) noexcept -> std::optional< JWSAlgorithm > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jws_algorithm_name (const JWSAlgorithm algorithm) noexcept -> std::string_view |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jws_algorithm_is_asymmetric (const JWSAlgorithm algorithm) noexcept -> bool |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jws_algorithm_digest_bits (const JWSAlgorithm algorithm) noexcept -> std::uint16_t |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::to_jwe_algorithm (const std::string_view value) noexcept -> std::optional< JWEAlgorithm > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwe_algorithm_name (const JWEAlgorithm algorithm) noexcept -> std::string_view |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::to_jwe_encryption (const std::string_view value) noexcept -> std::optional< JWEEncryption > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwe_encryption_name (const JWEEncryption encryption) noexcept -> std::string_view |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwe_algorithm_is_asymmetric (const JWEAlgorithm algorithm) noexcept -> bool |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwe_encryption_key_bytes (const JWEEncryption encryption) noexcept -> std::size_t |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwe_decrypt (const JWE &jwe, const JWKPrivate &key) -> std::optional< std::string > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwe_encrypt (const JSON &header, const std::string_view plaintext, const JWK &key) -> std::optional< std::string > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jws_sign (const JWSAlgorithm algorithm, const std::string_view signing_input, const JWKPrivate &key) -> std::optional< std::string > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwt_sign (const JSON &header, const JSON &payload, const JWKPrivate &key) -> std::optional< std::string > |
| auto | sourcemeta::core::jwt_bounded_clock_skew (const std::chrono::seconds skew) noexcept -> std::chrono::seconds |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwt_check_claims (const JWT &token, const std::string_view expected_issuer, const std::string_view expected_audience, const std::chrono::system_clock::time_point now, const JWTClockSkew clock_skew={}, const std::optional< std::string_view > expected_subject=std::nullopt) -> std::optional< JWTClaimError > |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jws_verify_signature (const std::optional< JWSAlgorithm > algorithm, const std::string_view signing_input, const std::string_view signature, const JWK &key) -> bool |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwt_verify_signature (const JWT &token, const JWK &key) -> bool |
| SOURCEMETA_CORE_JOSE_EXPORT auto | sourcemeta::core::jwt_verify (const JWT &token, const JWKS &keys, const std::span< const JWSAlgorithm > allowed_algorithms, const std::string_view expected_issuer, const std::string_view expected_audience, const std::chrono::system_clock::time_point now, const JWTClockSkew clock_skew, const std::optional< std::string_view > expected_subject, const std::optional< std::string_view > expected_type) -> std::optional< JWTVerificationError > |
Standards-driven primitives for validating JSON Web Tokens.
This functionality is included as follows:
| class sourcemeta::core::JWKParseError |
An error that occurs when parsing an invalid JSON Web Key.
| class sourcemeta::core::JWKSParseError |
An error that occurs when parsing an invalid JSON Web Key Set.
| class sourcemeta::core::JWTParseError |
An error that occurs when parsing an invalid JSON Web Token.
| class sourcemeta::core::JWEParseError |
An error that occurs when parsing an invalid JSON Web Encryption object.
| class sourcemeta::core::JWE |
A parsed JSON Web Encryption object in compact serialization (RFC 7516). It owns its decoded segments, so the string it was parsed from need not outlive it. The plaintext is recovered by passing it to jwe_decrypt. For example:
Public Member Functions | |
| JWE (const std::string_view input) | |
| auto | algorithm () const noexcept -> std::optional< JWEAlgorithm > |
| auto | encryption () const noexcept -> std::optional< JWEEncryption > |
| auto | key_id () const noexcept -> std::optional< std::string_view > |
| The key identifier from the protected header, if present. | |
| auto | header () const noexcept -> const JSON & |
| The decoded protected header. | |
| auto | protected_header () const noexcept -> std::string_view |
| auto | encrypted_key () const noexcept -> std::string_view |
| auto | initialization_vector () const noexcept -> std::string_view |
| The decoded JWE Initialization Vector. | |
| auto | ciphertext () const noexcept -> std::string_view |
| The decoded JWE Ciphertext. | |
| auto | tag () const noexcept -> std::string_view |
| The decoded JWE Authentication Tag. | |
Static Public Member Functions | |
| static auto | from (const std::string_view input) -> std::optional< JWE > |
|
explicit |
Parse a JSON Web Encryption object from its compact serialization, throwing a JWEParseError on invalid input.
|
inlinenodiscardnoexcept |
The key-management algorithm declared in the protected header, if it names a supported one.
|
inlinenodiscardnoexcept |
The decoded JWE Encrypted Key, empty for direct key agreement and direct encryption.
|
inlinenodiscardnoexcept |
The content encryption algorithm declared in the protected header, if it names a supported one.
|
staticnodiscard |
Parse a JSON Web Encryption object from its compact serialization, returning no value on invalid input.
|
inlinenodiscardnoexcept |
The exact base64url-encoded protected header, the Additional Authenticated Data the content encryption is bound to (RFC 7516 Section 5.1).
| class sourcemeta::core::JWK |
A parsed public JSON Web Key (RFC 7517), restricted to RSA, elliptic curve, octet key pair (RFC 8037), and symmetric octet (RFC 7518 Section 6.4) keys. The key owns its decoded material, so the source JSON document does not need to outlive it. For example:
Public Types | |
| enum class | Type : std::uint8_t { RSA , EllipticCurve , OctetKeyPair , Octet } |
| The family of key material a key holds. More... | |
Public Member Functions | |
| JWK (const JSON &value) | |
| Parse a JSON Web Key from a JSON value, throwing on invalid input. | |
| JWK (JSON &&value) | |
| Parse a JSON Web Key from a JSON value, throwing on invalid input. | |
| JWK (JWK &&other) noexcept=default | |
| A key exclusively owns its parsed public key, so it is move-only. | |
| JWK (const JWK &)=delete | |
| auto | type () const noexcept -> Type |
| The family of key material this key holds. | |
| auto | key_id () const noexcept -> std::optional< std::string_view > |
| The key identifier used to select this key, if present. | |
| auto | algorithm () const noexcept -> std::optional< JWSAlgorithm > |
| The algorithm this key is intended for, if present. | |
| auto | curve () const noexcept -> std::string_view |
| The curve this key is pinned to, empty when it carries none. | |
| auto | public_key () const noexcept -> const PublicKey * |
| auto | secret () const noexcept -> std::string_view |
| The raw symmetric secret, empty for asymmetric keys. | |
| auto | public_jwk () const -> std::optional< JSON > |
| auto | thumbprint () const -> std::optional< std::string > |
Static Public Member Functions | |
| static auto | from (const JSON &value) -> std::optional< JWK > |
| static auto | from (JSON &&value) -> std::optional< JWK > |
| static auto | from_octets (const std::string_view secret) -> JWK |
|
strong |
|
staticnodiscard |
Build a symmetric octet key (RFC 7518 Section 6.4) directly from raw secret octets, taken as-is rather than base64url-decoded, so a caller holding a shared secret does not assemble and re-parse an oct JSON Web Key. The minimum secret size each symmetric algorithm demands is enforced where the key is used, not here. For example:
|
nodiscard |
Serialize the public part of this key as a JSON Web Key (RFC 7517), returning no value for a symmetric key, which has no public form.
|
inlinenodiscardnoexcept |
The parsed platform key, null when the material could not be turned into one.
|
nodiscard |
The SHA-256 JSON Web Key thumbprint of this key (RFC 7638), base64url-encoded.
| class sourcemeta::core::JWKPrivate |
A parsed private JSON Web Key (RFC 7517), the private counterpart to a public one, restricted to RSA, elliptic curve, octet key pair (RFC 8037), and symmetric octet (RFC 7518 Section 6.4) keys. The key owns its decoded material, so neither the source JSON document nor the source PEM needs to outlive it. For example:
Public Types | |
| enum class | Type : std::uint8_t { RSA , EllipticCurve , OctetKeyPair , Octet } |
| The family of key material a key holds. More... | |
Public Member Functions | |
| JWKPrivate (JWKPrivate &&other) noexcept=default | |
| A key exclusively owns its parsed private key, so it is move-only. | |
| JWKPrivate (const JWKPrivate &)=delete | |
| auto | type () const noexcept -> Type |
| The family of key material this key holds. | |
| auto | key_id () const noexcept -> std::optional< std::string_view > |
| The key identifier used to select this key, if present. | |
| auto | algorithm () const noexcept -> std::optional< JWSAlgorithm > |
| The algorithm this key is intended for, if present. | |
| auto | curve () const noexcept -> std::string_view |
| The curve this key is pinned to, empty when it carries none. | |
| auto | private_key () const noexcept -> const PrivateKey * |
| The parsed private key, or null when the material could not be decoded. | |
| auto | secret () const noexcept -> std::string_view |
| The raw symmetric secret, empty for asymmetric keys. | |
| auto | public_jwk () const -> std::optional< JSON > |
| auto | thumbprint () const -> std::optional< std::string > |
Static Public Member Functions | |
| static auto | from (const JSON &value) -> std::optional< JWKPrivate > |
| static auto | from (JSON &&value) -> std::optional< JWKPrivate > |
| static auto | from_pem (const std::string_view pem) -> std::optional< JWKPrivate > |
| static auto | from_octets (const std::string_view secret) -> JWKPrivate |
|
strong |
|
staticnodiscard |
|
staticnodiscard |
|
staticnodiscard |
Build a symmetric octet key (RFC 7518 Section 6.4) directly from raw secret octets, taken as-is rather than base64url-decoded, so a caller minting a token under a shared secret does not assemble and re-parse an oct JSON Web Key. The minimum secret size each symmetric algorithm demands is enforced where the key is used, not here. For example:
|
staticnodiscard |
Parse a private key from an unencrypted PKCS#8 PEM document (RFC 5958), returning no value on invalid input. The key identifier and algorithm are left unset, as a PEM document carries no such metadata.
|
nodiscard |
Serialize the public part of this key as a JSON Web Key (RFC 7517), returning no value for a symmetric key, which has no public form, or for a key parsed from a PEM document whose public part could not be recovered.
|
nodiscard |
The SHA-256 JSON Web Key thumbprint of this key (RFC 7638), base64url-encoded, returning no value for a key whose public part could not be recovered.
| class sourcemeta::core::JWKS |
A parsed JSON Web Key Set (RFC 7517 Section 5). Keys that individually fail to parse, such as those of an unsupported type, are skipped rather than failing the whole set, so one exotic key cannot break verification of tokens signed by the others. The set owns its keys. For example:
Public Member Functions | |
| JWKS (const JSON &value) | |
| JWKS (JSON &&value) | |
| JWKS (JWKS &&other) noexcept=default | |
| A key set exclusively owns its keys, so it is move-only. | |
| JWKS (const JWKS &)=delete | |
| auto | find (const std::string_view key_id) const noexcept -> const JWK * |
| auto | size () const noexcept -> std::size_t |
| The number of keys in the set. | |
| auto | empty () const noexcept -> bool |
| Whether the set holds no keys. | |
Static Public Member Functions | |
| static auto | from (const JSON &value) -> std::optional< JWKS > |
| static auto | from (JSON &&value) -> std::optional< JWKS > |
|
explicit |
Parse a JSON Web Key Set from a JSON value, throwing a JWKSParseError on invalid input.
|
explicit |
Parse a JSON Web Key Set from a JSON value, throwing a JWKSParseError on invalid input.
|
nodiscardnoexcept |
Look up a key by its identifier (RFC 7515 Section 4.1.4), returning no pointer when no key in the set carries it.
| class sourcemeta::core::JWKSProvider |
A stateful, thread-safe resolver that owns a key set URL and verifies tokens against it, fetching and refreshing the keys internally. It adds caching, a freshness-aware refresh, a guarded refetch on rotation, and serving of the previously held keys through a transient outage. The current time is read internally from an injectable clock, so a caller never deals with it and tests can drive expiry, rotation, and the refetch cooldown deterministically.
Public Types | |
| using | Fetcher |
| using | Clock = std::function<std::chrono::system_clock::time_point()> |
Public Member Functions | |
| JWKSProvider (std::string jwks_uri, Fetcher fetcher) | |
| JWKSProvider (std::string jwks_uri, Fetcher fetcher, Options options) | |
| JWKSProvider (std::string jwks_uri, Fetcher fetcher, Options options, Clock clock) | |
| JWKSProvider (const JWKSProvider &)=delete | |
| JWKSProvider (JWKSProvider &&)=delete | |
| auto | verify (const JWT &token, const std::span< const JWSAlgorithm > allowed_algorithms, const std::string_view expected_issuer, const std::string_view expected_audience, const std::optional< std::string_view > expected_subject, const std::optional< std::string_view > expected_type) -> std::optional< JWTVerificationError > |
| auto | verify_access_token (const JWT &token, const std::span< const JWSAlgorithm > allowed_algorithms, const std::string_view expected_issuer, const std::string_view expected_audience, const std::optional< std::string_view > expected_subject=std::nullopt) -> std::optional< JWTVerificationError > |
| auto | verify (const JWT &token, const std::span< const JWSAlgorithm > allowed_algorithms, const std::string_view expected_issuer, const std::string_view expected_audience, const std::optional< std::string_view > expected_subject, const std::optional< std::string_view > expected_type, std::chrono::system_clock::time_point &resolved_now) -> std::optional< JWTVerificationError > |
| using sourcemeta::core::JWKSProvider::Clock = std::function<std::chrono::system_clock::time_point()> |
A source of the current time. It defaults to the system clock and exists as an injection point so that expiry, rotation, and the refetch cooldown can be driven deterministically under test.
A pluggable transport that turns a URL into raw key set bytes plus an optional freshness hint. Returns no value on a failed retrieval, such as a transport error, an unsuccessful response, or an oversized body. Injecting the transport keeps this module free of any networking dependency and makes the provider substitutable for testing.
| sourcemeta::core::JWKSProvider::JWKSProvider | ( | std::string | jwks_uri, |
| Fetcher | fetcher ) |
Construct a provider for a concrete key set URL with an injected transport, using the default policy and the system clock.
| sourcemeta::core::JWKSProvider::JWKSProvider | ( | std::string | jwks_uri, |
| Fetcher | fetcher, | ||
| Options | options ) |
Construct a provider for a concrete key set URL with an injected transport, overriding the caching and verification policy.
| sourcemeta::core::JWKSProvider::JWKSProvider | ( | std::string | jwks_uri, |
| Fetcher | fetcher, | ||
| Options | options, | ||
| Clock | clock ) |
Construct a provider for a concrete key set URL with an injected transport, overriding the policy and the clock.
|
delete |
The provider owns a lock guarding its cache, so it is neither copyable nor movable. Store it behind a pointer or construct it in place.
|
nodiscard |
Verify a token against the provider's key set, fetching or refreshing the keys as needed. Returns no value when the token is fully valid, otherwise the first failing step. The current time and the clock skew tolerance are the provider's own concern, so a caller supplies only what identifies the token: the expected subject, or no value to accept any, and the expected typ header, or no value to accept any.
Neither is defaulted. RFC 9068 Section 4 requires a resource server to "verify that the `typ` header value is `at+jwt` or `application/at+jwt` and reject tokens carrying any other value", and Section 5 explains that this is what keeps an OpenID Connect ID Token from being accepted as an access token. A default of no value would make skipping that check the thing a caller gets for writing less, so the decision is spelled at the call site instead. Prefer verify_access_token when the token is an access token.
|
nodiscard |
Verify a token exactly as the other overload does, additionally exposing the clock reading the verification ran at, so that a caller performing its own time checks on the same token, such as an issued-at age or an authentication-time freshness window, shares the verifier's clock rather than injecting a second, independently read one.
|
nodiscard |
Verify a token as an RFC 9068 JWT access token, pinning the typ header to at+jwt so the profile's Section 4 requirement cannot be omitted.
| class sourcemeta::core::JWT |
A parsed JSON Web Token in compact serialization (RFC 7519, RFC 7515). The token does not own its input, so the string it was parsed from must outlive it. For example:
Public Member Functions | |
| JWT (const std::string_view input) | |
| auto | algorithm () const noexcept -> std::optional< JWSAlgorithm > |
| The signing algorithm declared in the token header, if present. | |
| auto | key_id () const noexcept -> std::optional< std::string_view > |
| The key identifier from the token header, if present. | |
| auto | type () const noexcept -> std::optional< std::string_view > |
| The token type declared in the header, if present. | |
| auto | has_type (const std::string_view media_type) const -> bool |
| auto | header () const noexcept -> const JSON & |
| The decoded token header. | |
| auto | issuer () const noexcept -> std::optional< std::string_view > |
| The issuer that created the token, if present. | |
| auto | subject () const noexcept -> std::optional< std::string_view > |
| The subject the token is about, if present. | |
| auto | has_audience (const std::string_view audience) const noexcept -> bool |
| Whether the token is intended for the given audience. | |
| auto | expires_at () const -> std::optional< std::chrono::system_clock::time_point > |
| The time after which the token is no longer valid, if present. | |
| auto | not_before () const -> std::optional< std::chrono::system_clock::time_point > |
| The time before which the token is not yet valid, if present. | |
| auto | issued_at () const -> std::optional< std::chrono::system_clock::time_point > |
| The time at which the token was issued, if present. | |
| auto | token_id () const noexcept -> std::optional< std::string_view > |
| The unique identifier of the token, if present. | |
| auto | payload () const noexcept -> const JSON & |
| The decoded token payload. | |
| auto | signing_input () const noexcept -> std::string_view |
| The exact wire bytes the signature is computed over. | |
| auto | signature () const noexcept -> std::string_view |
| The raw token signature. | |
Static Public Member Functions | |
| static auto | from (const std::string_view input) -> std::optional< JWT > |
|
explicit |
Parse a JSON Web Token from its compact serialization, throwing a JWTParseError on invalid input.
|
staticnodiscard |
Parse a JSON Web Token from its compact serialization, returning no value on invalid input.
|
nodiscard |
Whether the token declares the given type, treating a subtype without a slash as application/-prefixed and comparing case-insensitively (RFC 7519 Section 5.1, RFC 7515 Section 4.1.9).
| struct sourcemeta::core::JWTClockSkew |
A tolerance for clock disagreement between the token issuer and the verifier, carried per time-based claim so that, for example, a deployment can forgive a small expiration overshoot without also accepting tokens issued in the future. A plain seconds value converts to a uniform tolerance across every claim. For example:
|
strong |
The JSON Web Encryption key-management algorithms (the alg value) from RFC 7518 Section 4.1. Each one determines how the content encryption key is delivered to the recipient, and demands a key of exactly one family, which is what keeps algorithm confusion attacks unexploitable.
|
strong |
The JSON Web Encryption content encryption algorithms (the enc value) from RFC 7518 Section 5.1. Each one seals the plaintext under the content encryption key with an authenticated encryption scheme.
|
strong |
The JSON Web Signature algorithms from RFC 7518 Section 3.1 and the Edwards-curve algorithm from RFC 8037 Section 3.1. The null algorithm is intentionally absent. Each algorithm demands a key of exactly one family, the symmetric algorithms an octet sequence and the asymmetric ones their own key type, which is what keeps algorithm confusion attacks unexploitable.
|
strong |
The claim validation errors that claim checking can return, one per check performed rather than an exhaustive list of registered claims.
|
strong |
The steps of full token verification that can fail, in the order they are evaluated.
|
noexcept |
Whether the key-management algorithm consumes an asymmetric recipient key (RSA-OAEP*, ECDH-ES*) rather than a shared octet secret (A*KW, dir) (RFC 7518 Section 4.1). For example:
|
noexcept |
Map a JSON Web Encryption key-management algorithm to its alg value, the inverse of parsing (RFC 7518 Section 4.1). For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jwe_decrypt | ( | const JWE & | jwe, |
| const JWKPrivate & | key ) -> std::optional< std::string > |
Decrypt a JSON Web Encryption object with the recipient key, returning the plaintext (RFC 7516). The key is the recipient private key for the asymmetric algorithms and the shared octet secret for A*KW and dir. Returns no value on any failure, uniformly, so that a key-unwrap failure, an authentication tag mismatch, and a structural error are indistinguishable and the recipient cannot be used as a decryption oracle (RFC 7516 Sections 11.4 and 11.5). For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jwe_encrypt | ( | const JSON & | header, |
| const std::string_view | plaintext, | ||
| const JWK & | key ) -> std::optional< std::string > |
Encrypt a plaintext into a JSON Web Encryption object in compact serialization (RFC 7516). The key-management algorithm and content encryption algorithm are taken from the protected header alg and enc parameters (RFC 7516 Sections 4.1.1 and 4.1.2). A fresh initialization vector is always generated. The content encryption key is generated as the algorithm requires: freshly random for the key-wrapping and RSA algorithms, derived from the agreement for direct ECDH-ES, and the shared secret itself for dir. For the ECDH-ES algorithms an ephemeral key is minted and its public part added to the emitted protected header as the epk parameter. The key is the recipient public key for the asymmetric algorithms and the shared octet secret for A*KW and dir. Returns no value when the header is not an object, has duplicate members, carries a critical extension or a compression parameter, names an unsupported or key-incompatible algorithm, or the key cannot serve it. For example:
|
noexcept |
The content encryption key length, in bytes, that a content encryption algorithm requires (RFC 7518 Sections 5.2 and 5.3). The CBC-HMAC algorithms take a double-width key that is split into a MAC key and an encryption key. For example:
|
noexcept |
Map a JSON Web Encryption content encryption algorithm to its enc value, the inverse of parsing (RFC 7518 Section 5.1). For example:
|
noexcept |
The size, in bits, of the digest each signature algorithm is defined over (RFC 7518 Section 3.1), so that derived hash claims such as OpenID Connect's at_hash and c_hash select their digest by table rather than by slicing the algorithm name. The Edwards-curve algorithm names no digest and OpenID Connect leaves the choice unspecified, so it is pinned to SHA-512, the convention deployed implementations follow for Ed25519, whose signature scheme is internally defined over that hash (RFC 8032 Section 5.1). Hash claims minted over Ed448 by a SHAKE256-based implementation need curve-specific handling that no algorithm-only table can provide. For example:
|
noexcept |
Whether an algorithm is an asymmetric digital signature algorithm rather than a symmetric message authentication code (RFC 7518 Section 3.1). For example:
|
noexcept |
Map a JSON Web Signature algorithm to its alg value, the inverse of parsing (RFC 7515 Section 4.1.1). For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jws_sign | ( | const JWSAlgorithm | algorithm, |
| const std::string_view | signing_input, | ||
| const JWKPrivate & | key ) -> std::optional< std::string > |
Produce the raw signature bytes for a signing input under an algorithm, the signing counterpart to signature verification. Returns no value for a key whose type or curve cannot serve the algorithm, a key declaring a contradicting algorithm, or a key whose material never formed a usable private key. The signing input is the exact bytes to sign, which carry no constraint on their content. For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jws_verify_signature | ( | const std::optional< JWSAlgorithm > | algorithm, |
| const std::string_view | signing_input, | ||
| const std::string_view | signature, | ||
| const JWK & | key ) -> bool |
Verify a JSON Web Signature given its algorithm, its signing input, and its decoded signature against a JSON Web Key, returning false rather than throwing for an unrecognized algorithm, a key whose type or curve cannot serve the algorithm, a key declaring a contradicting algorithm, or a signature that does not verify. The signing input is the exact bytes the signature was computed over, which carry no constraint on their content. For example:
|
inlinenoexcept |
Reduce a caller-supplied clock skew to the grace period token validation honours, one mean Gregorian year, treating a negative tolerance as none. Every path that validates a token applies this, so the same tolerance means the same thing whichever one runs. For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jwt_check_claims | ( | const JWT & | token, |
| const std::string_view | expected_issuer, | ||
| const std::string_view | expected_audience, | ||
| const std::chrono::system_clock::time_point | now, | ||
| const JWTClockSkew | clock_skew = {}, | ||
| const std::optional< std::string_view > | expected_subject = std::nullopt ) -> std::optional< JWTClaimError > |
Validate the registered claims of a JSON Web Token against the expected issuer and audience at a given time, returning the first failing check or no value when every check passes. The expiration claim is required (RFC 9068 Section 2.2), and the subject is checked only when an expected value is supplied. For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jwt_sign | ( | const JSON & | header, |
| const JSON & | payload, | ||
| const JWKPrivate & | key ) -> std::optional< std::string > |
Build and sign a JSON Web Token in compact serialization (RFC 7519, RFC 7515) from a header and a payload, returning the base64url header, payload, and signature joined by dots. The algorithm is taken from the header algorithm parameter (RFC 7515 Section 4.1.1). Returns no value when the header or payload is not an object, the header names no supported algorithm, or the key cannot produce the signature. For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jwt_verify | ( | const JWT & | token, |
| const JWKS & | keys, | ||
| const std::span< const JWSAlgorithm > | allowed_algorithms, | ||
| const std::string_view | expected_issuer, | ||
| const std::string_view | expected_audience, | ||
| const std::chrono::system_clock::time_point | now, | ||
| const JWTClockSkew | clock_skew, | ||
| const std::optional< std::string_view > | expected_subject, | ||
| const std::optional< std::string_view > | expected_type ) -> std::optional< JWTVerificationError > |
Verify a JSON Web Token end to end against a key set, in the mandated order: the algorithm must be in the allow-list, a key is selected by its identifier or, when absent, tried against every compatible key, the signature must verify, and the claims must pass. Returns no value when the token is fully valid, or the first failing step. The type check enforces the access token profile (RFC 9068 Section 2.1) only when an expected type is supplied. For example:
| SOURCEMETA_CORE_JOSE_EXPORT auto sourcemeta::core::jwt_verify_signature | ( | const JWT & | token, |
| const JWK & | key ) -> bool |
Verify the signature of a JSON Web Token against a JSON Web Key, returning false rather than throwing whenever the token does not carry a confirmed valid signature for the key. This includes an unrecognized algorithm, a key whose type or curve cannot serve the algorithm, a key declaring a contradicting algorithm, and a signature that does not verify. For example:
|
noexcept |
Map a JSON Web Encryption alg value to its algorithm, returning no value for any unrecognized name. For example:
|
noexcept |
Map a JSON Web Encryption enc value to its algorithm, returning no value for any unrecognized name. For example:
|
noexcept |
Map a JSON Web Signature alg value to its algorithm, returning no value for any unrecognized name. For example: