-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept access token from Authorization
header
#1720
Open
Qup42
wants to merge
1
commit into
ad-freiburg:master
Choose a base branch
from
Qup42:accessTokenFromAcceptHeader
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−27
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,14 @@ using namespace ad_utility::url_parser::sparqlOperation; | |
|
||
namespace { | ||
auto ParsedRequestIs = [](const std::string& path, | ||
const std::optional<std::string>& accessToken, | ||
const ParamValueMap& parameters, | ||
const std::variant<Query, Update, None>& operation) | ||
-> testing::Matcher<const ParsedRequest> { | ||
return testing::AllOf( | ||
AD_FIELD(ad_utility::url_parser::ParsedRequest, path_, testing::Eq(path)), | ||
AD_FIELD(ad_utility::url_parser::ParsedRequest, accessToken_, | ||
testing::Eq(accessToken)), | ||
AD_FIELD(ad_utility::url_parser::ParsedRequest, parameters_, | ||
testing::ContainerEq(parameters)), | ||
AD_FIELD(ad_utility::url_parser::ParsedRequest, operation_, | ||
|
@@ -55,19 +58,21 @@ TEST(ServerTest, parseHttpRequest) { | |
"application/x-www-form-urlencoded;charset=UTF-8"; | ||
const std::string QUERY = "application/sparql-query"; | ||
const std::string UPDATE = "application/sparql-update"; | ||
EXPECT_THAT(parse(MakeGetRequest("/")), ParsedRequestIs("/", {}, None{})); | ||
EXPECT_THAT(parse(MakeGetRequest("/")), | ||
ParsedRequestIs("/", std::nullopt, {}, None{})); | ||
EXPECT_THAT(parse(MakeGetRequest("/ping")), | ||
ParsedRequestIs("/ping", {}, None{})); | ||
ParsedRequestIs("/ping", std::nullopt, {}, None{})); | ||
EXPECT_THAT(parse(MakeGetRequest("/?cmd=stats")), | ||
ParsedRequestIs("/", {{"cmd", {"stats"}}}, None{})); | ||
ParsedRequestIs("/", std::nullopt, {{"cmd", {"stats"}}}, None{})); | ||
EXPECT_THAT(parse(MakeGetRequest( | ||
"/?query=SELECT+%2A%20WHERE%20%7B%7D&action=csv_export")), | ||
ParsedRequestIs("/", {{"action", {"csv_export"}}}, | ||
ParsedRequestIs("/", std::nullopt, {{"action", {"csv_export"}}}, | ||
Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/", URLENCODED, | ||
"query=SELECT+%2A%20WHERE%20%7B%7D&send=100")), | ||
ParsedRequestIs("/", {{"send", {"100"}}}, Query{"SELECT * WHERE {}"})); | ||
ParsedRequestIs("/", std::nullopt, {{"send", {"100"}}}, | ||
Query{"SELECT * WHERE {}"})); | ||
AD_EXPECT_THROW_WITH_MESSAGE( | ||
parse(MakePostRequest("/", URLENCODED, | ||
"ääär y=SELECT+%2A%20WHERE%20%7B%7D&send=100")), | ||
|
@@ -92,18 +97,20 @@ TEST(ServerTest, parseHttpRequest) { | |
EXPECT_THAT( | ||
parse(MakePostRequest("/", "application/x-www-form-urlencoded", | ||
"query=SELECT%20%2A%20WHERE%20%7B%7D&send=100")), | ||
ParsedRequestIs("/", {{"send", {"100"}}}, Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT(parse(MakePostRequest("/", URLENCODED, | ||
"query=SELECT%20%2A%20WHERE%20%7B%7D")), | ||
ParsedRequestIs("/", {}, Query{"SELECT * WHERE {}"})); | ||
ParsedRequestIs("/", std::nullopt, {{"send", {"100"}}}, | ||
Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/", URLENCODED, | ||
"query=SELECT%20%2A%20WHERE%20%7B%7D")), | ||
ParsedRequestIs("/", std::nullopt, {}, Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest( | ||
"/", URLENCODED, | ||
"query=SELECT%20%2A%20WHERE%20%7B%7D&default-graph-uri=https%3A%2F%" | ||
"2Fw3.org%2Fdefault&named-graph-uri=https%3A%2F%2Fw3.org%2F1&named-" | ||
"graph-uri=https%3A%2F%2Fw3.org%2F2")), | ||
ParsedRequestIs( | ||
"/", | ||
"/", std::nullopt, | ||
{{"default-graph-uri", {"https://w3.org/default"}}, | ||
{"named-graph-uri", {"https://w3.org/1", "https://w3.org/2"}}}, | ||
Query{"SELECT * WHERE {}"})); | ||
|
@@ -112,13 +119,15 @@ TEST(ServerTest, parseHttpRequest) { | |
"query=SELECT%20%2A%20WHERE%20%7B%7D")), | ||
testing::StrEq("URL-encoded POST requests must not contain query " | ||
"parameters in the URL.")); | ||
EXPECT_THAT(parse(MakePostRequest("/", URLENCODED, "cmd=clear-cache")), | ||
ParsedRequestIs("/", {{"cmd", {"clear-cache"}}}, None{})); | ||
EXPECT_THAT(parse(MakePostRequest("/", QUERY, "SELECT * WHERE {}")), | ||
ParsedRequestIs("/", {}, Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/?send=100", QUERY, "SELECT * WHERE {}")), | ||
ParsedRequestIs("/", {{"send", {"100"}}}, Query{"SELECT * WHERE {}"})); | ||
parse(MakePostRequest("/", URLENCODED, "cmd=clear-cache")), | ||
ParsedRequestIs("/", std::nullopt, {{"cmd", {"clear-cache"}}}, None{})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/", QUERY, "SELECT * WHERE {}")), | ||
ParsedRequestIs("/", std::nullopt, {}, Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT(parse(MakePostRequest("/?send=100", QUERY, "SELECT * WHERE {}")), | ||
ParsedRequestIs("/", std::nullopt, {{"send", {"100"}}}, | ||
Query{"SELECT * WHERE {}"})); | ||
AD_EXPECT_THROW_WITH_MESSAGE( | ||
parse(MakeBasicRequest(http::verb::patch, "/")), | ||
testing::StrEq( | ||
|
@@ -132,14 +141,35 @@ TEST(ServerTest, parseHttpRequest) { | |
AD_EXPECT_THROW_WITH_MESSAGE( | ||
parse(MakeGetRequest("/?update=DELETE%20%2A%20WHERE%20%7B%7D")), | ||
testing::StrEq("SPARQL Update is not allowed as GET request.")); | ||
EXPECT_THAT(parse(MakePostRequest("/", UPDATE, "DELETE * WHERE {}")), | ||
ParsedRequestIs("/", {}, Update{"DELETE * WHERE {}"})); | ||
EXPECT_THAT(parse(MakePostRequest("/", URLENCODED, | ||
"update=DELETE%20%2A%20WHERE%20%7B%7D")), | ||
ParsedRequestIs("/", {}, Update{"DELETE * WHERE {}"})); | ||
EXPECT_THAT(parse(MakePostRequest("/", URLENCODED, | ||
"update=DELETE+%2A+WHERE%20%7B%7D")), | ||
ParsedRequestIs("/", {}, Update{"DELETE * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/", UPDATE, "DELETE * WHERE {}")), | ||
ParsedRequestIs("/", std::nullopt, {}, Update{"DELETE * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/", URLENCODED, | ||
"update=DELETE%20%2A%20WHERE%20%7B%7D")), | ||
ParsedRequestIs("/", std::nullopt, {}, Update{"DELETE * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse( | ||
MakePostRequest("/", URLENCODED, "update=DELETE+%2A+WHERE%20%7B%7D")), | ||
ParsedRequestIs("/", std::nullopt, {}, Update{"DELETE * WHERE {}"})); | ||
// TODO<qup42>: there could be some more here, but i'll wait until #1668 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
EXPECT_THAT( | ||
parse(MakeGetRequest("/?query=a&access-token=foo")), | ||
ParsedRequestIs("/", "foo", {{"access-token", {"foo"}}}, Query{"a"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/", URLENCODED, | ||
"update=DELETE%20WHERE%20%7B%7D&access-token=foo")), | ||
ParsedRequestIs("/", "foo", {{"access-token", {"foo"}}}, | ||
Update{"DELETE WHERE {}"})); | ||
EXPECT_THAT(parse(MakePostRequest( | ||
"/", URLENCODED, | ||
"query=SELECT%20%2A%20WHERE%20%7B%7D&access-token=foo")), | ||
ParsedRequestIs("/", "foo", {{"access-token", {"foo"}}}, | ||
Query{"SELECT * WHERE {}"})); | ||
EXPECT_THAT( | ||
parse(MakePostRequest("/?access-token=foo", UPDATE, "DELETE * WHERE {}")), | ||
ParsedRequestIs("/", "foo", {{"access-token", {"foo"}}}, | ||
Update{"DELETE * WHERE {}"})); | ||
} | ||
|
||
TEST(ServerTest, checkParameter) { | ||
|
@@ -284,3 +314,57 @@ TEST(ServerTest, createMessageSender) { | |
"SELECT * WHERE { ?a ?b ?c }"), | ||
testing::HasSubstr("Assertion `queryHubLock` failed.")); | ||
} | ||
|
||
TEST(ServerTest, extractAccessToken) { | ||
auto extract = [](const ad_utility::httpUtils::HttpRequest auto& request) { | ||
auto parsedUrl = parseRequestTarget(request.target()); | ||
return Server::extractAccessToken(request, parsedUrl.parameters_); | ||
}; | ||
// TODO<qup42>: replace once #1668 is merged | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
auto makeRequest = | ||
[](const http::verb method = http::verb::get, | ||
const std::string& target = "/", | ||
const ad_utility::HashMap<http::field, std::string>& headers = {}, | ||
const std::optional<std::string>& body = std::nullopt) { | ||
// version 11 stands for HTTP/1.1 | ||
auto req = http::request<http::string_body>{method, target, 11}; | ||
for (const auto& [key, value] : headers) { | ||
req.set(key, value); | ||
} | ||
if (body.has_value()) { | ||
req.body() = body.value(); | ||
req.prepare_payload(); | ||
} | ||
return req; | ||
}; | ||
EXPECT_THAT(extract(MakeGetRequest("/")), testing::Eq(std::nullopt)); | ||
EXPECT_THAT(extract(MakeGetRequest("/?access-token=foo")), | ||
testing::Optional(testing::Eq("foo"))); | ||
EXPECT_THAT( | ||
extract(makeRequest(http::verb::get, "/", | ||
{{http::field::authorization, "Bearer foo"}})), | ||
testing::Optional(testing::Eq("foo"))); | ||
// The header takes precedence over the query parameter. | ||
EXPECT_THAT( | ||
extract(makeRequest(http::verb::get, "/?access-token=bar", | ||
{{http::field::authorization, "Bearer foo"}})), | ||
testing::Optional(testing::Eq("foo"))); | ||
AD_EXPECT_THROW_WITH_MESSAGE( | ||
extract(makeRequest(http::verb::get, "/", | ||
{{http::field::authorization, "foo"}})), | ||
testing::HasSubstr( | ||
"Authorization header must start with \"Bearer \". Got: \"foo\".")); | ||
EXPECT_THAT(extract(MakePostRequest("/", "text/turtle", "")), | ||
testing::Eq(std::nullopt)); | ||
EXPECT_THAT(extract(MakePostRequest("/?access-token=foo", "text/turtle", "")), | ||
testing::Optional(testing::Eq("foo"))); | ||
EXPECT_THAT( | ||
extract(makeRequest(http::verb::post, "/?access-token=bar", | ||
{{http::field::authorization, "Bearer foo"}})), | ||
testing::Optional(testing::Eq("foo"))); | ||
AD_EXPECT_THROW_WITH_MESSAGE( | ||
extract(makeRequest(http::verb::post, "/?access-token=bar", | ||
{{http::field::authorization, "foo"}})), | ||
testing::HasSubstr( | ||
"Authorization header must start with \"Bearer \". Got: \"foo\".")); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dislike responding with the access token, but did it to stay consistent with the rest of the code. Sensitive data should imo not be echoed.