This commit is contained in:
2025-10-20 14:10:54 +02:00
parent 75ca8fd840
commit d2c1970ef8
732 changed files with 101915 additions and 2 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signature;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signer;
final class KeyDumpSigner implements Signer
{
public function getAlgorithmId()
{
return 'keydump';
}
public function modifyHeader(array &$headers)
{
$headers['alg'] = $this->getAlgorithmId();
}
public function verify($expected, $payload, $key)
{
return $expected === $key->contents();
}
public function sign($payload, $key)
{
return new Signature($key->contents());
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signer;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signer\Key;
final class KeyDumpSigner implements Signer
{
public function algorithmId(): string
{
return 'keydump';
}
public function sign(string $payload, Key $key): string
{
return $key->contents();
}
public function verify(string $expected, string $payload, Key $key): bool
{
return $expected === $key->contents();
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
if (!InstalledVersions::satisfies(new VersionParser(), 'lcobucci/jwt', '^1 || ^2 || ^3')) {
require_once __DIR__ . '/../ext/KeyDumpSigner8.php';
} else {
require_once __DIR__ . '/../ext/KeyDumpSigner5.php';
}

View File

@@ -0,0 +1,226 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\Provider;
use Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface;
use Pshowsso\Scope68f5e85e9608b\GuzzleHttp\Psr7\Response;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Configuration;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signer\Key;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signer\Hmac\Sha256;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\Apple;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\AppleResourceOwner;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\KeyDumpSigner;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Token\AccessToken;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Token\AppleAccessToken;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Tool\QueryBuilderTrait;
use PHPUnit\Framework\TestCase;
use Mockery as m;
class AppleTest extends TestCase
{
use QueryBuilderTrait;
/**
* @return Apple
*/
private function getProvider()
{
return new Apple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFileId' => 'mock.file.id', 'keyFilePath' => __DIR__ . '/p256-private-key.p8', 'redirectUri' => 'none']);
}
public function testMissingTeamIdDuringInstantiationThrowsException()
{
$this->expectException('InvalidArgumentException');
new Apple(['clientId' => 'mock.example', 'keyFileId' => 'mock.file.id', 'keyFilePath' => __DIR__ . '/p256-private-key.p8', 'redirectUri' => 'none']);
}
public function testMissingKeyFileIdDuringInstantiationThrowsException()
{
$this->expectException('InvalidArgumentException');
new Apple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFilePath' => __DIR__ . '/p256-private-key.p8', 'redirectUri' => 'none']);
}
public function testMissingKeyFilePathDuringInstantiationThrowsException()
{
$this->expectException('InvalidArgumentException');
new Apple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFileId' => 'mock.file.id', 'redirectUri' => 'none']);
}
public function testMissingKeyDuringInstantiationThrowsException()
{
$this->expectException('InvalidArgumentException');
$this->getProvider()->getLocalKey();
}
public function testAuthorizationUrl()
{
$provider = $this->getProvider();
$url = $provider->getAuthorizationUrl();
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
$this->assertArrayHasKey('scope', $query);
$this->assertArrayHasKey('response_type', $query);
$this->assertArrayHasKey('response_mode', $query);
$this->assertNotNull($provider->getState());
}
public function testScopes()
{
$provider = $this->getProvider();
$scopeSeparator = ' ';
$options = ['scope' => [uniqid(), uniqid()]];
$query = ['scope' => implode($scopeSeparator, $options['scope'])];
$url = $provider->getAuthorizationUrl($options);
$encodedScope = $this->buildQueryString($query);
$this->assertNotFalse(strpos($url, $encodedScope));
}
public function testGetAuthorizationUrl()
{
$provider = $this->getProvider();
$url = $provider->getAuthorizationUrl();
$uri = parse_url($url);
$this->assertEquals('/auth/authorize', $uri['path']);
}
public function testGetBaseAccessTokenUrl()
{
$provider = $this->getProvider();
$params = [];
$url = $provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/auth/token', $uri['path']);
}
public function testGetAccessToken()
{
$this->expectException('UnexpectedValueException');
$provider = new TestApple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFileId' => 'mock.file.id', 'keyFilePath' => __DIR__ . '/../../resources/p256-private-key.p8', 'redirectUri' => 'none']);
$provider = m::mock($provider);
$configuration = Configuration::forSymmetricSigner(new KeyDumpSigner(), Key\InMemory::plainText('private'));
$time = new \DateTimeImmutable();
$expiresAt = $time->modify('+1 Hour');
$token = $configuration->builder()->issuedBy('test-team-id')->permittedFor('https://appleid.apple.com')->issuedAt($time)->expiresAt($expiresAt)->relatedTo('test-client')->withHeader('alg', 'RS256')->withHeader('kid', 'test')->getToken($configuration->signer(), $configuration->signingKey());
$client = m::mock(ClientInterface::class);
$client->shouldReceive('request')->times(1)->andReturn(new Response(200, [], file_get_contents('https://appleid.apple.com/auth/keys')));
$client->shouldReceive('send')->times(1)->andReturn(new Response(200, [], json_encode(['access_token' => 'aad897dee58fe4f66bf220c181adaf82b.0.mrwxq.hmiE0djj1vJqoNisKmF-pA', 'token_type' => 'Bearer', 'expires_in' => 3600, 'refresh_token' => 'r4a6e8b9c50104b78bc86b0d2649353fa.0.mrwxq.54joUj40j0cpuMANRtRjfg', 'id_token' => $token->toString()])));
$provider->setHttpClient($client);
$provider->getAccessToken('authorization_code', ['code' => 'hello-world']);
}
public function testGetAccessTokenFailedBecauseAppleHasError()
{
$this->expectException('Exception');
$this->expectExceptionMessage('Got no data within "id_token"!');
$provider = new TestApple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFileId' => 'mock.file.id', 'keyFilePath' => __DIR__ . '/../../resources/p256-private-key.p8', 'redirectUri' => 'none']);
$provider = m::mock($provider);
$client = m::mock(ClientInterface::class);
$client->shouldReceive('request')->times(1)->andReturn(new Response(500, [], 'Internal Server Error'));
$client->shouldReceive('send')->times(1)->andReturn(new Response(200, [], json_encode(['access_token' => 'aad897dee58fe4f66bf220c181adaf82b.0.mrwxq.hmiE0djj1vJqoNisKmF-pA', 'token_type' => 'Bearer', 'expires_in' => 3600, 'refresh_token' => 'r4a6e8b9c50104b78bc86b0d2649353fa.0.mrwxq.54joUj40j0cpuMANRtRjfg', 'id_token' => 'abc'])));
$provider->setHttpClient($client);
$provider->getAccessToken('authorization_code', ['code' => 'hello-world']);
}
public function testRevokeAccessToken()
{
$provider = new TestApple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFileId' => 'mock.file.id', 'keyFilePath' => __DIR__ . '/../../resources/p256-private-key.p8', 'redirectUri' => 'none']);
$provider = m::mock($provider);
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')->times(1)->andReturn(new Response(200, [], json_encode([])));
$provider->setHttpClient($client);
$this->assertEmpty($provider->revokeAccessToken('hello-world', 'access_token'));
}
public function testRevokeAccessTokenFailedBecauseAppleHasError()
{
$this->expectException('Exception');
$this->expectExceptionMessage('invalid_request');
$provider = new TestApple(['clientId' => 'mock.example', 'teamId' => 'mock.team.id', 'keyFileId' => 'mock.file.id', 'keyFilePath' => __DIR__ . '/../../resources/p256-private-key.p8', 'redirectUri' => 'none']);
$provider = m::mock($provider);
$client = m::mock(ClientInterface::class);
$client->shouldReceive('send')->times(1)->andReturn(new Response(400, [], json_encode(['error' => 'invalid_request'])));
$provider->setHttpClient($client);
$provider->revokeAccessToken('hello-world');
}
public function testFetchingOwnerDetails()
{
$provider = $this->getProvider();
$class = new \ReflectionClass($provider);
$method = $class->getMethod('fetchResourceOwnerDetails');
$method->setAccessible(\true);
$arr = ['name' => 'John Doe'];
$_POST['user'] = json_encode($arr);
$data = $method->invokeArgs($provider, [new AccessToken(['access_token' => 'hello'])]);
$this->assertEquals($arr, $data);
}
/**
* @see https://github.com/patrickbussmann/oauth2-apple/issues/12
*/
public function testFetchingOwnerDetailsIssue12()
{
$provider = $this->getProvider();
$class = new \ReflectionClass($provider);
$method = $class->getMethod('fetchResourceOwnerDetails');
$method->setAccessible(\true);
$_POST['user'] = '';
$data = $method->invokeArgs($provider, [new AccessToken(['access_token' => 'hello'])]);
$this->assertEquals([], $data);
}
public function testNotImplementedGetResourceOwnerDetailsUrl()
{
$this->expectException('Exception');
$provider = $this->getProvider();
$provider->getResourceOwnerDetailsUrl(new AccessToken(['access_token' => 'hello']));
}
public function testCheckResponse()
{
$this->expectException('Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\Exception\AppleAccessDeniedException');
$this->expectExceptionMessage('invalid_client');
$provider = $this->getProvider();
$class = new \ReflectionClass($provider);
$method = $class->getMethod('checkResponse');
$method->setAccessible(\true);
$method->invokeArgs($provider, [new Response(400, []), ['error' => 'invalid_client', 'code' => 400]]);
}
public function testResourceToArrayHasAttributes()
{
$provider = $this->getProvider();
$class = new \ReflectionClass($provider);
$method = $class->getMethod('createResourceOwner');
$method->setAccessible(\true);
/** @var AppleResourceOwner $data */
$data = $method->invokeArgs($provider, [[
'email' => 'john@doe.com',
// <- Fake E-Mail from user input
'name' => ['firstName' => 'John', 'lastName' => 'Doe'],
], new AccessToken(['access_token' => 'hello', 'email' => 'john@doe.de', 'resource_owner_id' => '123.4.567'])]);
$expectedArray = ['email' => 'john@doe.de', 'sub' => '123.4.567', 'name' => ['firstName' => 'John', 'lastName' => 'Doe'], 'isPrivateEmail' => null];
$this->assertEquals($expectedArray, $data->toArray());
}
public function testCreationOfResourceOwnerWithName()
{
$provider = $this->getProvider();
$class = new \ReflectionClass($provider);
$method = $class->getMethod('createResourceOwner');
$method->setAccessible(\true);
/** @var AppleResourceOwner $data */
$data = $method->invokeArgs($provider, [[
'email' => 'john@doe.com',
// <- Fake E-Mail from user input
'name' => ['firstName' => 'John', 'lastName' => 'Doe'],
], new AccessToken(['access_token' => 'hello', 'email' => 'john@doe.de', 'resource_owner_id' => '123.4.567'])]);
$this->assertEquals('john@doe.de', $data->getEmail());
$this->assertEquals('Doe', $data->getLastName());
$this->assertEquals('John', $data->getFirstName());
$this->assertEquals('123.4.567', $data->getId());
$this->assertFalse($data->isPrivateEmail());
$this->assertArrayHasKey('name', $data->toArray());
}
public function testCreationOfResourceOwnerWithoutName()
{
$provider = $this->getProvider();
$class = new \ReflectionClass($provider);
$method = $class->getMethod('createResourceOwner');
$method->setAccessible(\true);
/** @var AppleResourceOwner $data */
$data = $method->invokeArgs($provider, [[], new AccessToken(['access_token' => 'hello', 'email' => 'john@doe.de', 'resource_owner_id' => '123.4.567'])]);
$this->assertEquals('john@doe.de', $data->getEmail());
$this->assertNull($data->getLastName());
$this->assertNull($data->getFirstName());
}
public function testGetConfiguration()
{
$provider = m::mock(Apple::class)->makePartial();
$provider->shouldReceive('getLocalKey')->andReturn(m::mock(Key::class));
$this->assertInstanceOf(Configuration::class, $provider->getConfiguration());
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\Provider;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Configuration;
use Pshowsso\Scope68f5e85e9608b\Lcobucci\JWT\Signer\Key\InMemory;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\Apple;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\KeyDumpSigner;
/**
* Class TestApple
* @package League\OAuth2\Client\Test\Provider
* @author Patrick Bußmann <patrick.bussmann@bussmann-it.de>
*/
class TestApple extends Apple
{
/**
* {@inheritDoc}
*/
public function getConfiguration()
{
return Configuration::forSymmetricSigner(new KeyDumpSigner(), InMemory::plainText('private'));
}
/**
* {@inheritDoc}
*/
public function getLocalKey()
{
return null;
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\Token;
use Pshowsso\Scope68f5e85e9608b\Firebase\JWT\Key;
use Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Token\AppleAccessToken;
use PHPUnit\Framework\TestCase;
use Mockery as m;
class AppleAccessTokenTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testCreatingAccessToken()
{
$externalJWTMock = m::mock('overload:Firebase\JWT\JWT');
$externalJWTMock->shouldReceive('decode')->with('something', 'examplekey')->once()->andReturn(['sub' => '123.abc.123', 'email_verified' => \true, 'email' => 'john@doe.com', 'is_private_email' => \true]);
$accessToken = new AppleAccessToken(['examplekey'], ['access_token' => 'access_token', 'token_type' => 'Bearer', 'expires_in' => 3600, 'refresh_token' => 'abc.0.def', 'id_token' => 'something']);
$this->assertEquals('something', $accessToken->getIdToken());
$this->assertEquals('123.abc.123', $accessToken->getResourceOwnerId());
$this->assertEquals('access_token', $accessToken->getToken());
$this->assertEquals('john@doe.com', $accessToken->getEmail());
$this->assertTrue($accessToken->isPrivateEmail());
$this->assertTrue(\true);
}
public function testCreateFailsBecauseNoIdTokenIsSet()
{
$this->expectException('\InvalidArgumentException');
$this->expectExceptionMessage('Required option not passed: "id_token"');
new AppleAccessToken(['examplekey'], ['access_token' => 'access_token', 'token_type' => 'Bearer', 'expires_in' => 3600, 'refresh_token' => 'abc.0.def']);
}
public function testCreatingRefreshToken()
{
$refreshToken = new AppleAccessToken([], ['access_token' => 'access_token', 'token_type' => 'Bearer', 'expires_in' => 3600]);
$this->assertEquals('access_token', $refreshToken->getToken());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testCreatingAccessTokenFailsBecauseNoDecodingIsPossible()
{
$this->expectException('\Exception');
$this->expectExceptionMessage('Got no data within "id_token"!');
$externalJWTMock = m::mock('overload:Firebase\JWT\JWT');
$externalJWTMock->shouldReceive('decode')->with('something', 'examplekey')->once()->andReturnNull();
new AppleAccessToken(['examplekey'], ['access_token' => 'access_token', 'token_type' => 'Bearer', 'expires_in' => 3600, 'refresh_token' => 'abc.0.def', 'id_token' => 'something']);
}
}