Skip to content

Commit c8d72bb

Browse files
authored
Move classes from headers to classes (#1277)
Remove deprecated warnings Move classes to src folder Move constants from AutoRefreshingToken to TokenProvider Resolves: OLPEDGE-2658 Signed-off-by: Yevhenii Dudnyk <[email protected]>
1 parent 0c6ffbb commit c8d72bb

File tree

18 files changed

+227
-191
lines changed

18 files changed

+227
-191
lines changed

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

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,63 @@
2525
#include <utility>
2626

2727
#include <olp/authentication/AuthenticationCredentials.h>
28-
#include <olp/authentication/AutoRefreshingToken.h>
28+
#include <olp/authentication/ErrorResponse.h>
2929
#include <olp/authentication/Settings.h>
30-
#include <olp/authentication/TokenEndpoint.h>
31-
#include <olp/authentication/TokenResult.h>
3230
#include <olp/authentication/Types.h>
3331
#include <olp/core/client/CancellationContext.h>
3432
#include <olp/core/client/OauthToken.h>
3533
#include <olp/core/http/HttpStatusCode.h>
36-
#include <olp/core/utils/WarningWorkarounds.h>
3734

3835
namespace olp {
3936
namespace authentication {
4037

41-
// Needed to avoid endless warnings from TokenRequest/TokenResult
42-
PORTING_PUSH_WARNINGS()
43-
PORTING_CLANG_GCC_DISABLE_WARNING("-Wdeprecated-declarations")
38+
static constexpr auto kDefaultMinimumValidity = 300ll;
39+
static constexpr auto kDefaultMinimumValiditySeconds =
40+
std::chrono::seconds(kDefaultMinimumValidity);
41+
static constexpr auto kForceRefresh = std::chrono::seconds(0);
42+
43+
namespace internal {
44+
45+
class TokenProviderPrivate;
46+
47+
/// An implementation of `TokenProvider`.
48+
/// @note This is a private implementation class for internal use only, and not
49+
/// bound to any API stability promises. Please do not use directly.
50+
class TokenProviderImpl {
51+
public:
52+
/**
53+
* @brief Creates the `TokenProviderImpl` instance.
54+
*
55+
* @param settings The `Settings` object that is used to customize
56+
* the `TokenEndpoint` instance.
57+
* @param minimum_validity Sets the minimum validity period of
58+
* the token in seconds.
59+
*/
60+
TokenProviderImpl(Settings settings, std::chrono::seconds minimum_validity);
61+
62+
/// @copydoc TokenProvider::operator()()
63+
std::string operator()() const;
64+
65+
/// @copydoc TokenProvider::operator()(client::CancellationContext&)
66+
client::OauthTokenResponse operator()(
67+
client::CancellationContext& context) const;
68+
69+
/// @copydoc TokenProvider::GetErrorResponse()
70+
ErrorResponse GetErrorResponse() const;
71+
72+
/// @copydoc TokenProvider::GetHttpStatusCode()
73+
int GetHttpStatusCode() const;
74+
75+
/// @copydoc TokenProvider::GetResponse()(client::CancellationContext&)
76+
TokenResponse GetResponse(client::CancellationContext& context) const;
77+
78+
/// @copydoc TokenProvider::IsTokenResponseOK()
79+
bool IsTokenResponseOK() const;
80+
81+
private:
82+
std::shared_ptr<TokenProviderPrivate> impl_;
83+
};
84+
} // namespace internal
4485

4586
/**
4687
* @brief Provides the authentication tokens if the HERE platform
@@ -62,7 +103,7 @@ class TokenProvider {
62103
* the `TokenEndpoint` instance.
63104
*/
64105
explicit TokenProvider(Settings settings)
65-
: impl_(std::make_shared<TokenProviderImpl>(
106+
: impl_(std::make_shared<internal::TokenProviderImpl>(
66107
std::move(settings), std::chrono::seconds(MinimumValidity))) {}
67108

68109
/// A default copy constructor.
@@ -134,79 +175,12 @@ class TokenProvider {
134175
int GetHttpStatusCode() const { return impl_->GetHttpStatusCode(); }
135176

136177
private:
137-
class TokenProviderImpl {
138-
public:
139-
explicit TokenProviderImpl(Settings settings,
140-
std::chrono::seconds minimum_validity)
141-
: minimum_validity_{minimum_validity},
142-
token_(
143-
TokenEndpoint(std::move(settings)).RequestAutoRefreshingToken()) {
144-
}
145-
146-
/// @copydoc TokenProvider::operator()()
147-
std::string operator()() const {
148-
client::CancellationContext context;
149-
const auto response = GetResponse(context);
150-
return response ? response.GetResult().GetAccessToken() : "";
151-
}
152-
153-
/// @copydoc TokenProvider::operator()(client::CancellationContext&)
154-
client::OauthTokenResponse operator()(
155-
client::CancellationContext& context) const {
156-
const auto response = GetResponse(context);
157-
return response ? client::OauthTokenResponse(
158-
{response.GetResult().GetAccessToken(),
159-
response.GetResult().GetExpiryTime()})
160-
: client::OauthTokenResponse(response.GetError());
161-
}
162-
163-
/// @copydoc TokenProvider::GetErrorResponse()
164-
ErrorResponse GetErrorResponse() const {
165-
client::CancellationContext context;
166-
const auto response = GetResponse(context);
167-
return response ? response.GetResult().GetErrorResponse()
168-
: ErrorResponse{};
169-
}
170-
171-
/// @copydoc TokenProvider::GetHttpStatusCode()
172-
int GetHttpStatusCode() const {
173-
client::CancellationContext context;
174-
const auto response = GetResponse(context);
175-
return response ? response.GetResult().GetHttpStatus()
176-
: response.GetError().GetHttpStatusCode();
177-
}
178-
179-
/// Gets the token response from `AutoRefreshingToken` or requests a new
180-
/// token if the token response expired or is not present.
181-
TokenResponse GetResponse(client::CancellationContext& context) const {
182-
// Mutex is needed to prevent multiple authorization requests that can
183-
// happen when the token is not available, and multiple consumers
184-
// requested it.
185-
std::lock_guard<std::mutex> lock(request_mutex_);
186-
return token_.GetToken(context, minimum_validity_);
187-
}
188-
189-
/// Checks whether the available token response is valid.
190-
bool IsTokenResponseOK() const {
191-
client::CancellationContext context;
192-
193-
// Token response is successful if and only if we have a valid token.
194-
return GetResponse(context).IsSuccessful();
195-
}
196-
197-
private:
198-
std::chrono::seconds minimum_validity_{kDefaultMinimumValidity};
199-
AutoRefreshingToken token_;
200-
mutable std::mutex request_mutex_;
201-
};
202-
203-
std::shared_ptr<TokenProviderImpl> impl_;
178+
std::shared_ptr<internal::TokenProviderImpl> impl_;
204179
};
205180

206181
/// Provides the authentication tokens using the default minimum token
207182
/// validity.
208183
using TokenProviderDefault = TokenProvider<kDefaultMinimumValidity>;
209184

210-
PORTING_POP_WARNINGS()
211185
} // namespace authentication
212186
} // namespace olp

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20-
#include "olp/authentication/AutoRefreshingToken.h"
20+
#include "AutoRefreshingToken.h"
2121

2222
#include <chrono>
2323
#include <iostream>
2424

25-
#include "olp/authentication/TokenEndpoint.h"
26-
#include "olp/core/client/CancellationToken.h"
27-
#include "olp/core/logging/Log.h"
28-
#include "olp/core/porting/warning_disable.h"
25+
#include <olp/core/client/CancellationToken.h>
26+
#include <olp/core/logging/Log.h>
27+
#include "TokenEndpoint.h"
28+
#include "TokenRequest.h"
2929

3030
namespace {
3131
constexpr auto kLogTag = "authentication::AutoRefreshingToken";
@@ -47,8 +47,6 @@ std::chrono::steady_clock::time_point ComputeRefreshTime(
4747

4848
namespace olp {
4949
namespace authentication {
50-
PORTING_PUSH_WARNINGS()
51-
PORTING_CLANG_GCC_DISABLE_WARNING("-Wdeprecated-declarations")
5250

5351
struct AutoRefreshingToken::Impl {
5452
Impl(TokenEndpoint token_endpoint, TokenRequest token_request)
@@ -207,7 +205,6 @@ client::CancellationToken AutoRefreshingToken::GetToken(
207205
const std::chrono::seconds& minimum_validity) const {
208206
return impl_->GetToken(callback, minimum_validity);
209207
}
210-
PORTING_POP_WARNINGS()
211208

212209
} // namespace authentication
213210
} // namespace olp

olp-cpp-sdk-authentication/include/olp/authentication/AutoRefreshingToken.h renamed to olp-cpp-sdk-authentication/src/AutoRefreshingToken.h

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,22 @@
2323
#include <functional>
2424
#include <memory>
2525

26-
#include "AuthenticationApi.h"
26+
#include <olp/authentication/AuthenticationApi.h>
27+
#include <olp/core/client/CancellationContext.h>
28+
#include <olp/core/client/CancellationToken.h>
2729
#include "TokenEndpoint.h"
28-
#include "TokenRequest.h"
29-
#include "TokenResult.h"
30-
31-
#include "olp/core/client/CancellationContext.h"
32-
#include "olp/core/client/CancellationToken.h"
33-
#include "olp/core/porting/warning_disable.h"
3430

3531
namespace olp {
3632
namespace authentication {
3733

38-
static constexpr auto kDefaultMinimumValidity = 300ll;
39-
static constexpr auto kDefaultMinimumValiditySeconds =
40-
std::chrono::seconds(kDefaultMinimumValidity);
41-
static constexpr auto kForceRefresh = std::chrono::seconds(0);
42-
4334
/**
4435
* @brief Manages token requests.
4536
*
4637
* Requests a new token from the token endpoint and automatically refreshes it
4738
* when the token is about to expire.
4839
*/
49-
class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed by 10.2020.")
50-
AutoRefreshingToken {
40+
class AutoRefreshingToken {
5141
public:
52-
// Needed to avoid endless warnings from TokenRequest/TokenResult
53-
PORTING_PUSH_WARNINGS()
54-
PORTING_CLANG_GCC_DISABLE_WARNING("-Wdeprecated-declarations")
55-
5642
/**
5743
* @brief Specifies the callback signature that is required
5844
* when the get token request is completed.
@@ -79,8 +65,7 @@ class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed by 10.2020.")
7965
* Otherwise, returns the cached `TokenResponse` instance.
8066
*/
8167
TokenResponse GetToken(client::CancellationToken& cancellation_token,
82-
const std::chrono::seconds& minimum_validity =
83-
kDefaultMinimumValiditySeconds) const;
68+
const std::chrono::seconds& minimum_validity) const;
8469

8570
/**
8671
* @brief Synchronously gets a token that is always fresh.
@@ -101,8 +86,7 @@ class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed by 10.2020.")
10186
* Otherwise, returns the cached `TokenResponse` instance.
10287
*/
10388
TokenResponse GetToken(client::CancellationContext& context,
104-
const std::chrono::seconds& minimum_validity =
105-
kDefaultMinimumValiditySeconds) const;
89+
const std::chrono::seconds& minimum_validity) const;
10690

10791
/**
10892
* @brief Synchronously gets a token that is always fresh.
@@ -121,8 +105,7 @@ class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed by 10.2020.")
121105
* @return The `TokenResponse` instance if the old one is expired.
122106
* Otherwise, the cached `TokenResponse` instance.
123107
*/
124-
TokenResponse GetToken(const std::chrono::seconds& minimum_validity =
125-
kDefaultMinimumValiditySeconds) const;
108+
TokenResponse GetToken(const std::chrono::seconds& minimum_validity) const;
126109

127110
/**
128111
* @brief Asynchronously gets a token that is always fresh.
@@ -141,8 +124,7 @@ class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed by 10.2020.")
141124
*/
142125
client::CancellationToken GetToken(
143126
const GetTokenCallback& callback,
144-
const std::chrono::seconds& minimum_validity =
145-
kDefaultMinimumValiditySeconds) const;
127+
const std::chrono::seconds& minimum_validity) const;
146128

147129
/**
148130
* @brief Creates the `AutoRefreshingToken` instance.
@@ -153,8 +135,6 @@ class AUTHENTICATION_API OLP_SDK_DEPRECATED("Will be removed by 10.2020.")
153135
*/
154136
AutoRefreshingToken(TokenEndpoint token_endpoint, TokenRequest token_request);
155137

156-
PORTING_POP_WARNINGS()
157-
158138
private:
159139
struct Impl;
160140
std::shared_ptr<Impl> impl_;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20-
#include <olp/authentication/TokenEndpoint.h>
21-
22-
PORTING_PUSH_WARNINGS()
23-
PORTING_CLANG_GCC_DISABLE_WARNING("-Wdeprecated-declarations")
20+
#include "TokenEndpoint.h"
2421

2522
#include <olp/authentication/AuthenticationClient.h>
26-
#include <olp/authentication/AutoRefreshingToken.h>
2723
#include <olp/core/logging/Log.h>
24+
#include "AutoRefreshingToken.h"
2825
#include "TokenEndpointImpl.h"
26+
#include "TokenRequest.h"
2927

3028
namespace olp {
3129
namespace authentication {
@@ -82,6 +80,5 @@ AutoRefreshingToken TokenEndpoint::RequestAutoRefreshingToken(
8280
return AutoRefreshingToken(*this, token_request);
8381
}
8482

85-
PORTING_POP_WARNINGS()
8683
} // namespace authentication
8784
} // namespace olp

0 commit comments

Comments
 (0)