diff --git a/src/Auth0.php b/src/Auth0.php index 8bd65b37..a6026579 100644 --- a/src/Auth0.php +++ b/src/Auth0.php @@ -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'); @@ -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.'); diff --git a/tests/Auth0Test.php b/tests/Auth0Test.php index 70ce476e..d239a3c9 100644 --- a/tests/Auth0Test.php +++ b/tests/Auth0Test.php @@ -10,6 +10,7 @@ use Firebase\JWT\JWT; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; /** @@ -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()