From 0371816cf8d5a45d92dd6b5000059d20ea1d11d1 Mon Sep 17 00:00:00 2001 From: Morten Winkler Date: Sun, 27 Oct 2024 08:45:54 +0100 Subject: [PATCH] Feat: Add method to extract HTTP Bearer token from Authorization header. --- include/pistache/http_header.h | 4 ++++ src/common/http_header.cc | 10 ++++++++++ tests/headers_test.cc | 12 ++++++++++++ version.txt | 2 +- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/pistache/http_header.h b/include/pistache/http_header.h index 08315aec6..c458a6e8e 100644 --- a/include/pistache/http_header.h +++ b/include/pistache/http_header.h @@ -479,11 +479,15 @@ namespace Pistache::Http::Header // Get decoded user ID and password if basic method was used... std::string getBasicUser() const; std::string getBasicPassword() const; + std::string getBearerToken() const; // Set encoded user ID and password for basic method... void setBasicUserPassword(const std::string& User, const std::string& Password); + + void setBearertoken(const std::string& token); + void parse(const std::string& data) override; void write(std::ostream& os) const override; diff --git a/src/common/http_header.cc b/src/common/http_header.cc index 9e6aa21c8..bfa74daa6 100644 --- a/src/common/http_header.cc +++ b/src/common/http_header.cc @@ -450,6 +450,16 @@ namespace Pistache::Http::Header return true; } + std::string Authorization::getBearerToken() const + { + // Verify bearer authorization method was used... + if (!hasMethod()) + throw std::runtime_error("Authorization header does not use Bearer method."); + + const std::string token(value_.begin() + std::string("Bearer ").length(), value_.end()); + return token; + } + // Get decoded user ID if basic method was used... std::string Authorization::getBasicUser() const { diff --git a/tests/headers_test.cc b/tests/headers_test.cc index 5825db083..f679d3eae 100644 --- a/tests/headers_test.cc +++ b/tests/headers_test.cc @@ -427,6 +427,7 @@ TEST(headers_test, authorization_basic_test) // Verify it decoded correctly... ASSERT_EQ(au.getBasicUser(), "Aladdin"); ASSERT_EQ(au.getBasicPassword(), "OpenSesame"); + EXPECT_ANY_THROW( au.getBearerToken() ); } TEST(headers_test, authorization_bearer_test) @@ -458,6 +459,17 @@ TEST(headers_test, authorization_bearer_test) "eyJleHAiOjE1NzA2MzA0MDcsImlhdCI6MTU3MDU0NDAwNywibmFtZSI6IkFkbWluIE5hbWUi" "LCJzYW1wbGUiOiJUZXN0In0.zLTAAnBftlqccsU-4mL69P4tQl3VhcglMg-" "d0131JxqX4xSZLlO5xMRrCPBgn_00OxKJ9CQdnpjpuzblNQd2-A"); + + EXPECT_ANY_THROW(au.getBasicUser() ); + EXPECT_ANY_THROW(au.getBasicPassword() ); + + ASSERT_EQ(au.getBearerToken(), + "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXUyJ9." + "eyJleHAiOjE1NzA2MzA0MDcsImlhdCI6MTU3MDU0NDAwNywibmFtZSI6IkFkbWluIE5hbWUi" + "LCJzYW1wbGUiOiJUZXN0In0.zLTAAnBftlqccsU-4mL69P4tQl3VhcglMg-" + "d0131JxqX4xSZLlO5xMRrCPBgn_00OxKJ9CQdnpjpuzblNQd2-A"); + + } TEST(headers_test, expect_test) diff --git a/version.txt b/version.txt index 313ca67fc..fd12d75e4 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.4.11.20241018 +0.4.12.20241028