AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CBLAuthDelegate.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef CBLAUTHDELEGATE_CBLAUTHDELEGATE_H_
17 #define CBLAUTHDELEGATE_CBLAUTHDELEGATE_H_
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <thread>
25 #include <unordered_set>
26 
35 
36 #include "CBLAuthDelegate/CBLAuthDelegateStorageInterface.h"
37 #include "CBLAuthDelegate/CBLAuthRequesterInterface.h"
38 
39 namespace alexaClientSDK {
40 namespace authorization {
41 namespace cblAuthDelegate {
42 
51 public:
69  static std::shared_ptr<AuthDelegateInterface> createAuthDelegateInterface(
70  const std::shared_ptr<avsCommon::utils::configuration::ConfigurationNode>& configuration,
71  const std::shared_ptr<registrationManager::CustomerDataManagerInterface>& customerDataManager,
72  const std::shared_ptr<CBLAuthDelegateStorageInterface>& storage,
73  const std::shared_ptr<CBLAuthRequesterInterface>& authRequester,
74  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> httpPost,
75  const std::shared_ptr<avsCommon::utils::DeviceInfo>& deviceInfo);
76 
95  static std::unique_ptr<CBLAuthDelegate> create(
97  std::shared_ptr<registrationManager::CustomerDataManagerInterface> customerDataManager,
98  std::shared_ptr<CBLAuthDelegateStorageInterface> storage,
99  std::shared_ptr<CBLAuthRequesterInterface> authRequester,
100  std::shared_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> httpPost = nullptr,
101  std::shared_ptr<avsCommon::utils::DeviceInfo> deviceInfo = nullptr);
102 
108  CBLAuthDelegate(const CBLAuthDelegate& rhs) = delete;
109 
114 
120  CBLAuthDelegate& operator=(const CBLAuthDelegate& rhs) = delete;
121 
124  void addAuthObserver(std::shared_ptr<avsCommon::sdkInterfaces::AuthObserverInterface> observer) override;
125  void removeAuthObserver(std::shared_ptr<avsCommon::sdkInterfaces::AuthObserverInterface> observer) override;
126  std::string getAuthToken() override;
127  void onAuthFailure(const std::string& token) override;
129 
132  void clearData() override;
134 
135 private:
140  enum class FlowState {
143  STARTING,
148  REQUESTING_CODE_PAIR,
154  REQUESTING_TOKEN,
158  REFRESHING_TOKEN,
161  STOPPING
162  };
163 
175  std::shared_ptr<registrationManager::CustomerDataManagerInterface> customerDataManager,
176  std::shared_ptr<CBLAuthDelegateStorageInterface> storage,
177  std::shared_ptr<CBLAuthRequesterInterface> authRequester,
178  std::shared_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> httpPost);
179 
188  bool init(
190  const std::shared_ptr<avsCommon::utils::DeviceInfo>& deviceInfo);
191 
195  void stop();
196 
200  void handleAuthorizationFlow();
201 
207  FlowState handleStarting();
208 
214  FlowState handleRequestingCodePair();
215 
221  FlowState handleRequestingToken();
222 
228  FlowState handleRefreshingToken();
229 
235  FlowState handleStopping();
236 
243 
250 
257 
266 
279  bool expiresImmediately);
280 
286  void setAuthState(avsCommon::sdkInterfaces::AuthObserverInterface::State newState);
287 
294 
300  void setRefreshToken(const std::string& refreshToken);
301 
305  void clearRefreshToken();
306 
312  bool isStopping();
313 
318  std::shared_ptr<CBLAuthDelegateStorageInterface> m_storage;
319 
324  std::shared_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> m_httpPost;
325 
327  std::shared_ptr<CBLAuthRequesterInterface> m_authRequester;
328 
330  std::unique_ptr<acsdkAuthorization::lwa::LWAAuthorizationConfiguration> m_configuration;
331 
333  bool m_isStopping;
334 
336  std::mutex m_mutex;
337 
339  std::unordered_set<std::shared_ptr<avsCommon::sdkInterfaces::AuthObserverInterface>> m_observers;
340 
342  std::string m_accessToken;
343 
346 
349 
351  std::string m_deviceCode;
352 
354  std::string m_userCode;
355 
357  std::thread m_authorizationFlowThread;
358 
360  std::chrono::steady_clock::time_point m_codePairExpirationTime;
361 
363  std::chrono::steady_clock::time_point m_tokenExpirationTime;
364 
366  std::string m_refreshToken;
367 
369  std::condition_variable m_wake;
370 
372  std::chrono::steady_clock::time_point m_timeToRefresh;
373 
375  std::chrono::steady_clock::time_point m_requestTime;
376 
378  int m_retryCount;
379 
381  bool m_newRefreshToken;
382 
384  bool m_authFailureReported;
385 };
386 
387 } // namespace cblAuthDelegate
388 } // namespace authorization
389 } // namespace alexaClientSDK
390 
391 #endif // CBLAUTHDELEGATE_CBLAUTHDELEGATE_H_
void onAuthFailure(const std::string &token) override
void addAuthObserver(std::shared_ptr< avsCommon::sdkInterfaces::AuthObserverInterface > observer) override
::std::string string
Definition: gtest-port.h:1097
State
The enum State describes the state of authorization.
Definition: AuthObserverInterface.h:34
static std::unique_ptr< CBLAuthDelegate > create(const avsCommon::utils::configuration::ConfigurationNode &configuration, std::shared_ptr< registrationManager::CustomerDataManagerInterface > customerDataManager, std::shared_ptr< CBLAuthDelegateStorageInterface > storage, std::shared_ptr< CBLAuthRequesterInterface > authRequester, std::shared_ptr< avsCommon::utils::libcurlUtils::HttpPostInterface > httpPost=nullptr, std::shared_ptr< avsCommon::utils::DeviceInfo > deviceInfo=nullptr)
CBLAuthDelegate & operator=(const CBLAuthDelegate &rhs)=delete
Error
The enum Error encodes possible errors which may occur when changing state.
Definition: AuthObserverInterface.h:48
void removeAuthObserver(std::shared_ptr< avsCommon::sdkInterfaces::AuthObserverInterface > observer) override
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
static std::shared_ptr< AuthDelegateInterface > createAuthDelegateInterface(const std::shared_ptr< avsCommon::utils::configuration::ConfigurationNode > &configuration, const std::shared_ptr< registrationManager::CustomerDataManagerInterface > &customerDataManager, const std::shared_ptr< CBLAuthDelegateStorageInterface > &storage, const std::shared_ptr< CBLAuthRequesterInterface > &authRequester, std::unique_ptr< avsCommon::utils::libcurlUtils::HttpPostInterface > httpPost, const std::shared_ptr< avsCommon::utils::DeviceInfo > &deviceInfo)

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0