diff options
Diffstat (limited to 'server/src/srv.cpp')
| -rw-r--r-- | server/src/srv.cpp | 145 |
1 files changed, 128 insertions, 17 deletions
diff --git a/server/src/srv.cpp b/server/src/srv.cpp index 5de0fc5..287f838 100644 --- a/server/src/srv.cpp +++ b/server/src/srv.cpp | |||
| @@ -13,11 +13,14 @@ module; | |||
| 13 | 13 | ||
| 14 | #include <expat.h> | 14 | #include <expat.h> |
| 15 | 15 | ||
| 16 | #include <openssl/crypto.h> | ||
| 17 | |||
| 16 | module routemon:srv$impl; | 18 | module routemon:srv$impl; |
| 17 | 19 | ||
| 18 | import std; | 20 | import std; |
| 19 | import :gpx; | 21 | import :gpx; |
| 20 | import :srv; | 22 | import :srv; |
| 23 | import :rwgps.webhook; | ||
| 21 | 24 | ||
| 22 | namespace beast = boost::beast; | 25 | namespace beast = boost::beast; |
| 23 | namespace chrono = std::chrono; | 26 | namespace chrono = std::chrono; |
| @@ -330,9 +333,10 @@ struct raw_deflate_body | |||
| 330 | }; | 333 | }; |
| 331 | }; | 334 | }; |
| 332 | 335 | ||
| 333 | auto parse_timestamp(std::string_view in) -> std::optional<time::timestamp> | 336 | auto parse_timestamp_mins(std::string_view in) |
| 337 | -> std::optional<time::timestamp<std::chrono::minutes>> | ||
| 334 | { | 338 | { |
| 335 | auto res = time::timestamp{}; | 339 | auto res = time::timestamp<std::chrono::minutes>{}; |
| 336 | auto is = std::istringstream{in}; | 340 | auto is = std::istringstream{in}; |
| 337 | is >> std::chrono::parse("%Y-%m-%dT%H:%MZ", res); | 341 | is >> std::chrono::parse("%Y-%m-%dT%H:%MZ", res); |
| 338 | return is.fail() ? std::nullopt : std::make_optional(res); | 342 | return is.fail() ? std::nullopt : std::make_optional(res); |
| @@ -344,13 +348,13 @@ auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) | |||
| 344 | auto l = | 348 | auto l = |
| 345 | l_.sub("handle_process_gpx").with("trace_id", ctx.trace_id.as_string()); | 349 | l_.sub("handle_process_gpx").with("trace_id", ctx.trace_id.as_string()); |
| 346 | 350 | ||
| 347 | auto cp_begin = time::timestamp{}; | 351 | auto cp_begin = time::timestamp<std::chrono::minutes>{}; |
| 348 | auto cp_end = time::timestamp{}; | 352 | auto cp_end = time::timestamp<std::chrono::minutes>{}; |
| 349 | 353 | ||
| 350 | auto const params = ctx.target.params(); | 354 | auto const params = ctx.target.params(); |
| 351 | if (auto it = params.find("cpbegin"); it != params.end()) | 355 | if (auto it = params.find("cpbegin"); it != params.end()) |
| 352 | { | 356 | { |
| 353 | auto mts = parse_timestamp((*it).value); | 357 | auto mts = parse_timestamp_mins((*it).value); |
| 354 | if (!mts) | 358 | if (!mts) |
| 355 | { | 359 | { |
| 356 | auto tpl = problem::tpl{ | 360 | auto tpl = problem::tpl{ |
| @@ -390,7 +394,7 @@ auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) | |||
| 390 | } | 394 | } |
| 391 | if (auto it = params.find("cpend"); it != params.end()) | 395 | if (auto it = params.find("cpend"); it != params.end()) |
| 392 | { | 396 | { |
| 393 | auto mts = parse_timestamp((*it).value); | 397 | auto mts = parse_timestamp_mins((*it).value); |
| 394 | if (!mts) | 398 | if (!mts) |
| 395 | { | 399 | { |
| 396 | auto tpl = problem::tpl{ | 400 | auto tpl = problem::tpl{ |
| @@ -442,7 +446,7 @@ auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) | |||
| 442 | "Begin of check period must be before end of check period")), | 446 | "Begin of check period must be before end of check period")), |
| 443 | http::keep_alive{false}); | 447 | http::keep_alive{false}); |
| 444 | } | 448 | } |
| 445 | auto cp = time::period{cp_begin, cp_end}; | 449 | auto cp = time::period<std::chrono::minutes>{cp_begin, cp_end}; |
| 446 | 450 | ||
| 447 | auto gpx_file = gpx::file{}; | 451 | auto gpx_file = gpx::file{}; |
| 448 | try | 452 | try |
| @@ -487,7 +491,8 @@ auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r) | |||
| 487 | // TODO: catch handler exceptions and return 500 when raised? | 491 | // TODO: catch handler exceptions and return 500 when raised? |
| 488 | // (keep-alive depends on whether whole request was read) | 492 | // (keep-alive depends on whether whole request was read) |
| 489 | auto mres = inner_.process_gpx( | 493 | auto mres = inner_.process_gpx( |
| 490 | ctx.trace_id, std::move(gpx_file), time::period_seq{cp}); | 494 | ctx.trace_id, std::move(gpx_file), |
| 495 | time::period_seq<std::chrono::minutes>{cp}); | ||
| 491 | if (!mres) | 496 | if (!mres) |
| 492 | { | 497 | { |
| 493 | auto tpl = problem::tpl{ | 498 | auto tpl = problem::tpl{ |
| @@ -521,8 +526,109 @@ auto handler::handle_sysinfo(l0_ctx ctx, http::readable_request r) | |||
| 521 | co_return rsp; | 526 | co_return rsp; |
| 522 | } | 527 | } |
| 523 | 528 | ||
| 524 | handler::handler(log::logger const& l, api::handler&& inner) | 529 | auto decode_hexnib(char c) -> std::optional<std::uint8_t> |
| 525 | : l_{l.sub("handler")}, inner_{std::move(inner)} | 530 | { |
| 531 | if ('a' <= c && c <= 'f') | ||
| 532 | return c - 'a' + 10; | ||
| 533 | if ('A' <= c && c <= 'F') | ||
| 534 | return c - 'A' + 10; | ||
| 535 | if ('0' <= c && c <= '9') | ||
| 536 | return c - '0'; | ||
| 537 | return std::nullopt; | ||
| 538 | } | ||
| 539 | |||
| 540 | auto parse_timestamp_secs(std::string_view in) | ||
| 541 | -> std::optional<time::timestamp<std::chrono::seconds>> | ||
| 542 | { | ||
| 543 | auto res = time::timestamp<std::chrono::seconds>{}; | ||
| 544 | auto is = std::istringstream{in}; | ||
| 545 | is >> std::chrono::parse("%FT%TZ", res); | ||
| 546 | return is.fail() ? std::nullopt : std::make_optional(res); | ||
| 547 | } | ||
| 548 | |||
| 549 | auto handler::handle_rwgps_webhook(l0_ctx ctx, http::readable_request r) | ||
| 550 | -> net::awaitable<http::presponse> | ||
| 551 | { | ||
| 552 | if (r.p->get()[bhttp::field::content_type] != "application/json") | ||
| 553 | { | ||
| 554 | // TODO: 415 Unsupported Media Type | ||
| 555 | // (respond Accept-Post: application/json) | ||
| 556 | throw std::runtime_error{"unhandled: unsupported media type provided"}; | ||
| 557 | } | ||
| 558 | |||
| 559 | auto mrwgps_ts = parse_timestamp_secs(r.p->get()["x-rwgps-datetime"]); | ||
| 560 | if (!mrwgps_ts) | ||
| 561 | { | ||
| 562 | // TODO: 400 Bad Request | ||
| 563 | throw std::runtime_error{ | ||
| 564 | "failed to parse ISO 8601 date/time in x-rwgps-datetime header" | ||
| 565 | }; | ||
| 566 | } | ||
| 567 | |||
| 568 | // TODO: better to instead copy the expected API key to a buffer of | ||
| 569 | // (at least) the size of the input API key and to compare those two | ||
| 570 | // buffers? Then this operation should be Ω/Θ(API key provided in | ||
| 571 | // request) | ||
| 572 | auto req_api_key = r.p->get()["x-rwgps-api-key"]; | ||
| 573 | auto req_api_key_copy = std::string(rwgps_creds_->api_key().size(), 'X'); | ||
| 574 | std::memcpy( | ||
| 575 | req_api_key_copy.data(), req_api_key.data(), | ||
| 576 | std::min(req_api_key.size(), rwgps_creds_->api_key().size())); | ||
| 577 | |||
| 578 | if (CRYPTO_memcmp( | ||
| 579 | req_api_key_copy.data(), rwgps_creds_->api_key().data(), | ||
| 580 | rwgps_creds_->api_key().size())) | ||
| 581 | { | ||
| 582 | // TODO: Unauthorized | ||
| 583 | // TODO: constant-time comparison | ||
| 584 | throw std::runtime_error{"unhandled: mismatching API key provided"}; | ||
| 585 | } | ||
| 586 | |||
| 587 | auto rwgps_sig = r.p->get()["x-rwgps-signature"]; | ||
| 588 | auto rwgps_sig_decoded = std::array<std::uint8_t, 32>{}; | ||
| 589 | |||
| 590 | if (rwgps_sig.length() != 2 * rwgps_sig_decoded.size()) | ||
| 591 | { | ||
| 592 | // TODO: Bad request | ||
| 593 | throw std::runtime_error{"unhandled: bad signature provided"}; | ||
| 594 | } | ||
| 595 | |||
| 596 | for (auto [i, b] : rwgps_sig_decoded | std::views::enumerate) | ||
| 597 | { | ||
| 598 | auto mhi = decode_hexnib(rwgps_sig[static_cast<std::size_t>(i) * 2]); | ||
| 599 | auto mlo = decode_hexnib(rwgps_sig[static_cast<std::size_t>(i) * 2 + 1]); | ||
| 600 | if (!mhi || !mlo) | ||
| 601 | { | ||
| 602 | throw std::runtime_error{"TODO: INVALID SIGNATURE"}; | ||
| 603 | } | ||
| 604 | static_assert(std::same_as<decltype(b), std::uint8_t&>); | ||
| 605 | b = static_cast<std::uint8_t>((*mhi << 4) | *mlo); | ||
| 606 | } | ||
| 607 | |||
| 608 | auto req = co_await http:: | ||
| 609 | read_request<rwgps::webhook::hmac_body<rwgps::webhook::json_body>>( | ||
| 610 | ctx, std::move(r), rwgps_creds_->api_secret()); | ||
| 611 | |||
| 612 | if (req.body().mac() != rwgps_sig_decoded) | ||
| 613 | { | ||
| 614 | throw std::runtime_error{"unhandled: incorrect signature provided"}; | ||
| 615 | } | ||
| 616 | |||
| 617 | // TODO: handle conversion failure gracefully | ||
| 618 | inner_.rwgps_webhook( | ||
| 619 | *mrwgps_ts, json::value_to<rwgps::webhook_request>(req.body().get())); | ||
| 620 | |||
| 621 | auto rsp = http::make_rsp<bhttp::empty_body>( | ||
| 622 | bhttp::status::ok, http::keep_alive{true}); | ||
| 623 | rsp.prepare_payload(); | ||
| 624 | co_return rsp; | ||
| 625 | } | ||
| 626 | |||
| 627 | handler::handler( | ||
| 628 | log::logger const& l, api::handler&& inner, | ||
| 629 | std::shared_ptr<rwgps::credentials> rwgps_creds) | ||
| 630 | : l_{l.sub("handler")}, inner_{std::move(inner)}, | ||
| 631 | rwgps_creds_{std::move(rwgps_creds)} | ||
| 526 | { | 632 | { |
| 527 | } | 633 | } |
| 528 | 634 | ||
| @@ -536,11 +642,15 @@ auto handler::make_routes() -> http::route_tree<http::routed_ctx<outer_ctx>> | |||
| 536 | .post = handler(&handler::handle_process_gpx), | 642 | .post = handler(&handler::handle_process_gpx), |
| 537 | }} | 643 | }} |
| 538 | .no_subtrees()}, | 644 | .no_subtrees()}, |
| 539 | {"sysinfo", http::dtree<l0_ctx>{ | 645 | {"sysinfo", http::dtree<l0_ctx>{{ |
| 540 | { | 646 | .get = handler(&handler::handle_sysinfo), |
| 541 | .get = handler(&handler::handle_sysinfo), | 647 | }} |
| 542 | } | 648 | .no_subtrees()}, |
| 543 | }.no_subtrees()}, | 649 | {"webhook", http::dtree<l0_ctx>{}.named_subtrees({ |
| 650 | {"rwgps", http::dtree<l0_ctx>{ | ||
| 651 | {.post = handler(&handler::handle_rwgps_webhook)} | ||
| 652 | }.no_subtrees()}, | ||
| 653 | })}, | ||
| 544 | }); | 654 | }); |
| 545 | } | 655 | } |
| 546 | 656 | ||
| @@ -557,8 +667,9 @@ auto server::make_global_middleware(config::http_server const& cfg) | |||
| 557 | 667 | ||
| 558 | server::server( | 668 | server::server( |
| 559 | log::logger const& l, config::http_server const& cfg, | 669 | log::logger const& l, config::http_server const& cfg, |
| 560 | locale::selector&& lsel, api::handler&& inner) | 670 | std::shared_ptr<rwgps::credentials> rwgps_creds, locale::selector&& lsel, |
| 561 | : handler_{l, std::move(inner)}, | 671 | api::handler&& inner) |
| 672 | : handler_{l, std::move(inner), std::move(rwgps_creds)}, | ||
| 562 | srv_{ | 673 | srv_{ |
| 563 | l, | 674 | l, |
| 564 | http::router{ | 675 | http::router{ |