AuthenticationProviderBus.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <Authentication/AuthenticationTokens.h>
  11. namespace AWSClientAuth
  12. {
  13. //! Abstract class for authentication provider requests.
  14. class IAuthenticationProviderRequests
  15. {
  16. public:
  17. AZ_TYPE_INFO(IAuthenticationProviderRequests, "{4A8017C4-2742-48C4-AF07-1177CBF5E6E9}");
  18. //! Parse the settings file for required settings for authentication providers. Instantiate and initialize authentication providers
  19. //! @param providerNames List of provider names to instantiate and initialize for Authentication.
  20. //! @return bool True: if all providers initialized successfully. False: If any provider fails initialization.
  21. virtual bool Initialize(const AZStd::vector<ProviderNameEnum>& providerNames) = 0;
  22. //! Checks if user is signed in.
  23. //! If access tokens are available and not expired.
  24. //! @param providerName Provider to check signed in for
  25. //! @return bool True if valid access token available, else False
  26. virtual bool IsSignedIn(const ProviderNameEnum& providerName) = 0;
  27. //! [Deprecated] Get cached tokens from last successful sign-in for the provider.
  28. //! To enhance security, only the refresh token is cached and will be returned by this function.
  29. //! If you need the access or ID tokens, all authentication tokens (access token, ID token and refresh token)
  30. //! can be retrieved by implementing custom handlers for AuthenticationProviderNotifications in your project code.
  31. //! @param providerName Provider to get authentication tokens.
  32. //! @return AuthenticationTokens tokens from successful authentication.
  33. virtual AuthenticationTokens GetAuthenticationTokens(const ProviderNameEnum& providerName) = 0;
  34. // Below methods have corresponding notifications for success and failures.
  35. //! Call sign in endpoint for provider password grant flow.
  36. //! @param providerName Provider to call sign in.
  37. //! @param username Username to use to for sign in.
  38. //! @param password Password to use to for sign in.
  39. virtual void PasswordGrantSingleFactorSignInAsync(const ProviderNameEnum& providerName, const AZStd::string& username, const AZStd::string& password) = 0;
  40. //! Call sign in endpoint for provider password grant multi factor authentication flow.
  41. //! @param providerName Provider to call MFA sign in.
  42. //! @param username Username to use for MFA sign in.
  43. //! @param password Password to use for MFA sign in.
  44. virtual void PasswordGrantMultiFactorSignInAsync(const ProviderNameEnum& providerName, const AZStd::string& username, const AZStd::string& password) = 0;
  45. //! Call confirm endpoint for provider password grant multi factor authentication flow .
  46. //! @param providerName Provider to call MFA confirm sign in.
  47. //! @param username Username to use for MFA confirm.
  48. //! @param confirmationCode Confirmation code (sent to email/text) to use for MFA confirm.
  49. virtual void PasswordGrantMultiFactorConfirmSignInAsync(const ProviderNameEnum& providerName, const AZStd::string& username, const AZStd::string& confirmationCode) = 0;
  50. //! Call code-pair endpoint for provider device grant flow.
  51. //! @param providerName Provider to call device sign in.
  52. virtual void DeviceCodeGrantSignInAsync(const ProviderNameEnum& providerName) = 0;
  53. //! Call tokens endpoint for provider device grant flow.
  54. //! @param providerName Provider to call device confirm sign in.
  55. virtual void DeviceCodeGrantConfirmSignInAsync(const ProviderNameEnum& providerName) = 0;
  56. //! Call refresh endpoint for provider refresh grant flow.
  57. //! @param providerName Provider to call refresh tokens.
  58. virtual void RefreshTokensAsync(const ProviderNameEnum& providerName) = 0;
  59. //! Call refresh token if token not valid. If token valid, fires corresponding event.
  60. //! @param providerName Provider to get access token for.
  61. //! events: OnRefreshTokensSuccess, OnRefreshTokensFail
  62. virtual void GetTokensWithRefreshAsync(const ProviderNameEnum& providerName) = 0;
  63. //! Signs user out.
  64. //! Clears all cached tokens.
  65. //! @param providerName Provider to sign out.
  66. //! @return bool True: Successfully sign out.
  67. virtual bool SignOut(const ProviderNameEnum& providerName) = 0;
  68. //////////////////////////////////////////////////////////////////////////
  69. };
  70. //! Authentication Request bus for different supported providers.
  71. class AuthenticationProviderRequests
  72. : public AZ::EBusTraits
  73. {
  74. public:
  75. //////////////////////////////////////////////////////////////////////////
  76. // EBusTraits overrides
  77. using MutexType = AZ::NullMutex;
  78. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  79. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  80. //////////////////////////////////////////////////////////////////////////
  81. };
  82. using AuthenticationProviderRequestBus = AZ::EBus<IAuthenticationProviderRequests, AuthenticationProviderRequests>;
  83. //! Notification bus for Authentication Request bus.
  84. class AuthenticationProviderNotifications
  85. : public AZ::EBusTraits
  86. {
  87. public:
  88. //////////////////////////////////////////////////////////////////////////
  89. // EBusTraits overrides
  90. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
  91. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  92. //////////////////////////////////////////////////////////////////////////
  93. ////////////////////////////ss//////////////////////////////////////////////
  94. // Notifications interface
  95. //! Event for PasswordGrantSingleFactorSignIn success.
  96. //! @param authenticationToken Tokens on successful sign in.
  97. virtual void OnPasswordGrantSingleFactorSignInSuccess(const AuthenticationTokens& authenticationToken)
  98. {
  99. AZ_UNUSED(authenticationToken);
  100. }
  101. //! Event for PasswordGrantSingleFactorSignIn fail.
  102. //! @param error Error message
  103. virtual void OnPasswordGrantSingleFactorSignInFail(const AZStd::string& error)
  104. {
  105. AZ_UNUSED(error);
  106. }
  107. //! Event for PasswordGrantMultiFactorSignIn success.
  108. //! Event use to notify user to take corresponding challenge action.
  109. virtual void OnPasswordGrantMultiFactorSignInSuccess()
  110. {
  111. }
  112. //! Event for PasswordGrantMultiFactorSignIn fail.
  113. //! @param error Error message
  114. virtual void OnPasswordGrantMultiFactorSignInFail(const AZStd::string& error)
  115. {
  116. AZ_UNUSED(error);
  117. }
  118. //! Event for PasswordGrantMultiFactorConfirm success.
  119. //! @param authenticationToken Tokens on successful sign in.
  120. virtual void OnPasswordGrantMultiFactorConfirmSignInSuccess(const AuthenticationTokens& authenticationToken)
  121. {
  122. AZ_UNUSED(authenticationToken);
  123. }
  124. //! Event for PasswordGrantMultiFactorConfirm fail.
  125. //! @param error Error message
  126. virtual void OnPasswordGrantMultiFactorConfirmSignInFail(const AZStd::string& error)
  127. {
  128. AZ_UNUSED(error);
  129. }
  130. //! Event for DeviceCodeGrantSignIn success.
  131. //! Event use to notify user to take open verification url and enter displayed code.
  132. //! @param userCode Unique code generated for user for the session.
  133. //! @param verificationUrl Verification URL to enter user code in after signing in for the provider.
  134. //! @param codeExpiresInSeconds Code expiry in seconds.
  135. virtual void OnDeviceCodeGrantSignInSuccess(const AZStd::string& userCode, const AZStd::string& verificationUrl, int codeExpiresInSeconds)
  136. {
  137. AZ_UNUSED(userCode);
  138. AZ_UNUSED(verificationUrl);
  139. AZ_UNUSED(codeExpiresInSeconds);
  140. }
  141. //! Event for DeviceCodeGrantSignIn fail.
  142. //! @param error Error message
  143. virtual void OnDeviceCodeGrantSignInFail(const AZStd::string& error)
  144. {
  145. AZ_UNUSED(error);
  146. }
  147. //! Event for DeviceCodeGrantConfirmSignIn success.
  148. //! @param authenticationToken Tokens on successful sign in..
  149. virtual void OnDeviceCodeGrantConfirmSignInSuccess(const AuthenticationTokens& authenticationToken)
  150. {
  151. AZ_UNUSED(authenticationToken);
  152. }
  153. //! Event for DeviceCodeGrantConfirmSignIn fail.
  154. //! @param error Error message
  155. virtual void OnDeviceCodeGrantConfirmSignInFail(const AZStd::string& error)
  156. {
  157. AZ_UNUSED(error);
  158. }
  159. //! Event for RequestAccessTokenWithRefresh success.
  160. //! @param authenticationToken Tokens on successful sign in.
  161. virtual void OnRefreshTokensSuccess(const AuthenticationTokens& authenticationToken)
  162. {
  163. AZ_UNUSED(authenticationToken);
  164. }
  165. //! Event for RequestAccessTokenWithRefresh fail.
  166. //! @param error Error message
  167. virtual void OnRefreshTokensFail(const AZStd::string& error)
  168. {
  169. AZ_UNUSED(error);
  170. }
  171. //! Event for Sing out.
  172. //! @param providerName provider that signed out.
  173. virtual void OnSignOut(const ProviderNameEnum& provideName)
  174. {
  175. AZ_UNUSED(provideName);
  176. }
  177. //////////////////////////////////////////////////////////////////////////
  178. };
  179. using AuthenticationProviderNotificationBus = AZ::EBus<AuthenticationProviderNotifications>;
  180. } // namespace AWSClientAuth