Skip to content

Commit

Permalink
Merge pull request #98 from bshaffer/adds_get_last_received_token
Browse files Browse the repository at this point in the history
adds getLastReceivedToken and tests
  • Loading branch information
bshaffer committed Mar 2, 2016
2 parents 965e6fb + cb2d7d6 commit 84add3c
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 28 deletions.
24 changes: 24 additions & 0 deletions src/Credentials/AppIdentityCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
*/
class AppIdentityCredentials extends CredentialsLoader
{
/**
* Result of fetchAuthToken
*/
protected $lastReceivedToken;

/**
* Array of OAuth2 scopes to be requested
*/
private $scope;

public function __construct($scope = array())
Expand Down Expand Up @@ -104,10 +112,26 @@ public function fetchAuthToken(callable $httpHandler = null)
$scope = is_array($this->scope) ? $this->scope : explode(' ', $this->scope);

$token = AppIdentityService::getAccessToken($scope);
$this->lastReceivedToken = $token;

return $token;
}

/**
* Implements FetchAuthTokenInterface#getLastReceivedToken.
*/
public function getLastReceivedToken()
{
if ($this->lastReceivedToken) {
return [
'access_token' => $this->lastReceivedToken['access_token'],
'expires_at' => $this->lastReceivedToken['expiration_time'],
];
}

return null;
}

/**
* Implements FetchAuthTokenInterface#getCacheKey.
*
Expand Down
26 changes: 25 additions & 1 deletion src/Credentials/GCECredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class GCECredentials extends CredentialsLoader
*/
private $isOnGce = false;

/**
* Result of fetchAuthToken
*/
protected $lastReceivedToken;

/**
* The full uri for accessing the default token.
*/
Expand Down Expand Up @@ -158,6 +163,9 @@ public function fetchAuthToken(callable $httpHandler = null)
throw new \Exception('Invalid JSON response');
}

// store this so we can retrieve it later
$this->lastReceivedToken = $json;

return $json;
}

Expand All @@ -166,7 +174,23 @@ public function fetchAuthToken(callable $httpHandler = null)
*
* @return 'GOOGLE_AUTH_PHP_GCE'
*/
public function getCacheKey() {
public function getCacheKey()
{
return 'GOOGLE_AUTH_PHP_GCE';
}

/**
* Implements FetchAuthTokenInterface#getLastReceivedToken.
*/
public function getLastReceivedToken()
{
if ($this->lastReceivedToken) {
return [
'access_token' => $this->lastReceivedToken['access_token'],
'expires_at' => $this->lastReceivedToken['expiration_time'],
];
}

return null;
}
}
16 changes: 14 additions & 2 deletions src/Credentials/ServiceAccountCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use Google\Auth\CredentialsLoader;
use Google\Auth\OAuth2;
use GuzzleHttp\Psr7;

