2013-01-29 20:06:24 +04:00
|
|
|
<?php
|
|
|
|
|
2013-05-28 14:00:24 +04:00
|
|
|
namespace League\OAuth2\Client\Token;
|
|
|
|
|
2013-01-29 20:41:01 +04:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2013-02-26 15:33:00 +04:00
|
|
|
class AccessToken
|
2013-01-29 20:06:24 +04:00
|
|
|
{
|
2013-01-29 20:08:47 +04:00
|
|
|
/**
|
|
|
|
* @var string accessToken
|
|
|
|
*/
|
2013-01-29 20:41:01 +04:00
|
|
|
public $accessToken;
|
2013-01-29 20:08:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int expires
|
|
|
|
*/
|
2013-01-29 20:41:01 +04:00
|
|
|
public $expires;
|
2013-01-29 20:08:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string refreshToken
|
|
|
|
*/
|
2013-01-29 20:41:01 +04:00
|
|
|
public $refreshToken;
|
2013-01-29 20:08:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string uid
|
|
|
|
*/
|
2013-01-29 20:41:01 +04:00
|
|
|
public $uid;
|
2013-01-29 20:08:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the token, expiry, etc values.
|
|
|
|
*
|
2014-04-24 08:34:10 +04:00
|
|
|
* @param array $options token options
|
|
|
|
* @return void
|
2013-01-29 20:08:47 +04:00
|
|
|
*/
|
|
|
|
public function __construct(array $options = null)
|
|
|
|
{
|
2014-04-24 22:36:42 +04:00
|
|
|
if (! isset($options['access_token'])) {
|
2014-05-03 14:55:30 +04:00
|
|
|
throw new \InvalidArgumentException(
|
2014-11-09 00:30:40 +03:00
|
|
|
'Required option not passed: access_token'.PHP_EOL
|
|
|
|
.print_r($options, true)
|
2014-05-03 14:55:30 +04:00
|
|
|
);
|
2013-01-29 20:08:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->accessToken = $options['access_token'];
|
|
|
|
|
|
|
|
// Some providers (not many) give the uid here, so lets take it
|
|
|
|
isset($options['uid']) and $this->uid = $options['uid'];
|
|
|
|
|
2013-07-12 10:36:09 +04:00
|
|
|
// Vkontakte uses user_id instead of uid
|
2013-01-29 20:08:47 +04:00
|
|
|
isset($options['user_id']) and $this->uid = $options['user_id'];
|
|
|
|
|
2013-07-12 10:36:09 +04:00
|
|
|
// Mailru uses x_mailru_vid instead of uid
|
2013-01-29 20:08:47 +04:00
|
|
|
isset($options['x_mailru_vid']) and $this->uid = $options['x_mailru_vid'];
|
|
|
|
|
2014-09-12 16:49:34 +04:00
|
|
|
//Battle.net uses accountId instead of uid
|
|
|
|
isset($options['accountId']) and $this->uid = $options['accountId'];
|
|
|
|
|
2014-10-23 05:31:27 +04:00
|
|
|
// We need to know when the token expires. Show preference to
|
2014-06-27 03:49:41 +04:00
|
|
|
// 'expires_in' since it is defined in RFC6749 Section 5.1.
|
|
|
|
// Defer to 'expires' if it is provided instead.
|
|
|
|
if (!empty($options['expires_in'])) {
|
|
|
|
$this->expires = time() + ((int) $options['expires_in']);
|
|
|
|
} elseif (!empty($options['expires'])) {
|
|
|
|
// Some providers supply the seconds until expiration rather than
|
|
|
|
// the exact timestamp. Take a best guess at which we received.
|
2014-10-27 12:42:39 +03:00
|
|
|
$expires = $options['expires'];
|
|
|
|
$expiresInFuture = $expires > time();
|
|
|
|
$this->expires = $expiresInFuture ? $expires : time() + ((int) $expires);
|
2014-06-27 03:49:41 +04:00
|
|
|
}
|
2013-01-29 20:08:47 +04:00
|
|
|
|
|
|
|
// Grab a refresh token so we can update access tokens when they expires
|
|
|
|
isset($options['refresh_token']) and $this->refreshToken = $options['refresh_token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the token key.
|
|
|
|
*
|
2014-04-24 08:34:10 +04:00
|
|
|
* @return string
|
2013-01-29 20:08:47 +04:00
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return (string) $this->accessToken;
|
|
|
|
}
|
2013-01-29 20:41:01 +04:00
|
|
|
}
|