105 if (!claims.
is_object())
return std::nullopt;
106 auto it = claims.
find(name);
107 if (it == claims.
end())
return std::nullopt;
109 return it->template get<T>();
a class to store JSON values
Definition json.hpp:19401
iterator end() noexcept
returns an iterator to one past the last element
Definition json.hpp:22095
constexpr bool is_object() const noexcept
return whether value is an object
Definition json.hpp:20659
iterator find(const typename object_t::key_type &key)
find an element in a JSON object
Definition json.hpp:21953
general exception of the basic_json class
Definition json.hpp:4372
routing::Middleware jwt_auth(JwtOptions options)
Verifies an Authorization: Bearer JWT and stores its claims in req.user.
Definition JwtAuth.hpp:58
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...
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....
Definition JwtAuth.hpp:104
Definition HttpRequest.hpp:19
std::function< bool(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)> Middleware
Definition Router.hpp:19
Registered claims sign() adds. Claims already present in the payload win.
Definition JwtAuth.hpp:74
std::chrono::seconds not_before
"nbf" = now + this; zero adds no "nbf"
Definition JwtAuth.hpp:76
std::string audience
"aud", if set
Definition JwtAuth.hpp:78
std::string subject
"sub", if set
Definition JwtAuth.hpp:79
std::chrono::seconds expires_in
"exp" = now + this; zero adds no "exp"
Definition JwtAuth.hpp:75
std::string issuer
"iss", if set
Definition JwtAuth.hpp:77
The key a token is signed with. Set exactly one of secret and private_key_pem.
Definition JwtAuth.hpp:64
std::string secret
Definition JwtAuth.hpp:65
std::string private_key_pem
Definition JwtAuth.hpp:69
std::string kid
If set, the "kid" header, so verifiers can pick the key (JWKS)
Definition JwtAuth.hpp:70
Verification settings for jwt_auth(). Configure at least one key: secret, public_key_pem or jwks_url ...
Definition JwtAuth.hpp:15
std::chrono::seconds jwks_min_refetch
Minimum gap between fetches (bounds load from bogus kids)
Definition JwtAuth.hpp:26
std::string audience
If set, "aud" (string or array) must contain it.
Definition JwtAuth.hpp:30
std::string public_key_pem
Definition JwtAuth.hpp:20
std::string jwks_url
Definition JwtAuth.hpp:24
std::string issuer
If set, the "iss" claim must equal it.
Definition JwtAuth.hpp:29
std::chrono::seconds jwks_timeout
Timeout for one JWKS request.
Definition JwtAuth.hpp:27
bool require_exp
Reject tokens without an "exp" claim.
Definition JwtAuth.hpp:31
std::string secret
Definition JwtAuth.hpp:16
std::chrono::seconds leeway
Clock skew tolerated for "exp" and "nbf".
Definition JwtAuth.hpp:32
std::chrono::seconds jwks_refresh
Refetch the key set at least this often.
Definition JwtAuth.hpp:25