Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensures in-memory access token is updated when refresh token is used #1926

Merged
merged 4 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Google/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ public function __construct(array $config = array())
$this->setScopes($this->config['scopes']);
unset($this->config['scopes']);
}

// Set a default token callback to update the in-memory access token
if (is_null($this->config['token_callback'])) {
$this->config['token_callback'] = function ($cacheKey, $newAccessToken) {
$this->setAccessToken(
[
'access_token' => $newAccessToken,
'created' => time(),
]
);
};
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,36 @@ public function testTokenCallback()
$this->assertTrue($called);
}

public function testDefaultTokenCallback()
{
$this->onlyPhp55AndAbove();
$this->checkToken();

$client = $this->getClient();
$accessToken = $client->getAccessToken();

if (!isset($accessToken['refresh_token'])) {
$this->markTestSkipped('Refresh Token required');
}

// make the auth library think the token is expired
$accessToken['expires_in'] = 0;
$client->setAccessToken($accessToken);

// make a silly request to obtain a new token (it's ok if it fails)
$http = $client->authorize();
try {
$http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire');
} catch (Exception $e) {}

// Assert the in-memory token has been updated
$newToken = $client->getAccessToken();
$this->assertNotEquals(
$accessToken['access_token'],
$newToken['access_token']
);
}

public function testExecuteWithFormat()
{
$this->onlyGuzzle6();
Expand Down