Skip to content

Commit 3814af1

Browse files
authored
Don't send request if bearer token is empty. (#1236)
If during authorization error happened, token will be empty. We don't propagate error, but at least request should not be sent in this case. Relates-To: OLPEDGE-2607 Signed-off-by: Kostiantyn Zvieriev <[email protected]>
1 parent 1b91eb9 commit 3814af1

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

olp-cpp-sdk-core/src/client/OlpClient.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -359,7 +359,7 @@ class OlpClient::OlpClientImpl {
359359
const ParametersType& query_params, const ParametersType& header_params,
360360
const RequestBodyType& post_body, const std::string& content_type) const;
361361

362-
void AddBearer(bool query_empty, http::NetworkRequest& request) const;
362+
bool AddBearer(bool query_empty, http::NetworkRequest& request) const;
363363

364364
private:
365365
using MutexType = std::shared_mutex;
@@ -398,25 +398,31 @@ void OlpClient::OlpClientImpl::SetSettings(const OlpClientSettings& settings) {
398398
settings_ = settings;
399399
}
400400

401-
void OlpClient::OlpClientImpl::AddBearer(bool query_empty,
401+
bool OlpClient::OlpClientImpl::AddBearer(bool query_empty,
402402
http::NetworkRequest& request) const {
403403
const auto& settings = settings_.authentication_settings;
404404
if (!settings) {
405-
return;
405+
return true;
406406
}
407407

408408
if (settings->api_key_provider) {
409409
const auto& api_key = settings->api_key_provider();
410410
request.WithUrl(request.GetUrl() + (query_empty ? "?" : "&") +
411411
kApiKeyParam + api_key);
412-
return;
412+
return true;
413413
}
414414

415415
if (settings->provider) {
416-
std::string bearer =
417-
http::kBearer + std::string(" ") + settings->provider();
416+
const auto token = settings->provider();
417+
if (token.empty()) {
418+
return false;
419+
}
420+
421+
const std::string bearer = http::kBearer + std::string(" ") + token;
418422
request.WithHeader(http::kAuthorizationHeader, bearer);
419423
}
424+
425+
return true;
420426
}
421427

422428
std::shared_ptr<http::NetworkRequest> OlpClient::OlpClientImpl::CreateRequest(
@@ -475,7 +481,11 @@ CancellationToken OlpClient::OlpClientImpl::CallApi(
475481
auto network_request = CreateRequest(path, method, query_params,
476482
header_params, post_body, content_type);
477483

478-
AddBearer(query_params.empty(), *network_request);
484+
if (!AddBearer(query_params.empty(), *network_request)) {
485+
callback({static_cast<int>(http::ErrorCode::AUTHORIZATION_ERROR),
486+
"Invalid bearer token."});
487+
return CancellationToken();
488+
}
479489

480490
PendingUrlRequestPtr request_ptr = nullptr;
481491
auto& pending_requests = pending_requests_;
@@ -588,7 +598,10 @@ HttpResponse OlpClient::OlpClientImpl::CallApi(
588598
auto backdown_period =
589599
std::chrono::milliseconds(retry_settings.initial_backdown_period);
590600

591-
AddBearer(query_params.empty(), network_request);
601+
if (!AddBearer(query_params.empty(), network_request)) {
602+
return {static_cast<int>(http::ErrorCode::AUTHORIZATION_ERROR),
603+
"Invalid bearer token."};
604+
}
592605

593606
auto response =
594607
SendRequest(network_request, settings_, retry_settings, context);

olp-cpp-sdk-core/tests/client/OlpClientTest.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1638,6 +1638,25 @@ TEST_P(OlpClientTest, ApiKey) {
16381638
testing::Mock::VerifyAndClearExpectations(network.get());
16391639
}
16401640

1641+
TEST_P(OlpClientTest, EmptyBearerToken) {
1642+
// Make token provider generate empty strings. We expect no network requests
1643+
// made in this case.
1644+
auto authentication_settings = olp::client::AuthenticationSettings();
1645+
authentication_settings.provider = []() { return std::string(""); };
1646+
auto network = network_;
1647+
client_settings_.authentication_settings = authentication_settings;
1648+
client_.SetSettings(client_settings_);
1649+
1650+
EXPECT_CALL(*network, Send(_, _, _, _, _)).Times(0);
1651+
1652+
auto response =
1653+
call_wrapper_->CallApi("here.com", "GET", {}, {}, {}, nullptr, {});
1654+
EXPECT_EQ(response.GetStatus(),
1655+
static_cast<int>(http::ErrorCode::AUTHORIZATION_ERROR));
1656+
1657+
testing::Mock::VerifyAndClearExpectations(network.get());
1658+
}
1659+
16411660
INSTANTIATE_TEST_SUITE_P(, OlpClientTest,
16421661
::testing::Values(CallApiType::ASYNC,
16431662
CallApiType::SYNC));

tests/functional/olp-cpp-sdk-dataservice-read/VersionedLayerClientGetDataTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,6 +71,7 @@ TEST_F(VersionedLayerClientGetDataTest, GetDataFromPartitionAsync) {
7171
auto partition = std::to_string(0);
7272
const auto data = mockserver::ReadDefaultResponses::GenerateData();
7373
{
74+
mock_server_client_->MockAuth();
7475
mock_server_client_->MockLookupResourceApiResponse(
7576
mockserver::ApiDefaultResponses::GenerateResourceApisResponse(
7677
kTestHrn));

0 commit comments

Comments
 (0)