From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/rwgps_webhook.cppm | 237 ------------------------------------------ 1 file changed, 237 deletions(-) delete mode 100644 server/src/rwgps_webhook.cppm (limited to 'server/src/rwgps_webhook.cppm') 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 @@ -module; - -#include -#include -#include -#include - -#include -#include -#include - -export module routemon:rwgps.webhook; - -import :http.common; -import :util; -import std; - -namespace { - -namespace net = boost::asio; -namespace json = boost::json; - -} // namespace - -namespace routemon::rwgps::webhook { - -// TODO: constrain T? What are appropriate constraints? -template -class hmac_readable -{ - std::string_view key_; - std::optional> mac_; - T res_; - - template - friend struct hmac_body_reader; - -public: - template - explicit hmac_readable(std::string_view key, Args&&... args) - : key_{key}, res_{std::forward(args)...} - { - } - - template - constexpr auto get(this Self&& self) -> auto&& - { - return std::forward(self).res_; - } - - auto mac() const -> std::optional> - { - return mac_; - } -}; - -template -class hmac_body_reader -{ - EVP_MAC* mac_ = nullptr; - EVP_MAC_CTX* ctx_ = nullptr; - - util::not_null*> v_; - Sub::reader subr_; - beast::error_code ec_; - -public: - template - explicit hmac_body_reader( - bhttp::header& h, - hmac_readable& v) - : v_{util::not_null{&v}}, - subr_{h, v.get()} // we are not supposed to use v yet, but this just - // computes an address, so it should be fine - { - } - - ~hmac_body_reader() - { - ::EVP_MAC_CTX_free(ctx_); - ::EVP_MAC_free(mac_); - } - - // TODO: move to impl. unit. - auto init(boost::optional n, beast::error_code& ec) noexcept - -> void - { - // https://docs.openssl.org/master/man3/EVP_MAC/#examples - mac_ = ::EVP_MAC_fetch(nullptr, "HMAC", nullptr); - if (!mac_) - { - ec_ = ec = make_error_code(std::errc::not_enough_memory); - return; - } - - ctx_ = ::EVP_MAC_CTX_new(mac_); - if (!ctx_) - { - ec_ = ec = make_error_code(std::errc::not_enough_memory); - return; - } - - char sha256[] = "SHA256"; - ::OSSL_PARAM const params[] = { - OSSL_PARAM_utf8_string("digest", sha256, sizeof(sha256) - 1), - OSSL_PARAM_END - }; - if (!::EVP_MAC_init( - ctx_, reinterpret_cast(v_->key_.data()), - v_->key_.size(), params)) - { - ec_ = ec = beast::error_code{ - static_cast(::ERR_get_error()), net::error::get_ssl_category() - }; - return; - } - - subr_.init(n, ec); - } - - auto - put(beast::concepts::const_buffer_sequence auto b, - beast::error_code& ec) noexcept -> std::size_t - { - if ((ec = ec_)) - return 0uz; - - auto const total_read = subr_.put(b, ec); - if (total_read > 0) - { - auto remaining = total_read; - for (auto const buf : beast::buffers_range(b)) - { - auto const to_hash_from_this_buf = std::min(remaining, buf.size()); - if (!::EVP_MAC_update( - ctx_, static_cast(buf.data()), - to_hash_from_this_buf)) - { - ec_ = ec = beast::error_code{ - static_cast(::ERR_get_error()), net::error::get_ssl_category() - }; - return total_read; - } - remaining -= to_hash_from_this_buf; - } - } - return total_read; - } - - auto finish(beast::error_code& ec) noexcept - { - auto got_mac_len = 0uz; - auto got_mac = std::array{}; - if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size())) - { - ec = beast::error_code{ - static_cast(::ERR_get_error()), net::error::get_ssl_category() - }; - return; - } - assert(got_mac_len == got_mac.size()); - v_->mac_ = got_mac; - subr_.finish(ec); - } -}; - -template -struct hmac_body -{ - using value_type = hmac_readable; -}; - -template - requires bhttp::concepts::body_reader -struct hmac_body -{ - using value_type = hmac_readable; - - using reader = hmac_body_reader; -}; - -struct json_body -{ - using value_type = json::value; - - class reader - { - json::stream_parser p_; - util::not_null v_; - - public: - template - explicit reader(bhttp::header&, value_type& v) - : v_{util::not_null{&v}} - { - } - - // TODO: move to impl. unit. - auto - init(boost::optional /* n */, beast::error_code& ec) noexcept - -> void - { - p_.reset(); - ec = {}; - } - - auto - put(beast::concepts::const_buffer_sequence auto b, - beast::error_code& ec) noexcept -> std::size_t - { - ec = {}; - - auto wrote = 0uz; - for (auto const buf : beast::buffers_range(b)) - { - p_.write(static_cast(buf.data()), buf.size(), ec); - wrote += buf.size(); - if (ec) - break; - } - return wrote; - } - - auto finish(beast::error_code& ec) noexcept - { - p_.finish(ec); - if (ec) - return; - *v_ = p_.release(); - } - }; -}; - -static_assert(bhttp::concepts::body>); -static_assert(bhttp::concepts::body_reader>); - -} // namespace routemon::rwgps::webhook -- cgit v1.3.1