Skip to content

Commit

Permalink
Merge branch '2.x' of github.com:overtrue/socialite into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 23, 2020
2 parents c2a7124 + ae4c8d1 commit a4a9103
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "~5.0|~6.0",
"guzzlehttp/guzzle": "^5.0|^6.0|^7.0",
"symfony/http-foundation": "^2.7|^3.0|^4.0|^5.0",
"ext-json": "*"
},
Expand Down
14 changes: 13 additions & 1 deletion src/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,18 @@ abstract class AbstractProvider implements ProviderInterface
*/
public function __construct(Request $request, $config)
{
// 兼容处理
if (!\is_array($config)) {
$config = [
'client_id' => \func_get_arg(1),
'client_secret' => \func_get_arg(2),
'redirect' => \func_get_arg(3) ?: null,
];
}
$this->config = new Config($config);
$this->request = $request;
$this->clientId = $config['client_id'];
$this->clientSecret = $config['client_secret'];
$this->redirectUrl = isset($config['redirect']) ? $config['redirect'] : null;
}

Expand Down Expand Up @@ -266,7 +276,9 @@ public function getAccessToken($code)
return $this->accessToken;
}

$postKey = (1 === version_compare(ClientInterface::VERSION, '6')) ? 'form_params' : 'body';
$guzzleVersion = \defined(ClientInterface::class.'::VERSION') ? \constant(ClientInterface::class.'::VERSION') : 7;

$postKey = (1 === version_compare($guzzleVersion, '6')) ? 'form_params' : 'body';

$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
Expand Down
95 changes: 73 additions & 22 deletions tests/OAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,35 @@ public function tearDown()
m::close();
}

public function testAbstractProviderBackwardCompatible()
{
$request = Request::create('foo');
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('put')->once();
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');

$this->assertSame('client_id', $provider->getConfig()['client_id']);
$this->assertSame('client_secret', $provider->getConfig()['client_secret']);
$this->assertSame('redirect', $provider->getConfig()['redirect']);

$response = $provider->redirect();

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame('http://auth.url', $response->getTargetUrl());
}

public function testRedirectGeneratesTheProperSymfonyRedirectResponse()
{
$request = Request::create('foo');
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('put')->once();
$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]);
]
);
$response = $provider->redirect();

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
Expand All @@ -44,17 +63,21 @@ public function testRedirectUrl()
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));

$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
]);
]
);
$this->assertNull($provider->getRedirectUrl());

$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]);
]
);
$this->assertSame('redirect_uri', $provider->getRedirectUrl());
$provider->setRedirectUrl('overtrue.me');
$this->assertSame('overtrue.me', $provider->getRedirectUrl());
Expand All @@ -69,15 +92,26 @@ public function testUserReturnsAUserInstanceForTheAuthenticatedRequest()
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));

$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]);
]
);
$provider->http = m::mock('StdClass');
$provider->http->shouldReceive('post')->once()->with('http://token.url', [
'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'],
])->andReturn($response = m::mock('StdClass'));
$provider->http->shouldReceive('post')->once()->with(
'http://token.url',
[
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
],
]
)->andReturn($response = m::mock('StdClass'));
$response->shouldReceive('getBody')->once()->andReturn('{"access_token":"access_token"}');
$user = $provider->user();

Expand All @@ -93,11 +127,13 @@ public function testExceptionIsThrownIfStateIsInvalid()
$request = Request::create('foo', 'GET', ['state' => str_repeat('B', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]);
]
);
$user = $provider->user();
}

Expand All @@ -110,15 +146,26 @@ public function testExceptionisThrownIfAuthorizeFailed()
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]);
]
);
$provider->http = m::mock('StdClass');
$provider->http->shouldReceive('post')->once()->with('http://token.url', [
'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'],
])->andReturn($response = m::mock('StdClass'));
$provider->http->shouldReceive('post')->once()->with(
'http://token.url',
[
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
],
]
)->andReturn($response = m::mock('StdClass'));
$response->shouldReceive('getBody')->once()->andReturn('{"error":"scope is invalid"}');
$user = $provider->user();
}
Expand All @@ -131,22 +178,26 @@ public function testExceptionIsThrownIfStateIsNotSet()
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state');
$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]);
]
);
$user = $provider->user();
}

public function testDriverName()
{
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$provider = new OAuthTwoTestProviderStub($request, [
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]);
]
);

$this->assertSame('OAuthTwoTest', $provider->getName());
}
Expand Down

0 comments on commit a4a9103

Please sign in to comment.