Skip to content

Commit 58f9566

Browse files
authored
Use custom token endpoint url from credentials (#1433)
The token provider read from a credentials file in the method `AuthenticationCredentials::ReadFromStream()` was not used for the authentication request. Relates-To: OLPEDGE-2834 Signed-off-by: Mykola Malik <[email protected]>
1 parent 9fb70ea commit 58f9566

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

olp-cpp-sdk-authentication/include/olp/authentication/Settings.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2023 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.
@@ -53,8 +53,7 @@ struct AUTHENTICATION_API Settings {
5353
*
5454
* @param credentials Your access credentials to the HERE platform.
5555
*/
56-
Settings(AuthenticationCredentials credentials)
57-
: credentials(std::move(credentials)) {}
56+
explicit Settings(AuthenticationCredentials credentials);
5857

5958
/**
6059
* @brief The access key ID and access key secret that you got from the HERE
@@ -86,7 +85,7 @@ struct AUTHENTICATION_API Settings {
8685
* @note Only standard OAuth2 Token URLs (those ending in `oauth2/token`) are
8786
* supported.
8887
*/
89-
std::string token_endpoint_url{kHereAccountProductionTokenUrl};
88+
std::string token_endpoint_url;
9089

9190
/**
9291
* @brief Uses system system time in authentication requests rather than
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2023 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include "olp/authentication/Settings.h"
21+
22+
#include <utility>
23+
24+
namespace olp {
25+
namespace authentication {
26+
27+
Settings::Settings(AuthenticationCredentials credentials)
28+
: credentials(std::move(credentials)),
29+
token_endpoint_url{this->credentials.GetEndpointUrl().empty()
30+
? kHereAccountProductionTokenUrl
31+
: this->credentials.GetEndpointUrl()} {}
32+
33+
} // namespace authentication
34+
} // namespace olp

tests/integration/olp-cpp-sdk-authentication/TokenProviderTest.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,68 @@ TEST_F(TokenProviderTest, CancellableProvider) {
482482
}
483483
}
484484

485+
TEST_F(TokenProviderTest, CustomEndpoint) {
486+
const std::string kCustomTokenEndpoint{
487+
"https://custom.token.endpoint/oauth2/token"};
488+
const std::string kAnotherCustomTokenEndpoint{
489+
"https://another.custom.token.endpoint/oauth2/token"};
490+
const int kOkStatusCode = http::HttpStatusCode::OK;
491+
authentication::Settings token_provider_settings(
492+
{"fake.key.id", "fake.key.secret", kCustomTokenEndpoint});
493+
token_provider_settings.task_scheduler = settings_.task_scheduler;
494+
token_provider_settings.network_request_handler =
495+
settings_.network_request_handler;
496+
token_provider_settings.use_system_time = true;
497+
token_provider_settings.retry_settings.max_attempts = 1; // Disable retries
498+
499+
{
500+
SCOPED_TRACE("Use URL from credentials");
501+
502+
EXPECT_CALL(*network_mock_,
503+
Send(IsPostRequest(kCustomTokenEndpoint), _, _, _, _))
504+
.WillOnce(
505+
ReturnHttpResponse(GetResponse(kOkStatusCode), kResponseValidJson));
506+
507+
const authentication::TokenProviderDefault token_provider(
508+
token_provider_settings);
509+
510+
client::CancellationContext context;
511+
const auto token_response = token_provider(context);
512+
ASSERT_TRUE(token_response);
513+
EXPECT_EQ(token_response.GetResult().GetAccessToken(), kResponseToken);
514+
515+
EXPECT_TRUE(token_provider);
516+
EXPECT_EQ(token_provider.GetHttpStatusCode(), kOkStatusCode);
517+
518+
EXPECT_EQ(token_provider.GetErrorResponse().code, 0);
519+
520+
testing::Mock::VerifyAndClearExpectations(network_mock_.get());
521+
}
522+
523+
{
524+
SCOPED_TRACE("Override URL");
525+
526+
EXPECT_CALL(*network_mock_,
527+
Send(IsPostRequest(kAnotherCustomTokenEndpoint), _, _, _, _))
528+
.WillOnce(
529+
ReturnHttpResponse(GetResponse(kOkStatusCode), kResponseValidJson));
530+
531+
token_provider_settings.token_endpoint_url = kAnotherCustomTokenEndpoint;
532+
const authentication::TokenProviderDefault token_provider(
533+
token_provider_settings);
534+
535+
client::CancellationContext context;
536+
const auto token_response = token_provider(context);
537+
ASSERT_TRUE(token_response);
538+
EXPECT_EQ(token_response.GetResult().GetAccessToken(), kResponseToken);
539+
540+
EXPECT_TRUE(token_provider);
541+
EXPECT_EQ(token_provider.GetHttpStatusCode(), kOkStatusCode);
542+
543+
EXPECT_EQ(token_provider.GetErrorResponse().code, 0);
544+
545+
testing::Mock::VerifyAndClearExpectations(network_mock_.get());
546+
}
547+
}
548+
485549
} // namespace

0 commit comments

Comments
 (0)