/**
* ServiceAccountCredentials supports authorization using a Google service
Expand Down Expand Up @@ -56,6 +55,11 @@
*/
class ServiceAccountCredentials extends CredentialsLoader
{
/**
* The OAuth2 instance used to conduct authorization.
*/
protected $auth;

/**
* Create a new ServiceAccountCredentials.
*
Expand All @@ -77,7 +81,7 @@ public function __construct(
if (!file_exists($jsonKey)) {
throw new \InvalidArgumentException('file does not exist');
}
$jsonKeyStream = Psr7\stream_for(file_get_contents($jsonKey));
$jsonKeyStream = file_get_contents($jsonKey);
if (!$jsonKey = json_decode($jsonKeyStream, true)) {
throw new \LogicException('invalid json for auth config');
}
Expand Down Expand Up @@ -121,6 +125,14 @@ public function getCacheKey()
return $key;
}

/**
* Implements FetchAuthTokenInterface#getLastReceivedToken.
*/
public function getLastReceivedToken()
{
return $this->auth->getLastReceivedToken();
}

/**
* Updates metadata with the authorization token
*
Expand Down
28 changes: 26 additions & 2 deletions src/Credentials/ServiceAccountJwtAccessCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,28 @@
*/
class ServiceAccountJwtAccessCredentials extends CredentialsLoader
{

/**
* The OAuth2 instance used to conduct authorization.
*/
protected $auth;

/**
* Create a new ServiceAccountJwtAccessCredentials.
*
* @param array $jsonKey JSON credentials.
* @param string|array $jsonKey JSON credential file path or JSON credentials
* as an associative array
*/
public function __construct(array $jsonKey) {
public function __construct($jsonKey) {
if (is_string($jsonKey)) {
if (!file_exists($jsonKey)) {
throw new \InvalidArgumentException('file does not exist');
}
$jsonKeyStream = file_get_contents($jsonKey);
if (!$jsonKey = json_decode($jsonKeyStream, true)) {
throw new \LogicException('invalid json for auth config');
}
}
if (!array_key_exists('client_email', $jsonKey)) {
throw new \InvalidArgumentException(
'json key is missing the client_email field');
Expand Down Expand Up @@ -96,4 +112,12 @@ public function getCacheKey()
{
return $this->auth->getCacheKey();
}

/**
* Implements FetchAuthTokenInterface#getLastReceivedToken.
*/
public function getLastReceivedToken()
{
return $this->auth->getLastReceivedToken();
}
}
34 changes: 24 additions & 10 deletions src/Credentials/UserRefreshCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use Google\Auth\CredentialsLoader;
use Google\Auth\OAuth2;
use GuzzleHttp\Psr7;

/**
* Authenticates requests using User Refresh credentials.
Expand All @@ -34,25 +33,32 @@
*/
class UserRefreshCredentials extends CredentialsLoader
{
/**
* The OAuth2 instance used to conduct authorization.
*/
protected $auth;

/**
* Create a new UserRefreshCredentials.
*
* @param string|array $scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
*
* @param array $jsonKey JSON credentials.
*
* @param string $jsonKeyPath the path to a file containing JSON credentials. If
* jsonKey is set, it is ignored.
* @param string|array $jsonKey JSON credential file path or JSON credentials
* as an associative array
*/
public function __construct(
$scope,
$jsonKey,
$jsonKeyPath = null
$jsonKey
) {
if (is_null($jsonKey)) {
$jsonKeyStream = Psr7\stream_for(file_get_contents($jsonKeyPath));
$jsonKey = json_decode($jsonKeyStream->getContents(), true);
if (is_string($jsonKey)) {
if (!file_exists($jsonKey)) {
throw new \InvalidArgumentException('file does not exist');
}
$jsonKeyStream = file_get_contents($jsonKey);
if (!$jsonKey = json_decode($jsonKeyStream, true)) {
throw new \LogicException('invalid json for auth config');
}
}
if (!array_key_exists('client_id', $jsonKey)) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -90,4 +96,12 @@ public function getCacheKey()
{
return $this->auth->getClientId() . ':' . $this->auth->getCacheKey();
}

/**
* Implements FetchAuthTokenInterface#getLastReceivedToken.
*/
public function getLastReceivedToken()
{
return $this->auth->getLastReceivedToken();
}
}
5 changes: 0 additions & 5 deletions src/CredentialsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ private static function isOnWindows()
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

/**
* The OAuth2 instance used to conduct authorization.
*/
protected $auth;

/**
* Create a credentials instance from the path specified in the environment.
*
Expand Down
16 changes: 14 additions & 2 deletions src/FetchAuthTokenInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ interface FetchAuthTokenInterface
{

/**
* Fetchs the auth tokens based on the current state.
* Fetches the auth tokens based on the current state.
*
* @param callable $httpHandler callback which delivers psr7 request
* @return array a hash of auth tokens
*/
public function fetchAuthToken(callable $httpHandler = null);


/**
* Obtains a key that can used to cache the results of #fetchAuthToken.
*
Expand All @@ -40,4 +39,17 @@ public function fetchAuthToken(callable $httpHandler = null);
* @return string a key that may be used to cache the auth token.
*/
public function getCacheKey();

/**
* Returns an associative array with the token and
* expiration time.
*
* @return null|array {
* The last received access token.
*
* @type string $access_token The access token string.
* @type int $expires_at The time the token expires as a UNIX timestamp.
* }
*/
public function getLastReceivedToken();
}
18 changes: 15 additions & 3 deletions src/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,6 @@ class OAuth2 implements FetchAuthTokenInterface
* - state
* An arbitrary string designed to allow the client to maintain state.
*
* - code
* The authorization code received from the authorization server.
*
* - redirectUri
* The redirection URI used in the initial request.
*
Expand Down Expand Up @@ -1068,6 +1065,21 @@ public function setRefreshToken($refreshToken)
$this->refreshToken = $refreshToken;
}

/**
* The expiration of the last received token
*/
public function getLastReceivedToken()
{
if ($token = $this->getAccessToken()) {
return [
'access_token' => $token,
'expires_at' => $this->getExpiresAt(),
];
}

return null;
}

/**
* @todo handle uri as array
* @param string $uri
Expand Down
6 changes: 3 additions & 3 deletions tests/Credentials/UserRefreshCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ public function testShouldFailIfJsonDoesNotHaveRefreshToken()
}

/**
* @expectedException PHPUnit_Framework_Error_Warning
* @expectedException InvalidArgumentException
*/
public function testFailsToInitalizeFromANonExistentFile()
{
$keyFile = __DIR__ . '/../fixtures' . '/does-not-exist-private.json';
new UserRefreshCredentials('scope/1', null, $keyFile);
new UserRefreshCredentials('scope/1', $keyFile);
}

public function testInitalizeFromAFile()
{
$keyFile = __DIR__ . '/../fixtures2' . '/private.json';
$this->assertNotNull(
new UserRefreshCredentials('scope/1', null, $keyFile)
new UserRefreshCredentials('scope/1', $keyFile)
);
}
}
Expand Down
Loading

0 comments on commit 84add3c

Please sign in to comment.