-
Notifications
You must be signed in to change notification settings - Fork 752
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract out tokens from being tied tightly to access
- Loading branch information
1 parent
a1d3f8a
commit 993d279
Showing
5 changed files
with
154 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* This file is part of the league/oauth2-client library | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Alex Bilbie <[email protected]> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* @link http://thephpleague.com/oauth2-client/ Documentation | ||
* @link https://packagist.org/packages/league/oauth2-client Packagist | ||
* @link https://github.com/thephpleague/oauth2-client GitHub | ||
*/ | ||
|
||
namespace League\OAuth2\Client\Token; | ||
|
||
abstract class AbstractToken { | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $expires; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $values = []; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private static $timeNow; | ||
|
||
/** | ||
* Constructs an access token. | ||
* | ||
* @param array $options An array of options returned by the service provider | ||
* in the access token request. The `access_token` option is required. | ||
* @throws InvalidArgumentException if `access_token` is not provided in `$options`. | ||
*/ | ||
public function __construct(array $options = []) { | ||
// 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 (isset($options['expires_in'])) { | ||
if (!is_numeric($options['expires_in'])) { | ||
throw new \InvalidArgumentException('expires_in value must be an integer'); | ||
} | ||
|
||
$this->expires = $options['expires_in'] != 0 ? $this->getTimeNow() + $options['expires_in'] : 0; | ||
} elseif (!empty($options['expires'])) { | ||
// Some providers supply the seconds until expiration rather than | ||
// the exact timestamp. Take a best guess at which we received. | ||
$expires = $options['expires']; | ||
|
||
if (!$this->isExpirationTimestamp($expires)) { | ||
$expires += $this->getTimeNow(); | ||
} | ||
|
||
$this->expires = $expires; | ||
} | ||
} | ||
|
||
/** | ||
* Set the time now. This should only be used for testing purposes. | ||
* | ||
* @param int $timeNow the time in seconds since epoch | ||
* @return void | ||
*/ | ||
public static function setTimeNow($timeNow) | ||
{ | ||
self::$timeNow = $timeNow; | ||
} | ||
|
||
/** | ||
* Reset the time now if it was set for test purposes. | ||
* | ||
* @return void | ||
*/ | ||
public static function resetTimeNow() | ||
{ | ||
self::$timeNow = null; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTimeNow() | ||
{ | ||
return self::$timeNow ? self::$timeNow : time(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* This file is part of the league/oauth2-client library | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Alex Bilbie <[email protected]> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* @link http://thephpleague.com/oauth2-client/ Documentation | ||
* @link https://packagist.org/packages/league/oauth2-client Packagist | ||
* @link https://github.com/thephpleague/oauth2-client GitHub | ||
*/ | ||
|
||
namespace League\OAuth2\Client\Token; | ||
|
||
class IdToken extends AbstractToken implements ResourceOwnerTokenInterface { | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __construct(array $options = []) { | ||
parent::__construct($options); | ||
if (!empty($options['resource_owner_id'])) { | ||
$this->resourceOwnerId = $options['resource_owner_id']; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* This file is part of the league/oauth2-client library | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Alex Bilbie <[email protected]> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* @link http://thephpleague.com/oauth2-client/ Documentation | ||
* @link https://packagist.org/packages/league/oauth2-client Packagist | ||
* @link https://github.com/thephpleague/oauth2-client GitHub | ||
*/ | ||
|
||
namespace League\OAuth2\Client\Token; | ||
|
||
interface ResourceOwnerTokenInterface | ||
{ | ||
/** | ||
* Returns the resource owner identifier, if defined. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getResourceOwnerId(); | ||
} |