summaryrefslogtreecommitdiffstats
path: root/server/src/rwgps_webhook.cppm
diff options
context:
space:
mode:
authorRutger Broekhoff2026-11-30 21:27:55 +0100
committerRutger Broekhoff2026-11-30 21:27:55 +0100
commit7bba7176194c4d1d9c7066f8eda7a94491d924e6 (patch)
treea9a171e8608786e09d6342729ee13b8471c0c9c2 /server/src/rwgps_webhook.cppm
parent02475e05ccd1aa15c6a6163834f28bfebbef796f (diff)
downloadroutemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz
routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip
Cleanups and updates
Diffstat (limited to 'server/src/rwgps_webhook.cppm')
-rw-r--r--server/src/rwgps_webhook.cppm237
1 files changed, 0 insertions, 237 deletions
diff --git a/server/src/rwgps_webhook.cppm b/server/src/rwgps_webhook.cppm
deleted file mode 100644
index 5a1df37..0000000
--- a/server/src/rwgps_webhook.cppm
+++ /dev/null
@@ -1,237 +0,0 @@
1module;
2
3#include <boost/asio/ssl/error.hpp>
4#include <boost/beast/core.hpp>
5#include <boost/beast/http.hpp>
6#include <boost/json/stream_parser.hpp>
7
8#include <openssl/err.h>
9#include <openssl/evp.h>
10#include <openssl/params.h>
11
12export module routemon:rwgps.webhook;
13
14import :http.common;
15import :util;
16import std;
17
18namespace {
19
20namespace net = boost::asio;
21namespace json = boost::json;
22
23} // namespace
24
25namespace routemon::rwgps::webhook {
26
27// TODO: constrain T? What are appropriate constraints?
28template <typename T>
29class hmac_readable
30{
31 std::string_view key_;
32 std::optional<std::array<std::uint8_t, 32>> mac_;
33 T res_;
34
35 template <bhttp::concepts::body_reader Sub>
36 friend struct hmac_body_reader;
37
38public:
39 template <typename... Args>
40 explicit hmac_readable(std::string_view key, Args&&... args)
41 : key_{key}, res_{std::forward<Args>(args)...}
42 {
43 }
44
45 template <class Self>
46 constexpr auto get(this Self&& self) -> auto&&
47 {
48 return std::forward<Self>(self).res_;
49 }
50
51 auto mac() const -> std::optional<std::array<std::uint8_t, 32>>
52 {
53 return mac_;
54 }
55};
56
57template <bhttp::concepts::body_reader Sub>
58class hmac_body_reader
59{
60 EVP_MAC* mac_ = nullptr;
61 EVP_MAC_CTX* ctx_ = nullptr;
62
63 util::not_null<hmac_readable<typename Sub::value_type>*> v_;
64 Sub::reader subr_;
65 beast::error_code ec_;
66
67public:
68 template <bool isRequest, bhttp::concepts::fields Fields>
69 explicit hmac_body_reader(
70 bhttp::header<isRequest, Fields>& h,
71 hmac_readable<typename Sub::value_type>& v)
72 : v_{util::not_null{&v}},
73 subr_{h, v.get()} // we are not supposed to use v yet, but this just
74 // computes an address, so it should be fine
75 {
76 }
77
78 ~hmac_body_reader()
79 {
80 ::EVP_MAC_CTX_free(ctx_);
81 ::EVP_MAC_free(mac_);
82 }
83
84 // TODO: move to impl. unit.
85 auto init(boost::optional<std::uint64_t> n, beast::error_code& ec) noexcept
86 -> void
87 {
88 // https://docs.openssl.org/master/man3/EVP_MAC/#examples
89 mac_ = ::EVP_MAC_fetch(nullptr, "HMAC", nullptr);
90 if (!mac_)
91 {
92 ec_ = ec = make_error_code(std::errc::not_enough_memory);
93 return;
94 }
95
96 ctx_ = ::EVP_MAC_CTX_new(mac_);
97 if (!ctx_)
98 {
99 ec_ = ec = make_error_code(std::errc::not_enough_memory);
100 return;
101 }
102
103 char sha256[] = "SHA256";
104 ::OSSL_PARAM const params[] = {
105 OSSL_PARAM_utf8_string("digest", sha256, sizeof(sha256) - 1),
106 OSSL_PARAM_END
107 };
108 if (!::EVP_MAC_init(
109 ctx_, reinterpret_cast<unsigned char const*>(v_->key_.data()),
110 v_->key_.size(), params))
111 {
112 ec_ = ec = beast::error_code{
113 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
114 };
115 return;
116 }
117
118 subr_.init(n, ec);
119 }
120
121 auto
122 put(beast::concepts::const_buffer_sequence auto b,
123 beast::error_code& ec) noexcept -> std::size_t
124 {
125 if ((ec = ec_))
126 return 0uz;
127
128 auto const total_read = subr_.put(b, ec);
129 if (total_read > 0)
130 {
131 auto remaining = total_read;
132 for (auto const buf : beast::buffers_range(b))
133 {
134 auto const to_hash_from_this_buf = std::min(remaining, buf.size());
135 if (!::EVP_MAC_update(
136 ctx_, static_cast<unsigned char const*>(buf.data()),
137 to_hash_from_this_buf))
138 {
139 ec_ = ec = beast::error_code{
140 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
141 };
142 return total_read;
143 }
144 remaining -= to_hash_from_this_buf;
145 }
146 }
147 return total_read;
148 }
149
150 auto finish(beast::error_code& ec) noexcept
151 {
152 auto got_mac_len = 0uz;
153 auto got_mac = std::array<std::uint8_t, 32>{};
154 if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size()))
155 {
156 ec = beast::error_code{
157 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
158 };
159 return;
160 }
161 assert(got_mac_len == got_mac.size());
162 v_->mac_ = got_mac;
163 subr_.finish(ec);
164 }
165};
166
167template <bhttp::concepts::body Sub>
168struct hmac_body
169{
170 using value_type = hmac_readable<typename Sub::value_type>;
171};
172
173template <bhttp::concepts::body Sub>
174 requires bhttp::concepts::body_reader<Sub>
175struct hmac_body<Sub>
176{
177 using value_type = hmac_readable<typename Sub::value_type>;
178
179 using reader = hmac_body_reader<Sub>;
180};
181
182struct json_body
183{
184 using value_type = json::value;
185
186 class reader
187 {
188 json::stream_parser p_;
189 util::not_null<value_type*> v_;
190
191 public:
192 template <bool isRequest, bhttp::concepts::fields Fields>
193 explicit reader(bhttp::header<isRequest, Fields>&, value_type& v)
194 : v_{util::not_null{&v}}
195 {
196 }
197
198 // TODO: move to impl. unit.
199 auto
200 init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept
201 -> void
202 {
203 p_.reset();
204 ec = {};
205 }
206
207 auto
208 put(beast::concepts::const_buffer_sequence auto b,
209 beast::error_code& ec) noexcept -> std::size_t
210 {
211 ec = {};
212
213 auto wrote = 0uz;
214 for (auto const buf : beast::buffers_range(b))
215 {
216 p_.write(static_cast<char const*>(buf.data()), buf.size(), ec);
217 wrote += buf.size();
218 if (ec)
219 break;
220 }
221 return wrote;
222 }
223
224 auto finish(beast::error_code& ec) noexcept
225 {
226 p_.finish(ec);
227 if (ec)
228 return;
229 *v_ = p_.release();
230 }
231 };
232};
233
234static_assert(bhttp::concepts::body<hmac_body<json_body>>);
235static_assert(bhttp::concepts::body_reader<hmac_body<json_body>>);
236
237} // namespace routemon::rwgps::webhook