added socialite and some hacks via vendor sideload to add social login
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
/vendor
|
||||
composer.phar
|
||||
composer.lock
|
||||
.DS_Store
|
||||
@@ -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,30 @@
|
||||
{
|
||||
"name": "socialnorm/github",
|
||||
"description": "GitHub provider for SocialNorm",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Adam Wathan",
|
||||
"email": "adam.wathan@gmail.com"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"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\\GitHub\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"files": [
|
||||
"tests/TestCase.php"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,3 @@
|
||||
## SocialNorm GitHub Provider
|
||||
|
||||
@todo: Add docs :)
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php namespace SocialNorm\GitHub;
|
||||
|
||||
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
|
||||
use SocialNorm\Providers\OAuth2Provider;
|
||||
|
||||
class GitHubProvider extends OAuth2Provider
|
||||
{
|
||||
protected $authorizeUrl = "https://github.com/login/oauth/authorize";
|
||||
protected $accessTokenUrl = "https://github.com/login/oauth/access_token";
|
||||
protected $userDataUrl = "https://api.github.com/user";
|
||||
protected $scope = [
|
||||
'user:email',
|
||||
];
|
||||
|
||||
protected $headers = [
|
||||
'authorize' => [],
|
||||
'access_token' => [
|
||||
'Accept' => 'application/json'
|
||||
],
|
||||
'user_details' => [
|
||||
'Accept' => 'application/vnd.github.v3'
|
||||
],
|
||||
];
|
||||
|
||||
protected function getAuthorizeUrl()
|
||||
{
|
||||
return $this->authorizeUrl;
|
||||
}
|
||||
|
||||
protected function getAccessTokenBaseUrl()
|
||||
{
|
||||
return $this->accessTokenUrl;
|
||||
}
|
||||
|
||||
protected function getUserDataUrl()
|
||||
{
|
||||
return $this->userDataUrl;
|
||||
}
|
||||
|
||||
protected function parseTokenResponse($response)
|
||||
{
|
||||
return $this->parseJsonTokenResponse($response);
|
||||
}
|
||||
|
||||
protected function requestUserData()
|
||||
{
|
||||
$userData = parent::requestUserData();
|
||||
$userData['email'] = $this->requestEmail();
|
||||
return $userData;
|
||||
}
|
||||
|
||||
protected function requestEmail()
|
||||
{
|
||||
$url = $this->getEmailUrl();
|
||||
$emails = $this->getJson($url, $this->headers['user_details']);
|
||||
return $this->getPrimaryEmail($emails);
|
||||
}
|
||||
|
||||
protected function getEmailUrl()
|
||||
{
|
||||
$url = $this->getUserDataUrl() .'/emails';
|
||||
$url .= "?access_token=".$this->accessToken;
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function getJson($url, $headers)
|
||||
{
|
||||
$response = $this->httpClient->get($url, ['headers' => $headers]);
|
||||
return json_decode($response->getBody(), true);
|
||||
}
|
||||
|
||||
protected function getPrimaryEmail($emails)
|
||||
{
|
||||
foreach ($emails as $email) {
|
||||
if ($email['primary']) {
|
||||
return $email['email'];
|
||||
}
|
||||
}
|
||||
return $emails[0]['email'];
|
||||
}
|
||||
|
||||
protected function parseUserDataResponse($response)
|
||||
{
|
||||
$data = json_decode($response, true);
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function userId()
|
||||
{
|
||||
return $this->getProviderUserData('id');
|
||||
}
|
||||
|
||||
protected function nickname()
|
||||
{
|
||||
return $this->getProviderUserData('login');
|
||||
}
|
||||
|
||||
protected function fullName()
|
||||
{
|
||||
return $this->getProviderUserData('name');
|
||||
}
|
||||
|
||||
protected function avatar()
|
||||
{
|
||||
return $this->getProviderUserData('avatar_url');
|
||||
}
|
||||
|
||||
protected function email()
|
||||
{
|
||||
return $this->getProviderUserData('email');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
use Mockery as M;
|
||||
use SocialNorm\GitHub\GitHubProvider;
|
||||
use SocialNorm\Request;
|
||||
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\Client as HttpClient;
|
||||
|
||||
class GitHubProviderTest 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/github_accesstoken.php',
|
||||
__DIR__ . '/_fixtures/github_user.php',
|
||||
__DIR__ . '/_fixtures/github_email.php',
|
||||
]);
|
||||
|
||||
$provider = new GitHubProvider([
|
||||
'client_id' => 'abcdefgh',
|
||||
'client_secret' => '12345678',
|
||||
'redirect_uri' => 'http://example.com/login',
|
||||
], $client, new Request(['code' => 'abc123']));
|
||||
|
||||
$user = $provider->getUser();
|
||||
|
||||
$this->assertEquals('4323180', $user->id);
|
||||
$this->assertEquals('adamwathan', $user->nickname);
|
||||
$this->assertEquals('Adam Wathan', $user->full_name);
|
||||
$this->assertEquals('adam@example.com', $user->email);
|
||||
$this->assertEquals('https://avatars.githubusercontent.com/u/4323180?v=3', $user->avatar);
|
||||
$this->assertEquals('abcdefgh12345678', $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/github_accesstoken.php',
|
||||
__DIR__ . '/_fixtures/github_user.php',
|
||||
__DIR__ . '/_fixtures/github_email.php',
|
||||
]);
|
||||
|
||||
$provider = new GitHubProvider([
|
||||
'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,27 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'status' => 200,
|
||||
'headers' => [
|
||||
'Server' => 'GitHub.com',
|
||||
'Date' => 'Sat, 21 Feb 2015 18:44:34 GMT',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'Transfer-Encoding' => 'chunked',
|
||||
'Status' => '200 OK',
|
||||
'Content-Security-Policy' => 'default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src \'self\' \'unsafe-inline\' \'unsafe-eval\' assets-cdn.github.com; img-src \'self\' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src \'none\'; frame-src \'self\' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src \'self\' ghconduit.com:25035 live.github.com wss://live.github.com uploads.github.com www.google-analytics.com s3.amazonaws.com',
|
||||
'Cache-Control' => 'no-cache',
|
||||
'Vary' => 'X-PJAX, Accept-Encoding',
|
||||
'X-UA-Compatible' => 'IE=Edge,chrome=1',
|
||||
'Set-Cookie' => 'logged_in=no; domain=.github.com; path=/; expires=Wed, 21-Feb-2035 18:44:34 GMT; secure; HttpOnly, _gh_sess=eyJzZXNzaW9uX2lkIjoiNDZkZDdiMTMzNDIxMjQ5OTNjZjliNmUyMzg4OTM5MWUiLCJsYXN0X3dyaXRlIjoxNDI0NTQ0Mjc0NDgzfQ%3D%3D--4ca192ff94067bcf8922b053b0758d1f580f85a6; path=/; secure; HttpOnly',
|
||||
'X-Request-Id' => 'f8898bcf19b20706de5712177bdf9eeb',
|
||||
'X-Runtime' => '0.010527',
|
||||
'X-Rack-Cache' => 'invalidate, pass',
|
||||
'X-GitHub-Request-Id' => 'AE71B20F:0FF4:159043E3:54E8D212',
|
||||
'Strict-Transport-Security' => 'max-age=31536000; includeSubdomains; preload',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'X-XSS-Protection' => '1; mode=block',
|
||||
'X-Frame-Options' => 'deny',
|
||||
'X-Served-By' => 'a568c03544f42dddf712bab3bfd562fd'
|
||||
],
|
||||
'body' => '{"access_token":"abcdefgh12345678","token_type":"bearer","scope":"user:email"}'
|
||||
];
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'status' => 200,
|
||||
'headers' => [
|
||||
'Server' => 'GitHub.com',
|
||||
'Date' => 'Thu, 19 Mar 2015 03:42:05 GMT',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'Content-Length' => '62',
|
||||
'Status' => '200 OK',
|
||||
'X-RateLimit-Limit' => '5000',
|
||||
'X-RateLimit-Remaining' => '4948',
|
||||
'X-RateLimit-Reset' => '1426740125',
|
||||
'Cache-Control' => 'private, max-age=60, s-maxage=60',
|
||||
'ETag' => '"06ddc2c2d17761bc98b0e6419aed512c"',
|
||||
'X-OAuth-Scopes' => 'user:email',
|
||||
'X-Accepted-OAuth-Scopes' => 'user, user:email',
|
||||
'X-OAuth-Client-Id' => 'b80ba34640eb08f2b3e5',
|
||||
'Vary' => 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding',
|
||||
'X-GitHub-Media-Type' => 'github.v3',
|
||||
'X-XSS-Protection' => '1; mode=block',
|
||||
'X-Frame-Options' => 'deny',
|
||||
'Content-Security-Policy' => 'default-src \'none\'',
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Expose-Headers' => 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval',
|
||||
'Access-Control-Allow-Origin' => '*',
|
||||
'X-GitHub-Request-Id' => 'AE71B20F:4EF8:83D06C:550A458D',
|
||||
'Strict-Transport-Security' => 'max-age=31536000; includeSubdomains; preload',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'X-Served-By' => 'b0ef53392caa42315c6206737946d931'
|
||||
],
|
||||
'body' => '[{"email": "adam@example.com","primary": true,"verified": true}]'
|
||||
];
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'status' => 200,
|
||||
'headers' => [
|
||||
'Server' => 'GitHub.com',
|
||||
'Date' => 'Sat, 21 Feb 2015 18:43:17 GMT',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'Content-Length' => '1278',
|
||||
'Status' => '200 OK',
|
||||
'X-RateLimit-Limit' => '5000',
|
||||
'X-RateLimit-Remaining' => '4961',
|
||||
'X-RateLimit-Reset' => '1424546049',
|
||||
'Cache-Control' => 'private, max-age=60, s-maxage=60',
|
||||
'Last-Modified' => 'Sat, 21 Feb 2015 17:06:00 GMT',
|
||||
'ETag' => '"7a29c845b431fa302144d2d2da66e7e3"',
|
||||
'X-OAuth-Scopes' => 'user:email',
|
||||
'X-Accepted-OAuth-Scopes' => '',
|
||||
'X-OAuth-Client-Id' => 'b80ba34640eb08f2b3e5',
|
||||
'Vary' => 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding',
|
||||
'X-GitHub-Media-Type' => 'github.v3',
|
||||
'X-XSS-Protection' => '1; mode=block',
|
||||
'X-Frame-Options' => 'deny',
|
||||
'Content-Security-Policy' => 'default-src \'none\'',
|
||||
'Access-Control-Allow-Credentials' => 'true',
|
||||
'Access-Control-Expose-Headers' => 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval',
|
||||
'Access-Control-Allow-Origin' => '*',
|
||||
'X-GitHub-Request-Id' => 'AE71B20F:202B:3ABE9811:54E8D1C5',
|
||||
'Strict-Transport-Security' => 'max-age=31536000; includeSubdomains; preload',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'X-Served-By' => '065b43cd9674091fec48a221b420fbb3'
|
||||
],
|
||||
'body' => '{"login":"adamwathan","id":4323180,"avatar_url":"https://avatars.githubusercontent.com/u/4323180?v=3","gravatar_id":"","url":"https://api.github.com/users/adamwathan","html_url":"https://github.com/adamwathan","followers_url":"https://api.github.com/users/adamwathan/followers","following_url":"https://api.github.com/users/adamwathan/following{/other_user}","gists_url":"https://api.github.com/users/adamwathan/gists{/gist_id}","starred_url":"https://api.github.com/users/adamwathan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamwathan/subscriptions","organizations_url":"https://api.github.com/users/adamwathan/orgs","repos_url":"https://api.github.com/users/adamwathan/repos","events_url":"https://api.github.com/users/adamwathan/events{/privacy}","received_events_url":"https://api.github.com/users/adamwathan/received_events","type":"User","site_admin":false,"name":"Adam Wathan","company":"Tighten Co","blog":"","location":"Ontario,Canada","email":"","hireable":false,"bio":null,"public_repos":38,"public_gists":12,"followers":54,"following":10,"created_at":"2013-05-02T15:35:48Z","updated_at":"2015-02-21T17:06:00Z"}'
|
||||
];
|
||||
Reference in New Issue
Block a user