Skip to content

Commit

Permalink
Fixed #198
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 28, 2020
1 parent b08b037 commit cb1c93c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Exceptions/AuthorizeFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class AuthorizeFailedException extends Exception
* @param string $message
* @param array $body
*/
public function __construct($message, $body)
public function __construct(string $message, $body)
{
parent::__construct($message, -1);

$this->body = $body;
$this->body = (array) $body;
}
}
10 changes: 8 additions & 2 deletions src/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Overtrue\Socialite\Providers;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Stream;
use Overtrue\Socialite\Config;
use Overtrue\Socialite\Contracts\ProviderInterface;
use Overtrue\Socialite\Exceptions\AuthorizeFailedException;
Expand Down Expand Up @@ -86,7 +87,7 @@ public function userFromToken(string $token): User
* @param string $code
*
* @return array
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException|\GuzzleHttp\Exception\GuzzleException
*/
public function tokenFromCode(string $code): array
{
Expand Down Expand Up @@ -277,12 +278,17 @@ protected function getCodeFields(): array
*/
protected function normalizeAccessTokenResponse($response): array
{
if ($response instanceof Stream) {
$response->rewind();
$response = $response->getContents();
}

if (\is_string($response)) {
$response = json_decode($response, true) ?? [];
}

if (!\is_array($response)) {
throw new AuthorizeFailedException('Invalid token response', $response);
throw new AuthorizeFailedException('Invalid token response', [$response]);
}

if (empty($response[$this->accessTokenKey])) {
Expand Down
17 changes: 9 additions & 8 deletions src/Providers/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ protected function getTokenUrl(): string
}

/**
* @param string $code
* @param string $code
*
* @return string
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
*
*/
public function tokenFromCode($code): array
public function tokenFromCode(string $code): array
{
$response = $this->getHttpClient()->get(
$this->getTokenUrl(),
Expand All @@ -46,19 +46,20 @@ public function tokenFromCode($code): array
}

/**
* @param string $token
* @param array|null $query
* @param string $token
* @param array|null $query
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getUserByToken(string $token, ?array $query = []): array
{
$appSecretProof = hash_hmac('sha256', $token, $this->getConfig()->get('client_secret'));
$endpont = $this->graphUrl . '/' . $this->version . '/me?access_token=' . $token . '&appsecret_proof=' . $appSecretProof . '&fields=' .
$endpoint = $this->graphUrl . '/' . $this->version . '/me?access_token=' . $token . '&appsecret_proof=' . $appSecretProof . '&fields=' .
implode(',', $this->fields);

$response = $this->getHttpClient()->get(
$endpont,
$endpoint,
[
'headers' => [
'Accept' => 'application/json',
Expand Down

0 comments on commit cb1c93c

Please sign in to comment.