fixing up the authenticator to allow identiy chaining

This commit is contained in:
2018-07-10 21:55:47 +02:00
parent 598f2f8024
commit d74b76da3e
41 changed files with 513 additions and 346 deletions
@@ -0,0 +1,75 @@
<?php
use Mockery as M;
use SocialNorm\Google\GoogleProvider;
use SocialNorm\Request;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Client as HttpClient;
class GoogleProviderTest extends TestCase
{
private function getStubbedHttpClient($fixtures = [])
{
$mock = new MockHandler($this->createResponses($fixtures));
$handler = HandlerStack::create($mock);
return new HttpClient(['handler' => $handler]);
}
private function createResponses($fixtures)
{
$responses = [];
foreach ($fixtures as $fixture) {
$response = require $fixture;
$responses[] = new Response($response['status'], $response['headers'], $response['body']);
}
return $responses;
}
/** @test */
public function it_can_retrieve_a_normalized_user()
{
$client = $this->getStubbedHttpClient([
__DIR__ . '/_fixtures/google_accesstoken.php',
__DIR__ . '/_fixtures/google_user.php',
]);
$provider = new GoogleProvider([
'client_id' => 'abcdefgh',
'client_secret' => '12345678',
'redirect_uri' => 'http://example.com/login',
], $client, new Request(['code' => 'abc123']));
$user = $provider->getUser();
$this->assertEquals('103904294571447333816', $user->id);
$this->assertEquals('adam.wathan@example.com', $user->nickname);
$this->assertEquals('Adam Wathan', $user->full_name);
$this->assertEquals('adam.wathan@example.com', $user->email);
$this->assertEquals('https://lh3.googleusercontent.com/-w0_RpDnsIE4/AAAAAAAAAAI/AAAAAAAAAKM/NEiV3jig1HA/photo.jpg', $user->avatar);
$this->assertEquals('ya29.8xFOTYpQK48RgPH8KjQpSu9SrcANcOQx9JtRnEu52dNsXqai8VD4iY3nFzUBURWnAPeTPtPeIBNjIF', $user->access_token);
}
/**
* @test
* @expectedException SocialNorm\Exceptions\ApplicationRejectedException
*/
public function it_fails_to_retrieve_a_user_when_the_authorization_code_is_omitted()
{
$client = $this->getStubbedHttpClient([
__DIR__ . '/_fixtures/google_accesstoken.php',
__DIR__ . '/_fixtures/google_user.php',
]);
$provider = new GoogleProvider([
'client_id' => 'abcdefgh',
'client_secret' => '12345678',
'redirect_uri' => 'http://example.com/login',
], $client, new Request([]));
$user = $provider->getUser();
}
}
@@ -0,0 +1,12 @@
<?php
use Mockery as M;
class TestCase extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
M::close();
parent::tearDown();
}
}
@@ -0,0 +1,24 @@
<?php
return [
'status' => 200,
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma' => 'no-cache',
'Expires' => 'Fri, 01 Jan 1990 00:00:00 GMT',
'Date' => 'Thu, 19 Mar 2015 13:14:56 GMT',
'Content-Disposition' => 'attachment; filename="json.txt"; filename*=UTF-8\'\'json.txt',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'SAMEORIGIN',
'X-XSS-Protection' => '1; mode=block',
'Server' => 'GSE',
'Alternate-Protocol' => '443:quic,p=0.5',
'Accept-Ranges' => 'none',
'Vary' => 'Accept-Encoding',
'Transfer-Encoding' => 'chunked'
],
'body' => '{"access_token" :"ya29.8xFOTYpQK48RgPH8KjQpSu9SrcANcOQx9JtRnEu52dNsXqai8VD4iY3nFzUBURWnAPeTPtPeIBNjIF","token_type" :"Bearer","expires_in" :3600,"id_token" :"6XFIidpthpu8iFNhyo5xjcGa9bUdiTL1zKnZz2dN-35IFzlCJ_IcmHYcA1i03iA5YVMOM1nLGiAbRaLTaMoWM5X_j0MyFBknwGMs3MM2WmdiIhdC7uNiXY2US8mWvCiVNwMnchpkJJUGIMbTNNPQd5_08XQvNi3TINiYOwZLxOIJ6X3pi9hNVY0dNNtUYwYdiNgwIW3ClRNNY1jiZxciXSckj18jb2jbSMMYujDKbw9ipMOIUxlnm5zIzEjNLV1BcMFMbjQWBnhjFUW4li-QTjcSnfj5iMdUIkjlGvkWcoLQ3pNNOwMhwjZFhdv0G3DTyT2nyOIR3eejckDXGWGw4l2Ni3izRwMYctNF1I_VstkDwn1idZoMOdR3b-zEO5C9ItMzl0TyNJjeb73VFrQIwXpHcTTQMgym2o3mijwJTn2ypu9NP0jjM1lcjd0Euh0OLJYxIYTIfbBC5WuCDcDMZxCbcz7oYLMl2yYDmI0akcuiVZyDO2I1aS2DAIUip.oTuI0HnAbtiA5y.Tnywh3VU0JTZ2Iu9OhJnYkvYmhzOm2AYbu5P9Q71UQObK2tLNcYQmBDmZGsQNIMwh1tQtl0sDTXj9WTVETuYAx4WK9hoOzcOVEY8iNHcl"}'
];
@@ -0,0 +1,23 @@
<?php
return [
'status' => 200,
'headers' => [
'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma' => 'no-cache',
'Expires' => 'Fri, 01 Jan 1990 00:00:00 GMT',
'Date' => 'Thu, 19 Mar 2015 13:15:29 GMT',
'Vary' => 'X-Origin, Origin,Accept-Encoding',
'Content-Type' => 'application/json; charset=UTF-8',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'SAMEORIGIN',
'X-XSS-Protection' => '1; mode=block',
'Server' => 'GSE',
'Alternate-Protocol' => '443:quic,p=0.5',
'Accept-Ranges' => 'none',
'Transfer-Encoding' => 'chunked'
],
'body' => '{"id":"103904294571447333816","email":"adam.wathan@example.com","verified_email":true,"name":"Adam Wathan","given_name":"Adam","family_name":"Wathan","link":"https://plus.google.com/103904294571447333816","picture":"https://lh3.googleusercontent.com/-w0_RpDnsIE4/AAAAAAAAAAI/AAAAAAAAAKM/NEiV3jig1HA/photo.jpg","gender":"male","locale":"en"}'
];