Skip to content

Commit b1e42c5

Browse files
Add project scope to the settings (#1566)
It is attached to the token request and can be used on server side in the authentication process. Relates-To: DATASDK-8 Signed-off-by: Rustam Gamidov <[email protected]>
1 parent de37609 commit b1e42c5

File tree

8 files changed

+176
-21
lines changed

8 files changed

+176
-21
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -104,6 +104,11 @@ struct AUTHENTICATION_API Settings {
104104
* treated.
105105
*/
106106
client::RetrySettings retry_settings;
107+
108+
/**
109+
* @brief (Optional) The scope to be assigned to an access token requests.
110+
*/
111+
boost::optional<std::string> scope;
107112
};
108113

109114
} // namespace authentication

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -19,6 +19,7 @@
1919

2020
#pragma once
2121

22+
#include <boost/optional.hpp>
2223
#include <chrono>
2324
#include <ctime>
2425
#include <string>
@@ -43,16 +44,20 @@ class AUTHENTICATION_API TokenResult {
4344
* @param access_token The access token issued by the authorization server.
4445
* @param expiry_time The Epoch time when the token expires, or -1 if
4546
* the token is invalid.
47+
* @param scope The scope assigned to the access token.
4648
*/
47-
TokenResult(std::string access_token, time_t expiry_time);
49+
TokenResult(std::string access_token, time_t expiry_time,
50+
boost::optional<std::string> scope);
4851

4952
/**
5053
* @brief Creates the `TokenResult` instance.
5154
*
5255
* @param access_token The access token issued by the authorization server.
5356
* @param expires_in The expiry time of the access token.
57+
* @param scop The scope assigned to the access token.
5458
*/
55-
TokenResult(std::string access_token, std::chrono::seconds expires_in);
59+
TokenResult(std::string access_token, std::chrono::seconds expires_in,
60+
boost::optional<std::string> scope);
5661
/**
5762
* @brief Creates the default `TokenResult` instance.
5863
*/
@@ -81,10 +86,19 @@ class AUTHENTICATION_API TokenResult {
8186
*/
8287
std::chrono::seconds GetExpiresIn() const;
8388

89+
/**
90+
* @brief Gets the scope that is assigned to the access token.
91+
*
92+
* @return The optional string that contains the scope assigned to the access
93+
* token.
94+
*/
95+
const boost::optional<std::string>& GetScope() const;
96+
8497
private:
8598
std::string access_token_;
8699
time_t expiry_time_{};
87100
std::chrono::seconds expires_in_{};
101+
boost::optional<std::string> scope_;
88102
};
89103

90104
} // namespace authentication

olp-cpp-sdk-authentication/src/TokenEndpointImpl.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2023 HERE Europe B.V.
2+
* Copyright (C) 2021-2024 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.
@@ -19,7 +19,9 @@
1919

2020
#include "TokenEndpointImpl.h"
2121

22+
#include <memory>
2223
#include <thread>
24+
#include <utility>
2325

2426
#include <olp/authentication/SignInResult.h>
2527
#include <olp/core/http/HttpStatusCode.h>
@@ -52,6 +54,7 @@ constexpr auto kGrantType = "grantType";
5254
constexpr auto kClientGrantType = "client_credentials";
5355
constexpr auto kLogTag = "TokenEndpointImpl";
5456
constexpr auto kErrorWrongTimestamp = 401204;
57+
constexpr auto kScope = "scope";
5558

