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
3835namespace olp {
3936namespace 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.
208183using TokenProviderDefault = TokenProvider<kDefaultMinimumValidity >;
209184
210- PORTING_POP_WARNINGS ()
211185} // namespace authentication
212186} // namespace olp
0 commit comments