AccessToken.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * This file is part of the league/oauth2-client library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. * @link http://thephpleague.com/oauth2-client/ Documentation
  11. * @link https://packagist.org/packages/league/oauth2-client Packagist
  12. * @link https://github.com/thephpleague/oauth2-client GitHub
  13. */
  14. namespace League\OAuth2\Client\Token;
  15. use InvalidArgumentException;
  16. use RuntimeException;
  17. /**
  18. * Represents an access token.
  19. *
  20. * @link http://tools.ietf.org/html/rfc6749#section-1.4 Access Token (RFC 6749, §1.4)
  21. */
  22. class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface
  23. {
  24. /**
  25. * @var string
  26. */
  27. protected $accessToken;
  28. /**
  29. * @var int
  30. */
  31. protected $expires;
  32. /**
  33. * @var string
  34. */
  35. protected $refreshToken;
  36. /**
  37. * @var string
  38. */
  39. protected $resourceOwnerId;
  40. /**
  41. * @var array
  42. */
  43. protected $values = [];
  44. /**
  45. * @var int
  46. */
  47. private static $timeNow;
  48. /**
  49. * Set the time now. This should only be used for testing purposes.
  50. *
  51. * @param int $timeNow the time in seconds since epoch
  52. * @return void
  53. */
  54. public static function setTimeNow($timeNow)
  55. {
  56. self::$timeNow = $timeNow;
  57. }
  58. /**
  59. * Reset the time now if it was set for test purposes.
  60. *
  61. * @return void
  62. */
  63. public static function resetTimeNow()
  64. {
  65. self::$timeNow = null;
  66. }
  67. /**
  68. * @return int
  69. */
  70. public function getTimeNow()
  71. {
  72. return self::$timeNow ? self::$timeNow : time();
  73. }
  74. /**
  75. * Constructs an access token.
  76. *
  77. * @param array $options An array of options returned by the service provider
  78. * in the access token request. The `access_token` option is required.
  79. * @throws InvalidArgumentException if `access_token` is not provided in `$options`.
  80. */
  81. public function __construct(array $options = [])
  82. {
  83. if (empty($options['access_token'])) {
  84. throw new InvalidArgumentException('Required option not passed: "access_token"');
  85. }
  86. $this->accessToken = $options['access_token'];
  87. if (!empty($options['resource_owner_id'])) {
  88. $this->resourceOwnerId = $options['resource_owner_id'];
  89. }
  90. if (!empty($options['refresh_token'])) {
  91. $this->refreshToken = $options['refresh_token'];
  92. }
  93. // We need to know when the token expires. Show preference to
  94. // 'expires_in' since it is defined in RFC6749 Section 5.1.
  95. // Defer to 'expires' if it is provided instead.
  96. if (isset($options['expires_in'])) {
  97. if (!is_numeric($options['expires_in'])) {
  98. throw new \InvalidArgumentException('expires_in value must be an integer');
  99. }
  100. $this->expires = $options['expires_in'] != 0 ? $this->getTimeNow() + $options['expires_in'] : 0;
  101. } elseif (!empty($options['expires'])) {
  102. // Some providers supply the seconds until expiration rather than
  103. // the exact timestamp. Take a best guess at which we received.
  104. $expires = $options['expires'];
  105. if (!$this->isExpirationTimestamp($expires)) {
  106. $expires += $this->getTimeNow();
  107. }
  108. $this->expires = $expires;
  109. }
  110. // Capture any additional values that might exist in the token but are
  111. // not part of the standard response. Vendors will sometimes pass
  112. // additional user data this way.
  113. $this->values = array_diff_key($options, array_flip([
  114. 'access_token',
  115. 'resource_owner_id',
  116. 'refresh_token',
  117. 'expires_in',
  118. 'expires',
  119. ]));
  120. }
  121. /**
  122. * Check if a value is an expiration timestamp or second value.
  123. *
  124. * @param integer $value
  125. * @return bool
  126. */
  127. protected function isExpirationTimestamp($value)
  128. {
  129. // If the given value is larger than the original OAuth 2 draft date,
  130. // assume that it is meant to be a (possible expired) timestamp.
  131. $oauth2InceptionDate = 1349067600; // 2012-10-01
  132. return ($value > $oauth2InceptionDate);
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function getToken()
  138. {
  139. return $this->accessToken;
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function getRefreshToken()
  145. {
  146. return $this->refreshToken;
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function getExpires()
  152. {
  153. return $this->expires;
  154. }
  155. /**
  156. * @inheritdoc
  157. */
  158. public function getResourceOwnerId()
  159. {
  160. return $this->resourceOwnerId;
  161. }
  162. /**
  163. * @inheritdoc
  164. */
  165. public function hasExpired()
  166. {
  167. $expires = $this->getExpires();
  168. if (empty($expires)) {
  169. throw new RuntimeException('"expires" is not set on the token');
  170. }
  171. return $expires < time();
  172. }
  173. /**
  174. * @inheritdoc
  175. */
  176. public function getValues()
  177. {
  178. return $this->values;
  179. }
  180. /**
  181. * @inheritdoc
  182. */
  183. public function __toString()
  184. {
  185. return (string) $this->getToken();
  186. }
  187. /**
  188. * @inheritdoc
  189. */
  190. public function jsonSerialize()
  191. {
  192. $parameters = $this->values;
  193. if ($this->accessToken) {
  194. $parameters['access_token'] = $this->accessToken;
  195. }
  196. if ($this->refreshToken) {
  197. $parameters['refresh_token'] = $this->refreshToken;
  198. }
  199. if ($this->expires) {
  200. $parameters['expires'] = $this->expires;
  201. }
  202. if ($this->resourceOwnerId) {
  203. $parameters['resource_owner_id'] = $this->resourceOwnerId;
  204. }
  205. return $parameters;
  206. }
  207. }