added socialite and some hacks via vendor sideload to add social login

This commit is contained in:
2018-07-08 23:59:32 +02:00
parent d1fa087121
commit 6e2040249b
123 changed files with 4356 additions and 6 deletions
+4
View File
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
+11
View File
@@ -0,0 +1,11 @@
language: php
php:
- 5.5
- 5.6
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev
script: phpunit
@@ -0,0 +1,36 @@
{
"name": "socialnorm/facebook",
"description": "Facebook provider for SocialNorm",
"authors": [
{
"name": "Adam Wathan",
"email": "adam.wathan@gmail.com"
}
],
"license": "MIT",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/adamwathan/socialnorm"
}
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0",
"socialnorm/socialnorm": "^0.2"
},
"require-dev": {
"mockery/mockery": "~0.8",
"phpunit/phpunit": "^4.8"
},
"autoload": {
"psr-4": {
"SocialNorm\\Facebook\\": "src/"
}
},
"autoload-dev": {
"files": [
"tests/TestCase.php"
]
}
}
+18
View File
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
+3
View File
@@ -0,0 +1,3 @@
## SocialNorm Facebook Provider
@todo: Add docs :)
@@ -0,0 +1,98 @@
<?php namespace SocialNorm\Facebook;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
use SocialNorm\Providers\OAuth2Provider;
class FacebookProvider extends OAuth2Provider
{
protected $authorizeUrl = "https://www.facebook.com/dialog/oauth";
protected $accessTokenUrl = "https://graph.facebook.com/v2.4/oauth/access_token";
protected $userDataUrl = "https://graph.facebook.com/v2.4/me";
protected $scope = [
'email',
];
protected $fields = [
'email',
'name',
'first_name',
'last_name',
'age_range',
'timezone',
];
protected function getAuthorizeUrl()
{
return $this->authorizeUrl;
}
protected function getAccessTokenBaseUrl()
{
return $this->accessTokenUrl;
}
protected function getUserDataUrl()
{
return $this->userDataUrl;
}
protected function buildUserDataUrl()
{
$url = $this->getUserDataUrl();
$url .= "?access_token={$this->accessToken}";
$url .= "&fields=" . implode(',', $this->fields);
return $url;
}
protected function requestAccessToken()
{
$url = $this->getAccessTokenBaseUrl();
try {
$response = $this->httpClient->get($url, [
'query' => [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri(),
'code' => $this->request->authorizationCode(),
],
]);
} catch (BadResponseException $e) {
throw new InvalidAuthorizationCodeException((string) $e->getResponse());
}
return $this->parseTokenResponse((string) $response->getBody());
}
protected function parseTokenResponse($response)
{
return $this->parseJsonTokenResponse($response);
}
protected function parseUserDataResponse($response)
{
return json_decode($response, true);
}
protected function userId()
{
return $this->getProviderUserData('id');
}
protected function nickname()
{
return $this->getProviderUserData('name');
}
protected function fullName()
{
return $this->getProviderUserData('name');
}
protected function avatar()
{
return 'https://graph.facebook.com/v2.4/'.$this->userId().'/picture';
}
protected function email()
{
return $this->getProviderUserData('email');
}
}
@@ -0,0 +1,78 @@
<?php
use Mockery as M;
use SocialNorm\Facebook\FacebookProvider;
use SocialNorm\Request;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Client as HttpClient;
class FacebookProviderTest 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/facebook_accesstoken.php',
__DIR__ . '/_fixtures/facebook_user.php',
]);
$provider = new FacebookProvider([
'client_id' => 'abcdefgh',
'client_secret' => '12345678',
'redirect_uri' => 'http://example.com/login',
], $client, new Request(['code' => 'abc123']));
$user = $provider->getUser();
$this->assertEquals('187903669', $user->id);
$this->assertEquals('John Doe', $user->nickname);
$this->assertEquals('John Doe', $user->full_name);
$this->assertEquals('john@example.com', $user->email);
$this->assertEquals('https://graph.facebook.com/v2.4/187903669/picture', $user->avatar);
$this->assertEquals(
'RUXRIAxWwxVYKk3b1vrTACPUiAGImrszVsBXb2FQZZZXbd6JNzkZRAgZLCdAiCfKHrPanMTS8BAHLPqugidBcCNkUmz3y72XMZRZWw4SEGdczB2HygsA7oQOufDIbgZBtyA1KaznugApacfId5HIdZtIEh47ZLEa0BrJrBICZBf4uCWCGD5OBM40RpvTVaAux2vCv5wU9ZZzm91WAVtSC5ufoZmr3Ty',
$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/facebook_accesstoken.php',
__DIR__ . '/_fixtures/facebook_user.php',
]);
$provider = new FacebookProvider([
'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,19 @@
<?php
return [
'status' => 200,
'headers' => [
'Content-Type' => 'application/json; charset=UTF-8',
'Access-Control-Allow-Origin' => '*',
'X-FB-Rev' => '1669049',
'Pragma' => 'no-cache',
'Cache-Control' => 'private, no-cache, no-store, must-revalidate',
'Facebook-API-Version' => 'v2.3',
'Expires' => 'Sat, 01 Jan 2000 00:00:00 GMT',
'X-FB-Debug' => 'aWKI40XqPBE1YkeMX7Awln6RzgWl+JQpbYY7MWkiP2cRZ0TNadSG8rTnlHQovxjP+5fTYg1ZfkOVKsiMdJB9Jg==',
'Date' => 'Wed, 01 Apr 2015 12:47:40 GMT',
'Connection' => 'keep-alive',
'Content-Length' => '285'
],
'body' => '{"access_token":"RUXRIAxWwxVYKk3b1vrTACPUiAGImrszVsBXb2FQZZZXbd6JNzkZRAgZLCdAiCfKHrPanMTS8BAHLPqugidBcCNkUmz3y72XMZRZWw4SEGdczB2HygsA7oQOufDIbgZBtyA1KaznugApacfId5HIdZtIEh47ZLEa0BrJrBICZBf4uCWCGD5OBM40RpvTVaAux2vCv5wU9ZZzm91WAVtSC5ufoZmr3Ty","token_type":"bearer","expires_in":5183288}'
];
@@ -0,0 +1,21 @@
<?php
return [
'status' => 200,
'headers' => [
'Content-Type' => 'text/javascript; charset=UTF-8',
'Last-Modified' => '2014-12-21T00:04:42+0000',
'Access-Control-Allow-Origin' => '*',
'X-FB-Rev' => '1669049',
'ETag' => '"e732c03dd9c5d6f0a2aa6bb7b2e78e4e36bf5a4e"',
'Pragma' => 'no-cache',
'Cache-Control' => 'private, no-cache, no-store, must-revalidate',
'Facebook-API-Version' => 'v2.3',
'Expires' => 'Sat, 01 Jan 2000 00:00:00 GMT',
'X-FB-Debug' => 'WkWb9eeV7Aw1YJagx9Ffj0XzEQuDZhaUlOsNLGbf+es8H5Tq2aW4iBn6G0tUh1WRTG4Rbd+nIaTTR1Mbhz2b8Q==',
'Date' => 'Wed, 01 Apr 2015 12:49:00 GMT',
'Connection' => 'keep-alive',
'Content-Length' => '295'
],
'body' => '{"id":"187903669","birthday":"03\/15\/1987","email":"john\u0040example.com","first_name":"John","gender":"male","last_name":"Doe","link":"http:\/\/www.facebook.com\/187903669","locale":"en_US","name":"John Doe","timezone":-4,"updated_time":"2014-12-21T00:04:42+0000","verified":true}'
];