5659
std::string GetBasePath(const std::string& base_string) {
5760
// Remove /oauth2/token from url to make sure only the base url is used
@@ -125,7 +128,8 @@ std::string GenerateUid() {
125128
}
126129

127130
client::OlpClient::RequestBodyType GenerateClientBody(
128-
const TokenRequest& token_request) {
131+
const TokenRequest& token_request,
132+
const boost::optional<std::string>& scope) {
129133
rapidjson::StringBuffer data;
130134
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
131135
writer.StartObject();
@@ -140,6 +144,11 @@ client::OlpClient::RequestBodyType GenerateClientBody(
140144
writer.Uint(expires_in);
141145
}
142146

147+
if (scope) {
148+
writer.Key(kScope);
149+
writer.String(scope.get().c_str());
150+
}
151+
143152
writer.EndObject();
144153
auto content = data.GetString();
145154
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
@@ -177,13 +186,15 @@ TimeResponse GetTimeFromServer(client::CancellationContext& context,
177186

178187
TokenEndpointImpl::TokenEndpointImpl(Settings settings)
179188
: credentials_(std::move(settings.credentials)),
189+
scope_(std::move(settings.scope)),
180190
settings_(ConvertSettings(std::move(settings))),
181191
auth_client_(settings_) {}
182192

183193
client::CancellationToken TokenEndpointImpl::RequestToken(
184194
const TokenRequest& token_request, const RequestTokenCallback& callback) {
185195
AuthenticationClient::SignInProperties properties;
186196
properties.expires_in = token_request.GetExpiresIn();
197+
properties.scope = scope_;
187198
return auth_client_.SignInClient(
188199
credentials_, properties,
189200
[callback](
@@ -200,8 +211,10 @@ client::CancellationToken TokenEndpointImpl::RequestToken(
200211
return;
201212
}
202213

203-
callback(TokenResult{sign_in_result.GetAccessToken(),
204-
sign_in_result.GetExpiresIn()});
214+
callback(TokenResult{
215+
sign_in_result.GetAccessToken(), sign_in_result.GetExpiresIn(),
216+
sign_in_result.GetScope().empty() ? boost::optional<std::string>{}
217+
: sign_in_result.GetScope()});
205218
});
206219
}
207220

@@ -236,8 +249,10 @@ TokenResponse TokenEndpointImpl::RequestToken(
236249
return client::ApiError{sign_in_result.GetStatus(), std::move(message)};
237250
}
238251

239-
return TokenResult{sign_in_result.GetAccessToken(),
240-
sign_in_result.GetExpiresIn()};
252+
return TokenResult{
253+
sign_in_result.GetAccessToken(), sign_in_result.GetExpiresIn(),
254+
sign_in_result.GetScope().empty() ? boost::optional<std::string>{}
255+
: sign_in_result.GetScope()};
241256
}
242257

243258
SignInResponse TokenEndpointImpl::SignInClient(
@@ -255,7 +270,7 @@ SignInResponse TokenEndpointImpl::SignInClient(
255270

256271
RequestTimer timer = CreateRequestTimer(client, context);
257272

258-
const auto request_body = GenerateClientBody(token_request);
273+
const auto request_body = GenerateClientBody(token_request, scope_);
259274

260275
SignInResult response;
261276

olp-cpp-sdk-authentication/src/TokenEndpointImpl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 HERE Europe B.V.
2+
* Copyright (C) 2021-2024 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.
@@ -19,6 +19,9 @@
1919

2020
#pragma once
2121

22+
#include <boost/optional.hpp>
23+
#include <string>
24+
2225
#include <olp/authentication/AuthenticationClient.h>
2326
#include <olp/authentication/AuthenticationCredentials.h>
2427
#include <olp/authentication/AuthenticationSettings.h>
@@ -87,6 +90,7 @@ class TokenEndpointImpl {
8790
client::CancellationContext& context) const;
8891

8992
const AuthenticationCredentials credentials_;
93+
const boost::optional<std::string> scope_;
9094
const AuthenticationSettings settings_;
9195
AuthenticationClient auth_client_;
9296
};

olp-cpp-sdk-authentication/src/TokenResult.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -21,16 +21,22 @@
2121

2222
namespace olp {
2323
namespace authentication {
24-
TokenResult::TokenResult(std::string access_token, time_t expiry_time)
25-
: access_token_(std::move(access_token)), expiry_time_(expiry_time) {
24+
TokenResult::TokenResult(std::string access_token, time_t expiry_time,
25+
boost::optional<std::string> scope)
26+
: access_token_(std::move(access_token)),
27+
expiry_time_(expiry_time),
28+
scope_(std::move(scope)) {
2629
const auto now = std::time(nullptr);
2730
expires_in_ =
2831
std::chrono::seconds(expiry_time_ > now ? (expiry_time_ - now) : 0);
2932
}
3033

3134
TokenResult::TokenResult(std::string access_token,
32-
std::chrono::seconds expires_in)
33-
: access_token_(std::move(access_token)), expires_in_(expires_in) {
35+
std::chrono::seconds expires_in,
36+
boost::optional<std::string> scope)
37+
: access_token_(std::move(access_token)),
38+
expires_in_(expires_in),
39+
scope_(std::move(scope)) {
3440
expiry_time_ = std::time(nullptr) + expires_in_.count();
3541
}
3642

@@ -40,5 +46,9 @@ time_t TokenResult::GetExpiryTime() const { return expiry_time_; }
4046

4147
std::chrono::seconds TokenResult::GetExpiresIn() const { return expires_in_; }
4248

49+
const boost::optional<std::string>& TokenResult::GetScope() const {
50+
return scope_;
51+
}
52+
4353
} // namespace authentication
4454
} // namespace olp

tests/common/matchers/NetworkUrlMatchers.h

Lines changed: 8 additions & 1 deletion
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-2024 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.
@@ -93,6 +93,13 @@ MATCHER_P(BodyEq, expected_body, "") {
9393
: expected_body_str.empty();
9494
}
9595

96+
MATCHER_P(BodyContains, expected_substring, "") {
97+
const std::string sstr(expected_substring);
98+
const auto& body = arg.GetBody();
99+
return std::search(body->cbegin(), body->cend(), sstr.cbegin(),
100+
sstr.cend()) != body->cend();
101+
}
102+
96103
MATCHER_P(HeadersContain, expected_header, "") {
97104
const auto& headers = arg.GetHeaders();
98105
return std::find(headers.begin(), headers.end(), expected_header) !=

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2022 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -410,7 +410,10 @@ TEST_F(AuthenticationClientTest, SignInClientUseWrongLocalTime) {
410410
TEST_F(AuthenticationClientTest, SignInClientScope) {
411411
ExpectTimestampRequest(*network_);
412412

413-
EXPECT_CALL(*network_, Send(IsPostRequest(kRequestAuth), _, _, _, _))
413+
EXPECT_CALL(*network_,
414+
Send(testing::AllOf(IsPostRequest(kRequestAuth),
415+
BodyContains("\"scope\":\"scope\"")),
416+
_, _, _, _))
414417
.WillOnce(ReturnHttpResponse(GetResponse(http::HttpStatusCode::OK),
415418
kResponseWithScope));
416419

0 commit comments

Comments
 (0)