AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
LWAAuthorizationAdapter.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 ACSDKAUTHORIZATION_LWA_LWAAUTHORIZATIONADAPTER_H_
17 #define ACSDKAUTHORIZATION_LWA_LWAAUTHORIZATIONADAPTER_H_
18 
19 #include <condition_variable>
20 #include <thread>
21 
32 
33 namespace alexaClientSDK {
34 namespace acsdkAuthorization {
35 namespace lwa {
36 
49  , public std::enable_shared_from_this<LWAAuthorizationAdapter> {
50 public:
53 
65  static std::shared_ptr<LWAAuthorizationAdapter> create(
66  const std::shared_ptr<avsCommon::utils::configuration::ConfigurationNode>& configuration,
67  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> httpPost,
68  const std::shared_ptr<avsCommon::utils::DeviceInfo>& deviceInfo,
69  const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::LWAAuthorizationStorageInterface>& storage,
70  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpGetInterface> httpGet = nullptr,
71  const std::string& adapterId = "");
72 
75  bool authorizeUsingCBL(
76  const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface>& observer) override;
78  const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface>& observer) override;
80 
83  std::string getId() override;
85 
88  std::string getAuthToken() override;
89  void reset() override;
90  void onAuthFailure(const std::string& authToken) override;
92  std::shared_ptr<acsdkAuthorizationInterfaces::AuthorizationInterface> getAuthorizationInterface() override;
94  const std::shared_ptr<acsdkAuthorizationInterfaces::AuthorizationManagerInterface>& manager) override;
96 
97 private:
99  enum class TokenExchangeMethod {
104  NONE,
106  CBL
107  };
108 
112  struct RefreshTokenResponse {
114  std::string refreshToken;
115 
117  std::string accessToken;
118 
120  std::chrono::steady_clock::time_point requestTime;
121 
123  std::chrono::seconds expiration;
124 
129  bool isRefreshTokenVerified;
130 
132  RefreshTokenResponse() :
133  refreshToken{""},
134  accessToken{""},
135  requestTime{std::chrono::steady_clock::now()},
136  expiration{std::chrono::seconds::zero()},
137  isRefreshTokenVerified{true} {
138  }
139 
145  std::chrono::steady_clock::time_point getExpirationTime() const {
146  return requestTime + expiration;
147  }
148  };
149 
153  enum class FlowState {
157  IDLE,
170  REQUESTING_TOKEN,
175  REFRESHING_TOKEN,
180  CLEARING_DATA,
185  STOPPING
186  };
187 
190  const std::string& adapterId,
191  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> httpPost,
192  const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::LWAAuthorizationStorageInterface>& storage,
193  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpGetInterface> httpGet);
194 
202  bool init(
204  const std::shared_ptr<avsCommon::utils::DeviceInfo>& deviceInfo);
205 
211  FlowState retrievePersistedData();
212 
218  avsCommon::utils::libcurlUtils::HTTPResponse requestRefresh(std::chrono::steady_clock::time_point* requestTime);
219 
225  FlowState handleIdle();
226 
232  FlowState handleRequestingToken();
233 
239  FlowState handleRefreshingToken();
240 
246  FlowState handleClearingData();
247 
253  FlowState handleStopping();
254 
260  void handleAuthorizationFlow(FlowState state);
261 
264  bool authorizeUsingCBLHelper(
265  const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface>& observer,
266  bool requestCustomerProfile);
267 
276 
284  avsCommon::utils::libcurlUtils::HTTPResponse sendCodePairRequest();
285 
294 
303  RefreshTokenResponse* tokenResponse);
304 
311  avsCommon::utils::libcurlUtils::HTTPResponse sendTokenRequest(std::chrono::steady_clock::time_point* requestTime);
313 
324  bool expiresImmediately,
325  struct RefreshTokenResponse* tokenResponse);
326 
332  bool shouldStopRetrying();
333 
340  bool shouldStopRetryingLocked();
341 
347  bool isShuttingDown();
348 
352  void stop();
353 
358  void resetAuthMethodLocked();
359 
366  void setRefreshTokenResponse(const RefreshTokenResponse& response, bool persist = true);
367 
375  void setRefreshTokenResponseLocked(const RefreshTokenResponse& response, bool persist = true);
376 
383  void updateStateAndNotifyManager(
386 
394  bool getCustomerProfile(const std::string& accessToken);
395 
397  std::mutex m_mutex;
398 
403  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface> m_httpPost;
404 
409  std::unique_ptr<avsCommon::utils::libcurlUtils::HttpGetInterface> m_httpGet;
410 
412  std::shared_ptr<acsdkAuthorizationInterfaces::lwa::LWAAuthorizationStorageInterface> m_storage;
413 
415  std::unique_ptr<LWAAuthorizationConfiguration> m_configuration;
416 
419 
422 
424  std::string m_userId;
425 
427  std::condition_variable m_wake;
428 
430  std::thread m_authorizationFlowThread;
431 
435  std::chrono::steady_clock::time_point m_codePairExpirationTime;
436 
438  std::string m_deviceCode;
439 
441  std::string m_userCode;
442 
444  std::chrono::seconds m_tokenRequestInterval;
445 
447  std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface> m_cblRequester;
448 
450  bool m_requestCustomerProfile;
452 
454  bool m_authFailureReported;
455 
457  TokenExchangeMethod m_authMethod;
458 
460  RefreshTokenResponse m_refreshTokenResponse;
461 
463  std::shared_ptr<acsdkAuthorizationInterfaces::AuthorizationManagerInterface> m_manager;
464 
466  bool m_isShuttingDown;
467 
469  bool m_isClearingData;
470 
472  const std::string m_adapterId;
473 };
474 
475 } // namespace lwa
476 } // namespace acsdkAuthorization
477 } // namespace alexaClientSDK
478 
479 #endif // ACSDKAUTHORIZATION_LWA_LWAAUTHORIZATIONADAPTER_H_
bool authorizeUsingCBLWithCustomerProfile(const std::shared_ptr< acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface > &observer) override
::std::string string
Definition: gtest-port.h:1097
static std::shared_ptr< LWAAuthorizationAdapter > create(const std::shared_ptr< avsCommon::utils::configuration::ConfigurationNode > &configuration, std::unique_ptr< avsCommon::utils::libcurlUtils::HttpPostInterface > httpPost, const std::shared_ptr< avsCommon::utils::DeviceInfo > &deviceInfo, const std::shared_ptr< acsdkAuthorizationInterfaces::lwa::LWAAuthorizationStorageInterface > &storage, std::unique_ptr< avsCommon::utils::libcurlUtils::HttpGetInterface > httpGet=nullptr, const std::string &adapterId="")
State
The enum State describes the state of authorization.
Definition: AuthObserverInterface.h:34
avsCommon::sdkInterfaces::AuthObserverInterface::FullState onAuthorizationManagerReady(const std::shared_ptr< acsdkAuthorizationInterfaces::AuthorizationManagerInterface > &manager) override
An aggregated structure to simplify working with State and Error.
Definition: AuthObserverInterface.h:80
bool authorizeUsingCBL(const std::shared_ptr< acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface > &observer) override
Error
The enum Error encodes possible errors which may occur when changing state.
Definition: AuthObserverInterface.h:48
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::shared_ptr< acsdkAuthorizationInterfaces::AuthorizationInterface > getAuthorizationInterface() override
void onAuthFailure(const std::string &authToken) override
avsCommon::sdkInterfaces::AuthObserverInterface::FullState getState() override

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