oauth2-client/src/AccessToken.php

66 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2013-01-29 20:06:24 +04:00
<?php
namespace OAuth2;
2013-02-26 15:33:00 +04:00
class AccessToken
2013-01-29 20:06:24 +04:00
{
public $accessToken;
public $expires;
public $refreshToken;
public $uid;
2013-01-29 20:08:47 +04:00
/**
* Sets the token, expiry, etc values.
*
* @param array $options token options
2014-04-24 08:34:10 +04:00
* @return void
2013-01-29 20:08:47 +04:00
*/
public function __construct(array $options = null)
{
if (!isset($options['access_token']))
{
2014-05-03 14:55:30 +04:00
throw new \InvalidArgumentException(
'Required option not passed: access_token in '.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'];
if (!empty($options['uid']))
{
$this->uid = $options['uid'];
}
2013-01-29 20:08:47 +04:00
if (!empty($options['refresh_token']))
{
$this->refreshToken = $options['refresh_token'];
}
// We need to know when the token expires. Show preference to
// '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);
}
2013-01-29 20:08:47 +04:00
}
/**
* 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;
}
}