Skip to content

Commit

Permalink
Correct issue with Google refreshToken() method. (#686)
Browse files Browse the repository at this point in the history
* fix: default to known refresh token on Google refresh

* test: assert refresh token works on Google refresh
  • Loading branch information
iBotPeaches authored Feb 8, 2024
1 parent 05af22c commit d2a8113
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Two/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ protected function getUserByToken($token)
return json_decode($response->getBody(), true);
}

/**
* {@inheritdoc}
*/
public function refreshToken($refreshToken)
{
$response = $this->getRefreshTokenResponse($refreshToken);

return new Token(
Arr::get($response, 'access_token'),
Arr::get($response, 'refresh_token', $refreshToken),
Arr::get($response, 'expires_in'),
explode($this->scopeSeparator, Arr::get($response, 'scope', ''))
);
}

/**
* {@inheritdoc}
*/
Expand Down
50 changes: 50 additions & 0 deletions tests/Fixtures/GoogleTestProviderStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Laravel\Socialite\Tests\Fixtures;

use Laravel\Socialite\Two\GoogleProvider;
use Laravel\Socialite\Two\User;
use Mockery as m;
use stdClass;

class GoogleTestProviderStub extends GoogleProvider
{
/**
* @var \GuzzleHttp\Client|\Mockery\MockInterface
*/
public $http;

protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('http://auth.url', $state);
}

protected function getTokenUrl()
{
return 'http://token.url';
}

protected function getUserByToken($token)
{
return ['id' => 'foo'];
}

protected function mapUserToObject(array $user)
{
return (new User)->map(['id' => $user['id']]);
}

/**
* Get a fresh instance of the Guzzle HTTP client.
*
* @return \GuzzleHttp\Client|\Mockery\MockInterface
*/
protected function getHttpClient()
{
if ($this->http) {
return $this->http;
}

return $this->http = m::mock(stdClass::class);
}
}
20 changes: 20 additions & 0 deletions tests/OAuthTwoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Socialite\Tests\Fixtures\FacebookTestProviderStub;
use Laravel\Socialite\Tests\Fixtures\GoogleTestProviderStub;
use Laravel\Socialite\Tests\Fixtures\OAuthTwoTestProviderStub;
use Laravel\Socialite\Tests\Fixtures\OAuthTwoWithPKCETestProviderStub;
use Laravel\Socialite\Two\InvalidStateException;
Expand Down Expand Up @@ -197,4 +198,23 @@ public function testUserRefreshesToken()
$this->assertSame(3600, $token->expiresIn);
$this->assertSame(['scope1', 'scope2'], $token->approvedScopes);
}

public function testUserRefreshesGoogleToken()
{
$request = Request::create('/');
$provider = new GoogleTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
$provider->http = m::mock(stdClass::class);
$provider->http->expects('post')->with('http://token.url', [
'headers' => ['Accept' => 'application/json'],
'form_params' => ['grant_type' => 'refresh_token', 'client_id' => 'client_id', 'client_secret' => 'client_secret', 'refresh_token' => 'refresh_token'],
])->andReturns($response = m::mock(stdClass::class));
$response->expects('getBody')->andReturns('{ "access_token" : "access_token", "expires_in" : 3600, "scope" : "scope1 scope2" }');
$token = $provider->refreshToken('refresh_token');

$this->assertInstanceOf(Token::class, $token);
$this->assertSame('access_token', $token->token);
$this->assertSame('refresh_token', $token->refreshToken);
$this->assertSame(3600, $token->expiresIn);
$this->assertSame(['scope1', 'scope2'], $token->approvedScopes);
}
}

0 comments on commit d2a8113

Please sign in to comment.