SsoToken.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Aws\Token;
  3. /**
  4. * Token that comes from the SSO provider
  5. */
  6. class SsoToken extends Token
  7. {
  8. private $refreshToken;
  9. private $clientId;
  10. private $clientSecret;
  11. private $registrationExpiresAt;
  12. private $region;
  13. private $startUrl;
  14. /**
  15. * Constructs a new SSO token object, with the specified AWS
  16. * token
  17. *
  18. * @param string $token Security token to use
  19. * @param int $expires UNIX timestamp for when the token expires
  20. * @param int $refreshToken An opaque string returned by the sso-oidc service
  21. * @param int $clientId The client ID generated when performing the registration portion of the OIDC authorization flow
  22. * @param int $clientSecret The client secret generated when performing the registration portion of the OIDC authorization flow
  23. * @param int $registrationExpiresAt The expiration time of the client registration (clientId and clientSecret)
  24. * @param int $region The configured sso_region for the profile that credentials are being resolved for
  25. * @param int $startUrl The configured sso_start_url for the profile that credentials are being resolved for
  26. */
  27. public function __construct(
  28. $token,
  29. $expires,
  30. $refreshToken = null,
  31. $clientId = null,
  32. $clientSecret = null,
  33. $registrationExpiresAt = null,
  34. $region = null,
  35. $startUrl = null
  36. ) {
  37. parent::__construct($token, $expires);
  38. $this->refreshToken = $refreshToken;
  39. $this->clientId = $clientId;
  40. $this->clientSecret = $clientSecret;
  41. $this->registrationExpiresAt = $registrationExpiresAt;
  42. $this->region = $region;
  43. $this->startUrl = $startUrl;
  44. }
  45. /**
  46. * @return bool
  47. */
  48. public function isExpired()
  49. {
  50. if (isset($this->registrationExpiresAt)
  51. && time() >= $this->registrationExpiresAt
  52. ) {
  53. return false;
  54. }
  55. return $this->expires !== null && time() >= $this->expires;
  56. }
  57. /**
  58. * @return string|null
  59. */
  60. public function getRefreshToken()
  61. {
  62. return $this->refreshToken;
  63. }
  64. /**
  65. * @return string|null
  66. */
  67. public function getClientId()
  68. {
  69. return $this->clientId;
  70. }
  71. /**
  72. * @return string|null
  73. */
  74. public function getClientSecret()
  75. {
  76. return $this->clientSecret;
  77. }
  78. /**
  79. * @return int|null
  80. */
  81. public function getRegistrationExpiresAt()
  82. {
  83. return $this->registrationExpiresAt;
  84. }
  85. /**
  86. * @return string|null
  87. */
  88. public function getRegion()
  89. {
  90. return $this->region;
  91. }
  92. /**
  93. * @return string|null
  94. */
  95. public function getStartUrl()
  96. {
  97. return $this->startUrl;
  98. }
  99. /**
  100. * Creates an instance of SsoToken from a token data.
  101. *
  102. * @param $tokenData
  103. *
  104. * @return SsoToken
  105. */
  106. public static function fromTokenData($tokenData): SsoToken
  107. {
  108. return new SsoToken(
  109. $tokenData['accessToken'],
  110. \strtotime($tokenData['expiresAt']),
  111. isset($tokenData['refreshToken']) ? $tokenData['refreshToken'] : null,
  112. isset($tokenData['clientId']) ? $tokenData['clientId'] : null,
  113. isset($tokenData['clientSecret']) ? $tokenData['clientSecret'] : null,
  114. isset($tokenData['registrationExpiresAt']) ? $tokenData['registrationExpiresAt'] : null,
  115. isset($tokenData['region']) ? $tokenData['region'] : null,
  116. isset($tokenData['startUrl']) ? $tokenData['startUrl'] : null
  117. );
  118. }
  119. }