Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
JwtAuth.hpp
Go to the documentation of this file.
1#pragma once
4#include <chrono>
5#include <optional>
6#include <string>
7#include <orbit/http/json.hpp>
8
9namespace orbit::middleware {
10
15struct JwtOptions {
16 std::string secret;
20 std::string public_key_pem;
24 std::string jwks_url;
25 std::chrono::seconds jwks_refresh{3600};
26 std::chrono::seconds jwks_min_refetch{30};
27 std::chrono::seconds jwks_timeout{5};
28
29 std::string issuer;
30 std::string audience;
31 bool require_exp = false;
32 std::chrono::seconds leeway{0};
33};
34
49
54routing::Middleware jwt_auth(const std::string& secret_key);
55
56} // namespace middleware
57
58namespace orbit::jwt {
59
64struct SigningKey {
65 std::string secret;
69 std::string private_key_pem;
70 std::string kid;
71};
72
75 std::chrono::seconds expires_in{0};
76 std::chrono::seconds not_before{0};
77 std::string issuer;
78 std::string audience;
79 std::string subject;
80};
81
97std::string sign(nlohmann::json claims, const SigningKey& key, const SignOptions& options = {});
98
103template <typename T>
104std::optional<T> claim(const nlohmann::json& claims, const std::string& name) {
105 if (!claims.is_object()) return std::nullopt;
106 auto it = claims.find(name);
107 if (it == claims.end()) return std::nullopt;
108 try {
109 return it->template get<T>();
110 } catch (const nlohmann::json::exception&) {
111 return std::nullopt;
112 }
113}
114
115} // namespace orbit::jwt
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