Skip to content

Commit 1ade147

Browse files
author
Andrei Popescu
authored
Adapts TokenEndpoint to coding style. (#703)
TokenEndpoint impl is now a class, not a struct anymore as this is against the coding style. Some minor variables renamed. This also moves the impl methods definition from inlined to ouside the class to align all methods. TokenEndpoint, as it is currently public, should also include its dependency headers via global scope. Relates-to: OLPEDGE-1028 Signed-off-by: Andrei Popescu <[email protected]>
1 parent 2dac7cf commit 1ade147

File tree

2 files changed

+67
-71
lines changed

2 files changed

+67
-71
lines changed

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@
2222
#include <future>
2323
#include <memory>
2424

25-
#include "AuthenticationApi.h"
26-
#include "AuthenticationCredentials.h"
27-
#include "AuthenticationError.h"
28-
#include "Settings.h"
29-
#include "TokenRequest.h"
30-
#include "TokenResult.h"
31-
32-
#include "olp/core/client/ApiResponse.h"
33-
#include "olp/core/client/CancellationToken.h"
34-
#include "olp/core/porting/warning_disable.h"
25+
#include <olp/authentication/AuthenticationApi.h>
26+
#include <olp/authentication/AuthenticationCredentials.h>
27+
#include <olp/authentication/AuthenticationError.h>
28+
#include <olp/authentication/Settings.h>
29+
#include <olp/authentication/TokenRequest.h>
30+
#include <olp/authentication/TokenResult.h>
31+
#include <olp/authentication/Types.h>
32+
#include <olp/core/client/ApiResponse.h>
33+
#include <olp/core/client/CancellationToken.h>
34+
#include <olp/core/porting/warning_disable.h>
3535

3636
namespace olp {
3737
namespace authentication {
3838
class AutoRefreshingToken;
39+
3940
PORTING_PUSH_WARNINGS()
4041
PORTING_CLANG_GCC_DISABLE_WARNING("-Wdeprecated-declarations")
4142

@@ -46,22 +47,18 @@ PORTING_CLANG_GCC_DISABLE_WARNING("-Wdeprecated-declarations")
4647
class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed in 04.2020")
4748
TokenEndpoint {
4849
public:
49-
/**
50-
* @brief Defines the signature used to return the response to the client.
51-
*/
52-
using TokenResponse = client::ApiResponse<TokenResult, AuthenticationError>;
53-
/**
54-
* @brief Defines the callback that is invoked when the response on
55-
* `TokenRequest` is returned.
56-
*/
57-
using RequestTokenCallback = std::function<void(TokenResponse)>;
50+
/// Defines the signature used to return the response to the client.
51+
using TokenResponse = Response<TokenResult>;
52+
/// Defines the callback that is invoked when the response on
53+
/// `TokenRequest` is returned.
54+
using RequestTokenCallback = Callback<TokenResult>;
5855

5956
/**
6057
* @brief Executes the POST request method to the token endpoint.
6158
*
62-
* The request gets the HERE Access token that is used to access the HERE OLP
63-
* Services. Returns the token that is used as the `Authorization: Bearer`
64-
* token value.
59+
* The request gets the HERE Access token that is used to access the HERE
60+
* OLP Services. Returns the token that is used as the `Authorization:
61+
* Bearer` token value.
6562
*
6663
* @param token_request The `TokenRequest` instance.
6764
* @param callback The `RequestTokenCallback` instance that passes
@@ -127,10 +124,10 @@ class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed in 04.2020")
127124
explicit TokenEndpoint(Settings settings);
128125

129126
private:
130-
struct Impl;
127+
class Impl;
131128
std::shared_ptr<Impl> impl_;
132129
};
133-
PORTING_POP_WARNINGS()
134130

131+
PORTING_POP_WARNINGS()
135132
} // namespace authentication
136133
} // namespace olp

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

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20-
#include <olp/authentication/TokenEndpoint.h>
20+
#include "olp/authentication/TokenEndpoint.h"
2121

22-
#include <olp/authentication/TokenResult.h>
2322
#include "olp/authentication/AuthenticationClient.h"
2423
#include "olp/authentication/AuthenticationCredentials.h"
2524
#include "olp/authentication/AutoRefreshingToken.h"
2625
#include "olp/authentication/ErrorResponse.h"
27-
#include "olp/authentication/Settings.h"
2826
#include "olp/authentication/SignInResult.h"
29-
#include "olp/authentication/TokenRequest.h"
3027
#include "olp/core/logging/Log.h"
31-
#include "olp/core/porting/warning_disable.h"
32-
33-
namespace {
34-
const std::string kOauth2TokenEndpoint = "/oauth2/token";
35-
constexpr auto kLogTag = "here::account::oauth2::TokenEndpoint";
36-
} // namespace
3728

3829
namespace olp {
3930
namespace authentication {
4031

4132
namespace {
33+
static const std::string kOauth2TokenEndpoint = "/oauth2/token";
34+
static constexpr auto kLogTag = "TokenEndpoint";
35+
4236
AuthenticationSettings ConvertSettings(Settings settings) {
4337
AuthenticationSettings auth_settings;
4438
auth_settings.network_proxy_settings = settings.network_proxy_settings;
@@ -47,50 +41,56 @@ AuthenticationSettings ConvertSettings(Settings settings) {
4741
auth_settings.token_endpoint_url = settings.token_endpoint_url;
4842
return auth_settings;
4943
}
50-
5144
} // namespace
5245

53-
struct TokenEndpoint::Impl {
54-
explicit Impl(Settings settings)
55-
: auth_client_(ConvertSettings(settings)),
56-
auth_credentials_(std::move(settings.credentials)) {}
46+
class TokenEndpoint::Impl {
47+
public:
48+
explicit Impl(Settings settings);
5749

5850
client::CancellationToken RequestToken(const TokenRequest& token_request,
59-
const RequestTokenCallback& callback) {
60-
AuthenticationClient::SignInProperties properties;
61-
properties.expires_in = token_request.GetExpiresIn();
62-
return auth_client_.SignInClient(
63-
auth_credentials_, properties,
64-
[callback](
65-
const AuthenticationClient::SignInClientResponse& signInResponse) {
66-
if (signInResponse.IsSuccessful()) {
67-
TokenResult result(signInResponse.GetResult().GetAccessToken(),
68-
signInResponse.GetResult().GetExpiresIn(),
69-
signInResponse.GetResult().GetStatus(),
70-
signInResponse.GetResult().GetErrorResponse());
71-
callback(TokenResponse(result));
72-
} else {
73-
callback(signInResponse.GetError());
74-
}
75-
});
76-
}
51+
const RequestTokenCallback& callback);
7752

7853
std::future<TokenResponse> RequestToken(
7954
client::CancellationToken& cancellation_token,
8055
const TokenRequest& token_request);
8156

82-
olp::authentication::AuthenticationClient auth_client_;
83-
olp::authentication::AuthenticationCredentials auth_credentials_;
84-
}; // namespace authentication
57+
private:
58+
AuthenticationClient auth_client_;
59+
AuthenticationCredentials auth_credentials_;
60+
};
61+
62+
TokenEndpoint::Impl::Impl(Settings settings)
63+
: auth_client_(ConvertSettings(settings)),
64+
auth_credentials_(std::move(settings.credentials)) {}
65+
66+
client::CancellationToken TokenEndpoint::Impl::RequestToken(
67+
const TokenRequest& token_request, const RequestTokenCallback& callback) {
68+
AuthenticationClient::SignInProperties properties;
69+
properties.expires_in = token_request.GetExpiresIn();
70+
return auth_client_.SignInClient(
71+
auth_credentials_, properties,
72+
[callback](
73+
const AuthenticationClient::SignInClientResponse& signInResponse) {
74+
if (signInResponse.IsSuccessful()) {
75+
TokenResult result(signInResponse.GetResult().GetAccessToken(),
76+
signInResponse.GetResult().GetExpiresIn(),
77+
signInResponse.GetResult().GetStatus(),
78+
signInResponse.GetResult().GetErrorResponse());
79+
callback(TokenResponse(result));
80+
} else {
81+
callback(signInResponse.GetError());
82+
}
83+
});
84+
}
8585

8686
std::future<TokenEndpoint::TokenResponse> TokenEndpoint::Impl::RequestToken(
87-
client::CancellationToken& cancellation_token,
87+
client::CancellationToken& cancel_token,
8888
const TokenRequest& token_request) {
89-
auto p = std::make_shared<std::promise<TokenResponse> >();
90-
cancellation_token = RequestToken(
91-
token_request,
92-
[p](TokenResponse tokenResponse) { p->set_value(tokenResponse); });
93-
return p->get_future();
89+
auto promise = std::make_shared<std::promise<TokenResponse>>();
90+
cancel_token = RequestToken(token_request, [promise](TokenResponse response) {
91+
promise->set_value(std::move(response));
92+
});
93+
return promise->get_future();
9494
}
9595

9696
TokenEndpoint::TokenEndpoint(Settings settings) {
@@ -104,9 +104,8 @@ TokenEndpoint::TokenEndpoint(Settings settings) {
104104
} else {
105105
OLP_SDK_LOG_ERROR(
106106
kLogTag,
107-
"Expected '/oauth2/token' endpoint in the tokenEndpointUrl. Only "
108-
"standard "
109-
"OAuth2 token endpoint URLs are supported.");
107+
"Expected '/oauth2/token' endpoint in the token_endpoint_url. Only "
108+
"standard OAuth2 token endpoint URLs are supported.");
110109
}
111110

112111
impl_ = std::make_shared<TokenEndpoint::Impl>(std::move(settings));
@@ -126,8 +125,8 @@ std::future<TokenEndpoint::TokenResponse> TokenEndpoint::RequestToken(
126125

127126
std::future<TokenEndpoint::TokenResponse> TokenEndpoint::RequestToken(
128127
const TokenRequest& token_request) const {
129-
client::CancellationToken cancellationToken;
130-
return impl_->RequestToken(cancellationToken, token_request);
128+
client::CancellationToken cancellation_token;
129+
return impl_->RequestToken(cancellation_token, token_request);
131130
}
132131

133132
AutoRefreshingToken TokenEndpoint::RequestAutoRefreshingToken(

0 commit comments

Comments
 (0)