JWTXX
C++ library for JWT
Loading...
Searching...
No Matches
JWTXX::JWT Class Reference

Main class to work with JWT. More...

#include <jwt.h>

Classes

class  Error
 JWT-specific exception. More...
 
class  ParseError
 Indicates problems with JWT structure. More...
 
class  ValidationError
 Indicates problems with JWT validation (signature and claims). More...
 

Public Member Functions

 JWT (const std::string &token, Key key, Validators validators={Validate::exp()})
 Constructs a JWT from a token.
 
 JWT (Algorithm alg, Value::Object claims, Value::Object header=Value::Object{}) noexcept
 Constructs a JWT from scratch.
 
Algorithm alg () const noexcept
 Returns an algorithm.
 
const Value::Objectclaims () const noexcept
 Returns a list of claims.
 
const Value::Objectheader () const noexcept
 Returns a list of header fields.
 
Value claim (const std::string &name) const noexcept
 Returns a value of a specific claim.
 
std::string token (const std::string &keyData, const Key::PasswordCallback &cb=Key::noPasswordCallback) const
 Returns a signed token.
 
std::string token (const Key &key) const
 Returns a signed token using a pre-constructed key.
 

Static Public Member Functions

static JWT parse (const std::string &token)
 Returns a JWT for a token without validation.
 
static ValidationResult verify (const std::string &token, Key key, Validators validators={Validate::exp()}) noexcept
 Validates a token without constructing a JWT.
 

Detailed Description

Main class to work with JWT.

Constructor & Destructor Documentation

◆ JWT() [1/2]

JWTXX::JWT::JWT ( const std::string & token,
Key key,
Validators validators = {Validate::exp()} )

Constructs a JWT from a token.

Parameters
tokenthe token;
keykey to use for signatire verification;
validatorsan optional list of validators; validates 'exp' by default.

◆ JWT() [2/2]

JWTXX::JWT::JWT ( Algorithm alg,
Value::Object claims,
Value::Object header = Value::Object{} )
noexcept

Constructs a JWT from scratch.

Parameters
algsignature algorithm;
claimsa list of claims;
headeran optional list of header records; 'alg' and 'typ' can't be specified manually.

Member Function Documentation

◆ claim()

Value JWTXX::JWT::claim ( const std::string & name) const
noexcept

Returns a value of a specific claim.

Parameters
nameclaim name.
Note
Returns an empty string if the claim is missing.

◆ parse()

static JWT JWTXX::JWT::parse ( const std::string & token)
static

Returns a JWT for a token without validation.

Parameters
tokenthe token.

◆ token() [1/2]

std::string JWTXX::JWT::token ( const Key & key) const

Returns a signed token using a pre-constructed key.

Parameters
keycryptographic key to use for signing.
Returns
signed JWT token string.
Note
This overload allows key reuse for better performance when generating multiple tokens.
The key algorithm must match the algorithm specified in this JWT.

◆ token() [2/2]

std::string JWTXX::JWT::token ( const std::string & keyData,
const Key::PasswordCallback & cb = Key::noPasswordCallback ) const

Returns a signed token.

Parameters
keyDatakey-specific data;
cbpassword callback for password-protected keys.
Note
Automatically constructs key using the algorithm specified in this JWT.

◆ verify()

static ValidationResult JWTXX::JWT::verify ( const std::string & token,
Key key,
Validators validators = {Validate::exp()} )
staticnoexcept

Validates a token without constructing a JWT.

Parameters
tokenthe token;
keykey to use for signatire verification;
validatorsan optional list of validators; validates 'exp' by default.

The documentation for this class was generated from the following file: