userver: crypto::jwt Namespace Reference
Loading...
Searching...
No Matches
crypto::jwt Namespace Reference

Detailed Description

Signed JWTs in the three-part JWS compact serialization.

Classes

struct  JoseHeader
 JOSE header parameters for a compact signed JWT. More...
struct  Token
 Header and claims returned by crypto::jwt::Verify. More...
struct  VerificationOptions
 Claim checks performed by crypto::jwt::Verify after signature verification. More...

Enumerations

enum class  ExpirationPolicy {
  kRequired ,
  kOptional
}
 Whether verification requires an expiration claim. More...

Functions

JoseHeader Parse (const formats::json::Value &value, formats::parse::To< JoseHeader >)
 Parses the JOSE header from a JSON object.
formats::json::Value Serialize (const JoseHeader &header, formats::serialize::To< formats::json::Value >)
 Serializes a JOSE header to a JSON object, omitting absent parameters.
std::string MakeToken (const formats::json::Value &payload, const crypto::Signer &signer)
 Signs the supplied claims with typ: JWT and the signer's algorithm. Checks registered claim types before signing.
std::string MakeToken (std::string_view json_payload, const crypto::Signer &signer)
 Creates a compact token from the exact bytes of a serialized JSON payload. The header contains typ: JWT followed by the signer's alg. Any crypto::Signer is accepted, including crypto::SignerNone for unsigned tokens. The caller is responsible for validating registered claims.
std::string MakeToken (std::string_view json_payload, const crypto::Signer &signer, std::string_view key_id)
JoseHeader UnverifiedParseJoseHeader (std::string_view token)
 Reads the unverified JOSE header from a compact JWT for key selection. Checks the three-part structure and decodes only the header. The returned algorithm may be unknown. Pass the original token to crypto::jwt::Verify to check the algorithm, signature, and claims.
Token Verify (std::string_view token, const crypto::Verifier &verifier, const VerificationOptions &options={})
 Verifies a JWT with the application's trusted key and algorithm, then validates its registered claims.

Enumeration Type Documentation

◆ ExpirationPolicy

enum class crypto::jwt::ExpirationPolicy
strong

Whether verification requires an expiration claim.

Enumerator
kRequired 

Reject tokens without an exp claim.

kOptional 

Allow missing exp, but validate it whenever present.

Definition at line 55 of file jwt.hpp.

Function Documentation

◆ MakeToken() [1/3]

std::string crypto::jwt::MakeToken ( const formats::json::Value & payload,
const crypto::Signer & signer )

Signs the supplied claims with typ: JWT and the signer's algorithm. Checks registered claim types before signing.

Precondition
A custom HMAC signer produces the full MAC for its declared algorithm.
The caller supplies an HMAC key of at least 32 bytes for HS256, 48 for HS384, or 64 for HS512.
Every JSON object in payload has unique member names, including nested objects.
Exceptions
crypto::SignErrorfor an unsupported algorithm, invalid claims, or a signing failure.

◆ MakeToken() [2/3]

std::string crypto::jwt::MakeToken ( std::string_view json_payload,
const crypto::Signer & signer )

Creates a compact token from the exact bytes of a serialized JSON payload. The header contains typ: JWT followed by the signer's alg. Any crypto::Signer is accepted, including crypto::SignerNone for unsigned tokens. The caller is responsible for validating registered claims.

Precondition
json_payload contains valid JSON with unique object member names.

◆ MakeToken() [3/3]

std::string crypto::jwt::MakeToken ( std::string_view json_payload,
const crypto::Signer & signer,
std::string_view key_id )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Appends kid to the header, including when key_id is empty.

◆ UnverifiedParseJoseHeader()

JoseHeader crypto::jwt::UnverifiedParseJoseHeader ( std::string_view token)

Reads the unverified JOSE header from a compact JWT for key selection. Checks the three-part structure and decodes only the header. The returned algorithm may be unknown. Pass the original token to crypto::jwt::Verify to check the algorithm, signature, and claims.

Exceptions
crypto::VerificationErrorfor malformed compact structure, invalid header parameters, unsupported critical or unencoded-payload extensions, or a nested JWT content type.

◆ Verify()

Token crypto::jwt::Verify ( std::string_view token,
const crypto::Verifier & verifier,
const VerificationOptions & options = {} )

Verifies a JWT with the application's trusted key and algorithm, then validates its registered claims.

Precondition
A custom HMAC verifier checks the full MAC, including its length.
The caller supplies an HMAC key meeting the minimum length documented in crypto::jwt::MakeToken.
Exceptions
crypto::VerificationErrorif parsing, signature verification, or claim checks fail.
std::invalid_argumentif options contains a negative leeway.