Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::jwt Namespace Reference

Classes

struct  SigningKey
 The key a token is signed with. Set exactly one of secret and private_key_pem. More...
 
struct  SignOptions
 Registered claims sign() adds. Claims already present in the payload win. More...
 

Functions

std::string sign (nlohmann::json claims, const SigningKey &key, const SignOptions &options={})
 Issues a signed JWT (JWS compact serialisation) that jwt_auth() with the matching secret or public key accepts.
 
template<typename T >
std::optional< T > claim (const nlohmann::json &claims, const std::string &name)
 A claim from verified claims (req.user), if present with that type: jwt::claim<std::string>(req.user, "sub"), claim<int64_t>(req.user, "exp").
 

Function Documentation

◆ claim()

template<typename T >
std::optional< T > orbit::jwt::claim ( const nlohmann::json &  claims,
const std::string &  name 
)

A claim from verified claims (req.user), if present with that type: jwt::claim<std::string>(req.user, "sub"), claim<int64_t>(req.user, "exp").

◆ sign()

std::string orbit::jwt::sign ( nlohmann::json  claims,
const SigningKey &  key,
const SignOptions &  options = {} 
)

Issues a signed JWT (JWS compact serialisation) that jwt_auth() with the matching secret or public key accepts.

"iat" is set to the current time unless claims has one; see SignOptions for the others. claims may be null ({}) for none.

auto token = orbit::jwt::sign({{"role", "admin"}}, {.secret = secret},
{.expires_in = std::chrono::hours(1), .subject = user_id});
std::string sign(nlohmann::json claims, const SigningKey &key, const SignOptions &options={})
Issues a signed JWT (JWS compact serialisation) that jwt_auth() with the matching secret or public ke...
Exceptions
std::invalid_argumentif claims is not an object, or the key is missing, ambiguous or unusable.