Skip to content

Commit

Permalink
use guzzle instance from AbstractProvider to get apple auth keys inst…
Browse files Browse the repository at this point in the history
…ead of file_get_contents
  • Loading branch information
Juris Malinens committed Nov 27, 2020
1 parent 754769b commit 96bb3ab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Provider/Apple.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function __construct(array $options = [], array $collaborators = [])
*/
protected function createAccessToken(array $response, AbstractGrant $grant)
{
return new AppleAccessToken($response);
return new AppleAccessToken($response, $this->getHttpClient());
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Token/AppleAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use GuzzleHttp\ClientInterface;
use InvalidArgumentException;

class AppleAccessToken extends AccessToken
Expand All @@ -23,6 +24,11 @@ class AppleAccessToken extends AccessToken
*/
protected $isPrivateEmail;

/**
* @var ClientInterface
*/
protected $httpClient;

/**
* Constructs an access token.
*
Expand All @@ -32,8 +38,10 @@ class AppleAccessToken extends AccessToken
*
* @throws \Exception
*/
public function __construct(array $options = [])
public function __construct(array $options = [], $httpClient)
{
$this->httpClient = $httpClient;

if (array_key_exists('refresh_token', $options)) {
if (empty($options['id_token'])) {
throw new InvalidArgumentException('Required option not passed: "id_token"');
Expand Down Expand Up @@ -84,7 +92,9 @@ public function __construct(array $options = [])
*/
protected function getAppleKey()
{
return JWK::parseKeySet(json_decode(file_get_contents('https://appleid.apple.com/auth/keys'), true));
$response = $this->httpClient->request('GET', 'https://appleid.apple.com/auth/keys');

return JWK::parseKeySet(json_decode($response->getBody()->getContents(), true));
}

/**
Expand Down
22 changes: 20 additions & 2 deletions test/src/Token/AppleAccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace League\OAuth2\Client\Test\Token;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use League\OAuth2\Client\Token\AppleAccessToken;
use PHPUnit\Framework\TestCase;
use Mockery as m;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class AppleAccessTokenTest extends TestCase
{
Expand Down Expand Up @@ -39,7 +43,7 @@ public function testCreatingAccessToken()
'expires_in' => 3600,
'refresh_token' => 'abc.0.def',
'id_token' => 'something'
]);
], $this->getClient(1));
$this->assertEquals('something', $accessToken->getIdToken());
$this->assertEquals('123.abc.123', $accessToken->getResourceOwnerId());
$this->assertEquals('access_token', $accessToken->getToken());
Expand All @@ -51,7 +55,21 @@ public function testCreatingRefreshToken()
'access_token' => 'access_token',
'token_type' => 'Bearer',
'expires_in' => 3600
]);
], $this->getClient(0));
$this->assertEquals('access_token', $refreshToken->getToken());
}

private function getClient($times)
{
$client = m::mock('GuzzleHttp\ClientInterface');
if ($times > 0) {
$client->shouldReceive('request')
->times($times)
->withArgs(['GET', 'https://appleid.apple.com/auth/keys'])
->andReturn(new Response())
;
}

return $client;
}
}

0 comments on commit 96bb3ab

Please sign in to comment.