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

Correct issue with Google refreshToken() method #686

Merged
merged 2 commits into from
Feb 8, 2024
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
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);
}
}
Loading