AWSCognitoAuthorizationBus.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <AzCore/std/string/string.h>
  11. #include <Authorization/ClientAuthAWSCredentials.h>
  12. #include <aws/core/auth/AWSCredentialsProvider.h>
  13. namespace AWSClientAuth
  14. {
  15. //! Abstract class for AWS Cognito authorization requests.
  16. class IAWSCognitoAuthorizationRequests
  17. {
  18. public:
  19. AZ_TYPE_INFO(IAWSCognitoAuthorizationRequests, "{F60A2C40-48F5-49A1-ABFA-A08D0DD4ECCC}");
  20. //! Initializes settings for Cognito identity pool from settings registry.
  21. //! @param settingsRegistryPath Path for the settings registry file to use.
  22. virtual bool Initialize() = 0;
  23. //! Once credentials provider are set they cannot be reset. So recreates new Cognito credentials provider on reset.
  24. //! Service clients need to be created with the new AWSCredentialsProvider after reset.
  25. virtual void Reset() = 0;
  26. //! Get cached Cognito identity id from last successful GetId call to Cognito.
  27. //! @return Cognito identity id
  28. virtual AZStd::string GetIdentityId() = 0;
  29. //! Checks if logins are persisted.
  30. //! @return True if logins persists else false.
  31. virtual bool HasPersistedLogins() = 0;
  32. //! Returns AWSCredentialsProvider to initialize up AWS Native SDK clients.
  33. //! std::shared_ptr to allow sharing ownership with AWS Native SDK.
  34. //! @return std::shared_ptr for Aws::Auth::AWSCredentialProvider.
  35. virtual std::shared_ptr<Aws::Auth::AWSCredentialsProvider> GetCognitoCredentialsProvider() = 0;
  36. //! Returns anonymous AWSCredentialsProvider to initialize up AWS Native SDK clients.
  37. //! std::shared_ptr to allow sharing ownership with AWS Native SDK.
  38. //! @return std::shared_ptr for Aws::Auth::AWSCredentialProvider.
  39. virtual std::shared_ptr<Aws::Auth::AWSCredentialsProvider> GetAnonymousCognitoCredentialsProvider() = 0;
  40. //! Get cached AWS credentials or fetch credentials from Cognito.
  41. //! Will fetch authenticated role credentials if login are cached else fetches unauthenticated role credentials if enabled in Cognito Identity pool.
  42. //! If multiple logins are persisted and no cached credentials found, GetId call to Cognito will link the login provider to same identity.
  43. virtual void RequestAWSCredentialsAsync() = 0;
  44. };
  45. //! Request bus to handle AWS Cognito Identity pool authorization.
  46. class AWSCognitoAuthorizationRequests
  47. : public AZ::EBusTraits
  48. {
  49. public:
  50. //////////////////////////////////////////////////////////////////////////
  51. // EBusTraits overrides
  52. using MutexType = AZ::NullMutex;
  53. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  54. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  55. //////////////////////////////////////////////////////////////////////////
  56. };
  57. using AWSCognitoAuthorizationRequestBus = AZ::EBus<IAWSCognitoAuthorizationRequests, AWSCognitoAuthorizationRequests>;
  58. //! Notification bus for corresponding Authorization Request bus.
  59. class AWSCognitoAuthorizationNotifications
  60. : public AZ::EBusTraits
  61. {
  62. public:
  63. //////////////////////////////////////////////////////////////////////////
  64. // EBusTraits overrides
  65. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Multiple;
  66. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  67. //////////////////////////////////////////////////////////////////////////
  68. //////////////////////////////////////////////////////////////////////////
  69. // Notifications interface
  70. //! Event called on request AWS credentials success.
  71. //! @param awsCredentials Credentials for authenticated role associated with Cognito identity pool.
  72. virtual void OnRequestAWSCredentialsSuccess(const ClientAuthAWSCredentials& awsCredentials)
  73. {
  74. AZ_UNUSED(awsCredentials);
  75. }
  76. //! Event called on request AWS credentials fail.
  77. //! @param error Error message
  78. virtual void OnRequestAWSCredentialsFail(const AZStd::string& error)
  79. {
  80. AZ_UNUSED(error);
  81. }
  82. //////////////////////////////////////////////////////////////////////////
  83. };
  84. using AWSCognitoAuthorizationNotificationBus = AZ::EBus<AWSCognitoAuthorizationNotifications>;
  85. } // namespace AWSClientAuth