Skip to content
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
7 changes: 5 additions & 2 deletions src/Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,14 @@ public function exchange()
* Renews the access token and ID token using an existing refresh token.
* Scope "offline_access" must be declared in order to obtain refresh token for later token renewal.
*
* @param array $options Options for the token endpoint request.
* - options.scope Access token scope requested; optional.
*
* @throws CoreException If the Auth0 object does not have access token and refresh token
* @throws ApiException If the Auth0 API did not renew access and ID token properly
* @link https://auth0.com/docs/tokens/refresh-token/current
*/
public function renewTokens()
public function renewTokens(array $options = [])
{
if (! $this->accessToken) {
throw new CoreException('Can\'t renew the access token if there isn\'t one valid');
Expand All @@ -609,7 +612,7 @@ public function renewTokens()
throw new CoreException('Can\'t renew the access token if there isn\'t a refresh token available');
}

$response = $this->authentication->refresh_token( $this->refreshToken );
$response = $this->authentication->refresh_token( $this->refreshToken, $options );

if (empty($response['access_token']) || empty($response['id_token'])) {
throw new ApiException('Token did not refresh correctly. Access or ID token not provided.');
Expand Down
17 changes: 14 additions & 3 deletions tests/Auth0Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Firebase\JWT\JWT;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;

/**
Expand Down Expand Up @@ -268,28 +269,38 @@ public function testThatRenewTokensFailsIfNoAccessOrIdTokenReturned()
public function testThatRenewTokensSucceeds()
{
$id_token = JWT::encode( ['sub' => uniqid()], '__test_client_secret__' );

$request_history = [];
$mock = new MockHandler( [
// Code exchange response.
new Response( 200, self::$headers, '{"access_token":"1.2.3","refresh_token":"2.3.4"}' ),
// Refresh token response.
new Response( 200, self::$headers, '{"access_token":"__test_access_token__","id_token":"'.$id_token.'"}' ),
] );
$handler = HandlerStack::create($mock);
$handler->push( Middleware::history($request_history) );

$add_config = [
'skip_userinfo' => true,
'persist_access_token' => true,
'guzzle_options' => [ 'handler' => HandlerStack::create($mock) ]
'guzzle_options' => [ 'handler' => $handler ]
];
$auth0 = new Auth0( self::$baseConfig + $add_config );

$_GET['code'] = uniqid();

$this->assertTrue( $auth0->exchange() );
$auth0->renewTokens();
$auth0->renewTokens(['scope' => 'openid']);

$this->assertEquals( '__test_access_token__', $auth0->getAccessToken() );
$this->assertEquals( $id_token, $auth0->getIdToken() );

$renew_request = $request_history[1]['request'];
$renew_body = json_decode($renew_request->getBody(), true);
$this->assertEquals( 'openid', $renew_body['scope'] );
$this->assertEquals( '__test_client_secret__', $renew_body['client_secret'] );
$this->assertEquals( '__test_client_id__', $renew_body['client_id'] );
$this->assertEquals( '2.3.4', $renew_body['refresh_token'] );
$this->assertEquals( 'https://__test_domain__/oauth/token', (string) $renew_request->getUri() );
}

public function testThatGetLoginUrlUsesDefaultValues()
Expand Down