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 GoogleProvider getUserByToken() method #689

Merged
merged 2 commits into from
Feb 11, 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
5 changes: 5 additions & 0 deletions src/Two/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Socialite\Two;

use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

Expand Down Expand Up @@ -56,6 +57,10 @@ protected function getUserByToken($token)
],
]);

if ($response->getBody() instanceof Stream) {
return json_decode($response->getBody()->getContents(), true);
}

return json_decode($response->getBody(), true);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/GoogleProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Laravel\Socialite\Tests;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Request;
use Laravel\Socialite\Tests\Fixtures\GoogleTestProviderStub;
use Laravel\Socialite\Two\User;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class GoogleProviderTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();

m::close();
}

public function test_it_can_map_a_user_from_an_access_token()
{
$request = Request::create('/');

$provider = new GoogleTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');

$provider->http = m::mock(Client::class);

$provider->http->allows('get')->with('https://www.googleapis.com/oauth2/v3/userinfo', [
RequestOptions::QUERY => [
'prettyPrint' => 'false',
],
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer fake-token',
],
])->andReturns($response = m::mock(ResponseInterface::class));

$response->allows('getBody')->andReturns(m::mock(StreamInterface::class));

$user = $provider->userFromToken('fake-token');

$this->assertInstanceOf(User::class, $user);
}
}
Loading