PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
jwks_provider.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#pragma once
19
20#include <chrono>
21#include <cstdint>
22#include <functional>
23#include <mutex>
24#include <optional>
25#include <string>
26#include <string_view>
27#include <vector>
28
30
34struct jwk_key {
35 std::string kid;
36 std::string kty;
37 std::string alg;
38 std::string use;
39 std::string pem;
40};
41
49 std::function<std::optional<std::string>(const std::string& url)>;
50
69public:
71
81 bool load_from_json(std::string_view jwks_json);
82
87 void set_fetcher(jwks_fetch_callback fetcher);
88
93 void set_jwks_url(const std::string& url);
94
104 [[nodiscard]] std::optional<jwk_key> get_key(
105 std::string_view kid) const;
106
112 [[nodiscard]] std::optional<jwk_key> get_key_by_alg(
113 std::string_view alg) const;
114
119 bool refresh();
120
124 [[nodiscard]] const std::vector<jwk_key>& keys() const;
125
129 void set_cache_ttl(std::uint32_t seconds);
130
134 [[nodiscard]] bool is_cache_expired() const;
135
136private:
137 mutable std::vector<jwk_key> keys_;
139 std::string jwks_url_;
140 std::uint32_t cache_ttl_seconds_{3600};
141 mutable std::chrono::steady_clock::time_point last_fetch_{};
142 mutable std::mutex mutex_;
143
144 bool try_refresh() const;
145};
146
147} // namespace kcenon::pacs::web::auth
bool is_cache_expired() const
Check if the cache has expired.
std::chrono::steady_clock::time_point last_fetch_
bool load_from_json(std::string_view jwks_json)
Load keys from a JWKS JSON string.
const std::vector< jwk_key > & keys() const
Get all currently loaded keys.
std::optional< jwk_key > get_key(std::string_view kid) const
Get a key by Key ID (kid)
void set_jwks_url(const std::string &url)
Set the JWKS endpoint URL for fetching.
void set_fetcher(jwks_fetch_callback fetcher)
Set a callback for fetching JWKS from a URL.
bool refresh()
Force refresh keys from the configured JWKS URL.
void set_cache_ttl(std::uint32_t seconds)
Set cache TTL in seconds (default: 3600)
std::optional< jwk_key > get_key_by_alg(std::string_view alg) const
Get the first key matching the given algorithm.
std::function< std::optional< std::string >(const std::string &url)> jwks_fetch_callback
Callback type for fetching JWKS JSON from a URL.
Represents a single JSON Web Key converted to PEM format.
std::string use
Key use (sig)
std::string alg
Algorithm (RS256, ES256)
std::string pem
PEM-encoded public key.
std::string kty
Key type (RSA, EC)