AuthenticationTokens.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <Authentication/AuthenticationTokens.h>
  9. namespace AWSClientAuth
  10. {
  11. //! Used to share authentication tokens to caller and to AWSCognitoAuthorizationController.
  12. AuthenticationTokens::AuthenticationTokens()
  13. {
  14. m_tokensExpireTimeStamp = AZStd::chrono::steady_clock::time_point::min();
  15. m_providerName = ProviderNameEnum::None;
  16. }
  17. AuthenticationTokens::AuthenticationTokens(const AuthenticationTokens& other)
  18. : m_accessToken(other.m_accessToken)
  19. , m_refreshToken(other.m_refreshToken)
  20. , m_openIdToken(other.m_openIdToken)
  21. , m_providerName(other.m_providerName)
  22. , m_tokensExpireTimeSeconds(other.m_tokensExpireTimeSeconds)
  23. , m_tokensExpireTimeStamp(other.m_tokensExpireTimeStamp)
  24. {
  25. }
  26. AuthenticationTokens::AuthenticationTokens(
  27. const AZStd::string& accessToken, const AZStd::string& refreshToken, const AZStd::string& openIdToken, const ProviderNameEnum& providerName, int tokensExpireTimeSeconds)
  28. : m_accessToken(accessToken)
  29. , m_refreshToken(refreshToken)
  30. , m_openIdToken(openIdToken)
  31. , m_providerName(providerName)
  32. , m_tokensExpireTimeSeconds(tokensExpireTimeSeconds)
  33. , m_tokensExpireTimeStamp(AZStd::chrono::steady_clock::now() + AZStd::chrono::seconds(tokensExpireTimeSeconds))
  34. {
  35. }
  36. //! Compares current time stamp to expired time stamp.
  37. //! @return True if current TS less than expiry TS.
  38. bool AuthenticationTokens::AreTokensValid() const
  39. {
  40. return AZStd::chrono::steady_clock::now() < m_tokensExpireTimeStamp;
  41. }
  42. //! @return Open id token from authentication.
  43. AZStd::string AuthenticationTokens::GetOpenIdToken() const
  44. {
  45. return m_openIdToken;
  46. }
  47. //! @return Access token from authentication.
  48. AZStd::string AuthenticationTokens::GetAccessToken() const
  49. {
  50. return m_accessToken;
  51. }
  52. //! @return Refresh token from authentication.
  53. AZStd::string AuthenticationTokens::GetRefreshToken() const
  54. {
  55. return m_refreshToken;
  56. }
  57. //! @return Provide name for the tokens.
  58. ProviderNameEnum AuthenticationTokens::GetProviderName() const
  59. {
  60. return m_providerName;
  61. }
  62. //! @return Expiration time in seconds.
  63. int AuthenticationTokens::GetTokensExpireTimeSeconds() const
  64. {
  65. return m_tokensExpireTimeSeconds;
  66. }
  67. void AuthenticationTokens::Reflect(AZ::ReflectContext* context)
  68. {
  69. auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  70. if (serializeContext)
  71. {
  72. serializeContext->Class<AuthenticationTokens>()
  73. ->Field("AccessToken", &AuthenticationTokens::m_accessToken)
  74. ->Field("OpenIdToken", &AuthenticationTokens::m_openIdToken)
  75. ->Field("RefreshToken", &AuthenticationTokens::m_refreshToken);
  76. }
  77. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  78. if (behaviorContext)
  79. {
  80. behaviorContext->Class<AuthenticationTokens>()
  81. ->Attribute(AZ::Script::Attributes::Category, "AWSClientAuth")
  82. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  83. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  84. ->Constructor()
  85. ->Constructor<const AuthenticationTokens&>()
  86. ->Property("AccessToken", BehaviorValueGetter(&AuthenticationTokens::m_accessToken), BehaviorValueSetter(&AuthenticationTokens::m_accessToken))
  87. ->Property("OpenIdToken", BehaviorValueGetter(&AuthenticationTokens::m_openIdToken), BehaviorValueSetter(&AuthenticationTokens::m_accessToken))
  88. ->Property("RefreshToken", BehaviorValueGetter(&AuthenticationTokens::m_refreshToken), BehaviorValueSetter(&AuthenticationTokens::m_accessToken));
  89. }
  90. }
  91. } // namespace AWSClientAuth