cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
http_jwks_endpoint.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <functional>
6#include <string>
7#include <utility>
8
10#include "cxxmcp/auth/jwks.hpp"
11#include "cxxmcp/auth/types.hpp"
13#include "nlohmann/json.hpp"
14
17
18namespace mcp::auth {
19
24class HttpJwksEndpoint final : public JwksEndpoint {
25 public:
26 explicit HttpJwksEndpoint(OAuthHttpGet get) : get_(std::move(get)) {}
27
29 const JwksFetchRequest& request) override {
30 if (request.jwks_uri.empty()) {
31 return core::unexpected(
32 core::Error{static_cast<int>(OAuthErrorCode::kInvalidRequest),
33 "JWKS URI is required",
34 {},
35 std::string(AuthErrorCategory)});
36 }
37
38 MetadataFetchRequest fetch_request;
39 fetch_request.url = request.jwks_uri;
40 fetch_request.headers = request.headers;
41
42 auto response = get_(fetch_request);
43 if (!response.has_value()) {
44 return core::unexpected(response.error());
45 }
46 if (response->status_code < 200 || response->status_code >= 300) {
47 return core::unexpected(core::Error{
48 static_cast<int>(OAuthErrorCode::kMetadataDiscoveryFailed),
49 "JWKS fetch failed with HTTP " +
50 std::to_string(response->status_code),
51 {},
52 std::string(AuthErrorCategory)});
53 }
54
55 nlohmann::json parsed;
56 try {
57 parsed = nlohmann::json::parse(response->body);
58 } catch (const nlohmann::json::parse_error& ex) {
59 return core::unexpected(core::Error{
60 static_cast<int>(OAuthErrorCode::kMetadataDiscoveryFailed),
61 "JWKS response is not valid JSON", ex.what(),
62 std::string(AuthErrorCategory)});
63 }
64
65 return parse_json_web_key_set(parsed);
66 }
67
68 private:
69 OAuthHttpGet get_;
70};
71
72} // namespace mcp::auth
Shared lightweight value types for cxxmcp auth contracts.
Concrete implementation of JwksEndpoint that fetches JWKS over HTTP.
Definition http_jwks_endpoint.hpp:24
Application-provided JWKS retrieval boundary.
Definition jwks.hpp:60
Default HTTP metadata endpoint parser for OAuth discovery.
std::function< core::Result< OAuthHttpResponse >(const MetadataFetchRequest &)> OAuthHttpGet
Application or SDK transport adapter used for metadata GET calls.
Definition http_metadata_endpoint.hpp:27
JWKS value models, parsing, fetch, and cache contracts.
core::Result< JsonWebKeySet > parse_json_web_key_set(const nlohmann::json &value)
Parse a public JWKS document from JSON.
Definition jwks.hpp:211
Shared result and error primitives used by the public cxxmcp SDK.
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
Request for retrieving a JWKS document.
Definition jwks.hpp:54
Metadata fetch request routed through application/transport code.
Definition lifecycle.hpp:342
Structured error returned by fallible SDK operations.
Definition result.hpp:35