AXIAM C++ SDK 1.0.0-alpha8
Authentication, authorization, JWKS & route guards (REST + mTLS)
Loading...
Searching...
No Matches
jwks.hpp
Go to the documentation of this file.
1// JWKS fetch + Ed25519 (EdDSA) JWT *signature* verification.
2//
3// Fetches GET {base}/oauth2/jwks, caches the key set for 300s, and verifies a
4// compact JWS using OpenSSL raw Ed25519 keys. Only alg == "EdDSA" is accepted;
5// any other alg is rejected before signature work.
6//
7// !! This is an EXPERT-ONLY primitive. !!
8// It checks the signature and NOTHING ELSE — no `exp`, no `nbf`, no `iss`, no
9// `aud`, and no tenant binding. Wiring it directly into a request guard accepts
10// expired tokens and tokens minted for a different tenant.
11//
12// The supported entry point for authenticating an inbound request is
13// axiam::TokenAuthenticator in <axiam/authenticator.hpp>, which layers the
14// expiry, not-before and tenant checks on top of this primitive and fails
15// closed. Reach for JwksVerifier::verify_signature_only_unchecked() only when
16// you are deliberately implementing those checks yourself.
17#pragma once
18
19#include <chrono>
20#include <map>
21#include <mutex>
22#include <optional>
23#include <string>
24
25#include "axiam/transport.hpp"
26
27namespace axiam {
28
30struct Ed25519Jwk {
31 std::string kid;
32 std::string x_b64url; // 32-byte public key, base64url (unpadded)
33};
34
37 std::string payload_json;
38};
39
41std::optional<std::string> base64url_decode(const std::string& in);
42
44public:
48 JwksVerifier(Transport transport, std::string base_url,
49 std::chrono::seconds cache_ttl = std::chrono::seconds(300));
50
61 std::optional<VerifiedToken> verify_signature_only_unchecked(const std::string& jwt);
62
65
67 std::size_t cached_key_count();
68
69private:
70 void ensure_keys_locked();
71 void load_from_json(const std::string& body);
72
73 Transport transport_;
74 std::string base_url_;
75 std::chrono::seconds cache_ttl_;
76
77 std::mutex mtx_;
78 std::map<std::string, Ed25519Jwk> keys_; // kid -> jwk
79 std::chrono::steady_clock::time_point fetched_at_{};
80 bool have_keys_ = false;
81};
82
83} // namespace axiam
Definition jwks.hpp:43
void refresh_keys()
Force-refresh the cached key set (also called lazily by verify()).
std::size_t cached_key_count()
Test/introspection helper: number of currently-cached keys.
std::optional< VerifiedToken > verify_signature_only_unchecked(const std::string &jwt)
EXPERT PRIMITIVE — signature only.
JwksVerifier(Transport transport, std::string base_url, std::chrono::seconds cache_ttl=std::chrono::seconds(300))
Definition authenticator.hpp:40
std::function< HttpResponse(const HttpRequest &)> Transport
The transport seam. Injectable; defaults to the libcurl implementation.
Definition transport.hpp:56
std::optional< std::string > base64url_decode(const std::string &in)
Base64url decode (unpadded or padded). Returns nullopt on malformed input.
One Ed25519 (OKP) public key from the JWK set.
Definition jwks.hpp:30
std::string kid
Definition jwks.hpp:31
std::string x_b64url
Definition jwks.hpp:32
Result of a successful verification: the decoded payload (claims) JSON string.
Definition jwks.hpp:36
std::string payload_json
Definition jwks.hpp:37