CCF
Loading...
Searching...
No Matches
acme_challenge_frontend.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
8#include "ds/actors.h"
10#include "node/rpc/frontend.h"
11
12namespace ccf
13{
15 {
16 public:
20 {
21 auto handler = [this](auto& ctx) {
22 ccf::http_status response_status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
23 std::string response_body;
24
25 const auto& path_params = ctx.rpc_ctx->get_request_path_params();
26 const auto url_token_it = path_params.find("token");
27
28 if (url_token_it == path_params.end())
29 {
30 ctx.rpc_ctx->set_response_status(HTTP_STATUS_NOT_FOUND);
31 ctx.rpc_ctx->set_response_body("no token in URL");
32 }
33
34 std::string token = url_token_it->second;
35 LOG_DEBUG_FMT("ACME: challenge request for token '{}'", token);
36
37 auto tit = prepared_responses.find(token);
38 if (tit == prepared_responses.end())
39 {
40 auto prit = prepared_responses.find("");
41 if (prit != prepared_responses.end())
42 {
43 response_status = HTTP_STATUS_OK;
44 response_body = token + "." + prit->second;
45 }
46 else
47 {
48 response_status = HTTP_STATUS_NOT_FOUND;
49 response_body =
50 fmt::format("Challenge response for token '{}' not found", token);
51 }
52 }
53 else
54 {
55 response_status = HTTP_STATUS_OK;
56 response_body = token + "." + tit->second;
57 }
58
59 ctx.rpc_ctx->set_response_status(response_status);
60 ctx.rpc_ctx->set_response_body(std::move(response_body));
61 };
62
63 make_endpoint("/{token}", HTTP_GET, handler, no_auth_required)
65 .set_auto_schema<void, std::string>()
66 .install();
67 }
68
69 virtual ~ACMERpcEndpoints() = default;
70
71 void add(const std::string& token, const std::string& response)
72 {
74 "ACME: challenge server received response for token '{}' ({})",
75 token,
76 response);
77
78 prepared_responses.emplace(token, response);
79 }
80
81 void remove(const std::string& token)
82 {
83 prepared_responses.erase(token);
84 }
85
86 protected:
87 std::map<std::string, std::string> prepared_responses;
88 };
89
91 {
92 protected:
94
95 public:
97 RpcFrontend(*network.tables, endpoints, context),
98 endpoints(network, context)
99 {}
100
101 virtual ~ACMERpcFrontend() = default;
102
103 void add(const std::string& token, const std::string& response)
104 {
105 endpoints.add(token, response);
106 }
107
108 void remove(const std::string& token)
109 {
110 endpoints.remove(token);
111 }
112 };
113}
Definition acme_challenge_frontend.h:15
virtual ~ACMERpcEndpoints()=default
void add(const std::string &token, const std::string &response)
Definition acme_challenge_frontend.h:71
ACMERpcEndpoints(NetworkState &network, ccf::AbstractNodeContext &context)
Definition acme_challenge_frontend.h:17
void remove(const std::string &token)
Definition acme_challenge_frontend.h:81
std::map< std::string, std::string > prepared_responses
Definition acme_challenge_frontend.h:87
Definition acme_challenge_frontend.h:91
virtual ~ACMERpcFrontend()=default
ACMERpcEndpoints endpoints
Definition acme_challenge_frontend.h:93
void add(const std::string &token, const std::string &response)
Definition acme_challenge_frontend.h:103
ACMERpcFrontend(NetworkState &network, ccf::AbstractNodeContext &context)
Definition acme_challenge_frontend.h:96
void remove(const std::string &token)
Definition acme_challenge_frontend.h:108
ccf::AbstractNodeContext & context
Definition base_endpoint_registry.h:123
Definition common_endpoint_registry.h:16
Definition frontend.h:34
ccf::kv::Store & tables
Definition frontend.h:36
virtual Endpoint make_endpoint(const std::string &method, RESTVerb verb, const EndpointFunction &f, const AuthnPolicies &ap)
Definition endpoint_registry.cpp:204
void install(Endpoint &endpoint) override
Definition endpoint_registry.cpp:290
#define LOG_TRACE_FMT
Definition logger.h:356
#define LOG_DEBUG_FMT
Definition logger.h:357
Definition app_interface.h:14
constexpr auto get_actor_prefix(ActorsType at)
Definition actors.h:31
llhttp_status http_status
Definition http_status.h:9
ActorsType
Definition actors.h:11
Definition node_context.h:12
Definition network_state.h:12
Endpoint & set_forwarding_required(ForwardingRequired fr)
Definition endpoint.cpp:68
Endpoint & set_auto_schema(std::optional< http_status > status=std::nullopt)
Definition endpoint.h:345