olp-cpp-sdk  1.22.0
TokenProvider.h
1 /*
2  * Copyright (C) 2019-2023 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <chrono>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 #include <olp/authentication/AuthenticationCredentials.h>
28 #include <olp/authentication/ErrorResponse.h>
29 #include <olp/authentication/Settings.h>
30 #include <olp/authentication/Types.h>
31 #include <olp/core/client/CancellationContext.h>
32 #include <olp/core/client/OauthToken.h>
33 #include <olp/core/http/HttpStatusCode.h>
34 
35 namespace olp {
36 namespace authentication {
37 
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 
50 class AUTHENTICATION_API TokenProviderImpl {
51  public:
60  TokenProviderImpl(Settings settings, std::chrono::seconds minimum_validity);
61 
64  client::CancellationContext& context) const;
65 
68 
70  int GetHttpStatusCode() const;
71 
74 
76  bool IsTokenResponseOK() const;
77 
78  private:
79  std::shared_ptr<TokenProviderPrivate> impl_;
80 };
81 } // namespace internal
82 
93 template <uint64_t MinimumValidity>
95  public:
102  explicit TokenProvider(Settings settings)
103  : impl_(std::make_shared<internal::TokenProviderImpl>(
104  std::move(settings), std::chrono::seconds(MinimumValidity))) {}
105 
107  TokenProvider(const TokenProvider& other) = default;
108 
110  TokenProvider(TokenProvider&& other) noexcept = default;
111 
113  TokenProvider& operator=(const TokenProvider& other) = default;
114 
116  TokenProvider& operator=(TokenProvider&& other) noexcept = default;
117 
126  operator bool() const { return impl_->IsTokenResponseOK(); }
127 
137  client::CancellationContext& context) const {
138  return impl_->operator()(context);
139  }
140 
147  ErrorResponse GetErrorResponse() const { return impl_->GetErrorResponse(); }
148 
155  int GetHttpStatusCode() const { return impl_->GetHttpStatusCode(); }
156 
157  private:
158  std::shared_ptr<internal::TokenProviderImpl> impl_;
159 };
160 
164 
165 } // namespace authentication
166 } // namespace olp
Provides the authentication tokens if the HERE platform user credentials are valid.
Definition: TokenProvider.h:94
int GetHttpStatusCode() const
Gets the HTTP status code of the last request.
Definition: TokenProvider.h:155
TokenProvider(const TokenProvider &other)=default
A default copy constructor.
client::OauthTokenResponse operator()(client::CancellationContext &context) const
Returns the access token or an error.
Definition: TokenProvider.h:136
TokenProvider(TokenProvider &&other) noexcept=default
A default move constructor.
TokenProvider(Settings settings)
Creates the TokenProvider instance with the settings parameter.
Definition: TokenProvider.h:102
TokenProvider & operator=(TokenProvider &&other) noexcept=default
A default move assignment operator.
ErrorResponse GetErrorResponse() const
Allows the olp::client::ApiError object associated with the last request to be accessed if the token ...
Definition: TokenProvider.h:147
TokenProvider & operator=(const TokenProvider &other)=default
A default copy assignment operator.
client::OauthTokenResponse operator()(client::CancellationContext &context) const
TokenResponse GetResponse(client::CancellationContext &context) const
TokenProviderImpl(Settings settings, std::chrono::seconds minimum_validity)
Creates the TokenProviderImpl instance.
int GetHttpStatusCode() const
Gets the HTTP status code of the last request.
ErrorResponse GetErrorResponse() const
Allows the olp::client::ApiError object associated with the last request to be accessed if the token ...
Represents a request outcome.
Definition: ApiResponse.h:65
A wrapper that manages the cancellation state of an asynchronous operation in a thread-safe way.
Definition: CancellationContext.h:40
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
Detailed descriptions of errors returned as a response to an authentication operation.
Definition: ErrorResponse.h:36
Configures the TokenEndpoint instance.
Definition: Settings.h:50