diff options
Diffstat (limited to 'server/src/rwgps_webhook.cppm')
| -rw-r--r-- | server/src/rwgps_webhook.cppm | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/server/src/rwgps_webhook.cppm b/server/src/rwgps_webhook.cppm new file mode 100644 index 0000000..5a1df37 --- /dev/null +++ b/server/src/rwgps_webhook.cppm | |||
| @@ -0,0 +1,237 @@ | |||
| 1 | module; | ||
| 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 | |||
| 12 | export module routemon:rwgps.webhook; | ||
| 13 | |||
| 14 | import :http.common; | ||
| 15 | import :util; | ||
| 16 | import std; | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | namespace net = boost::asio; | ||
| 21 | namespace json = boost::json; | ||
| 22 | |||
| 23 | } // namespace | ||
| 24 | |||
| 25 | namespace routemon::rwgps::webhook { | ||
| 26 | |||
| 27 | // TODO: constrain T? What are appropriate constraints? | ||
| 28 | template <typename T> | ||
| 29 | class 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 | |||
| 38 | public: | ||
| 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 | |||
| 57 | template <bhttp::concepts::body_reader Sub> | ||
| 58 | class 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 | |||
| 67 | public: | ||
| 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 | |||
| 167 | template <bhttp::concepts::body Sub> | ||
| 168 | struct hmac_body | ||
| 169 | { | ||
| 170 | using value_type = hmac_readable<typename Sub::value_type>; | ||
| 171 | }; | ||
| 172 | |||
| 173 | template <bhttp::concepts::body Sub> | ||
| 174 | requires bhttp::concepts::body_reader<Sub> | ||
| 175 | struct hmac_body<Sub> | ||
| 176 | { | ||
| 177 | using value_type = hmac_readable<typename Sub::value_type>; | ||
| 178 | |||
| 179 | using reader = hmac_body_reader<Sub>; | ||
| 180 | }; | ||
| 181 | |||
| 182 | struct 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 | |||
| 234 | static_assert(bhttp::concepts::body<hmac_body<json_body>>); | ||
| 235 | static_assert(bhttp::concepts::body_reader<hmac_body<json_body>>); | ||
| 236 | |||
| 237 | } // namespace routemon::rwgps::webhook | ||