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
3829namespace olp {
3930namespace authentication {
4031
4132namespace {
33+ static const std::string kOauth2TokenEndpoint = " /oauth2/token" ;
34+ static constexpr auto kLogTag = " TokenEndpoint" ;
35+
4236AuthenticationSettings 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
8686std::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
9696TokenEndpoint::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
127126std::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
133132AutoRefreshingToken TokenEndpoint::RequestAutoRefreshingToken (
0 commit comments