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