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

pull/26/head
Ondřej Hruška 6 years ago
parent d1fa087121
commit 6e2040249b
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 10
      app/Models/DataTable.php
  2. 10
      app/Models/DiscussionFollow.php
  3. 10
      app/Models/TableComment.php
  4. 10
      app/Models/TableFavourite.php
  5. 10
      app/Models/TableRevision.php
  6. 10
      app/Models/UserFollow.php
  7. 76
      app/SocialLogin/Authenticator.php
  8. 28
      app/SocialLogin/EloquentIdentityStore.php
  9. 11
      app/SocialLogin/Facades/OAuth.php
  10. 9
      app/SocialLogin/IdentityStore.php
  11. 26
      app/SocialLogin/OAuthIdentity.php
  12. 37
      app/SocialLogin/OAuthManager.php
  13. 23
      app/SocialLogin/Session.php
  14. 27
      app/SocialLogin/UserStore.php
  15. 9
      composer.json
  16. 358
      composer.lock
  17. 5
      config/app.php
  18. 20
      config/services.php
  19. 9
      database/factories/DataTableFactory.php
  20. 4
      database/migrations/2014_10_12_000000_create_users_table.php
  21. 66
      database/migrations/2018_07_08_193638_create_data_tables_table.php
  22. 41
      database/migrations/2018_07_08_202724_create_table_comments_table.php
  23. 41
      database/migrations/2018_07_08_203424_create_user_follows_table.php
  24. 41
      database/migrations/2018_07_08_203801_create_discussion_follows_table.php
  25. 42
      database/migrations/2018_07_08_204223_create_table_favourites_table.php
  26. 39
      database/migrations/2018_07_08_204449_create_oauth_identities_table.php
  27. 39
      database/migrations/2018_07_08_214204_create_sessions_table.php
  28. 9
      resources/views/auth/login.blade.php
  29. 61
      routes/web.php
  30. 3
      sideload/adamwathan/eloquent-oauth-l5/.gitignore
  31. 11
      sideload/adamwathan/eloquent-oauth-l5/.travis.yml
  32. 24
      sideload/adamwathan/eloquent-oauth-l5/composer.json
  33. 0
      sideload/adamwathan/eloquent-oauth-l5/config/.gitkeep
  34. 46
      sideload/adamwathan/eloquent-oauth-l5/config/eloquent-oauth.php
  35. 26
      sideload/adamwathan/eloquent-oauth-l5/migrations/create_oauth_identities_table.stub
  36. 18
      sideload/adamwathan/eloquent-oauth-l5/phpunit.xml
  37. 263
      sideload/adamwathan/eloquent-oauth-l5/readme.md
  38. 127
      sideload/adamwathan/eloquent-oauth-l5/src/EloquentOAuthServiceProvider.php
  39. 5
      sideload/adamwathan/eloquent-oauth-l5/src/Installation/FileExistsException.php
  40. 70
      sideload/adamwathan/eloquent-oauth-l5/src/Installation/InstallCommand.php
  41. 0
      sideload/adamwathan/eloquent-oauth-l5/tests/.gitkeep
  42. 10
      sideload/adamwathan/eloquent-oauth-l5/todo.md
  43. 3
      sideload/adamwathan/eloquent-oauth/.gitignore
  44. 11
      sideload/adamwathan/eloquent-oauth/.travis.yml
  45. 39
      sideload/adamwathan/eloquent-oauth/composer.json
  46. 18
      sideload/adamwathan/eloquent-oauth/phpunit.xml
  47. 159
      sideload/adamwathan/eloquent-oauth/readme.md
  48. 83
      sideload/adamwathan/eloquent-oauth/src/Authenticator.php
  49. 28
      sideload/adamwathan/eloquent-oauth/src/EloquentIdentityStore.php
  50. 11
      sideload/adamwathan/eloquent-oauth/src/Facades/OAuth.php
  51. 9
      sideload/adamwathan/eloquent-oauth/src/IdentityStore.php
  52. 26
      sideload/adamwathan/eloquent-oauth/src/OAuthIdentity.php
  53. 38
      sideload/adamwathan/eloquent-oauth/src/OAuthManager.php
  54. 23
      sideload/adamwathan/eloquent-oauth/src/Session.php
  55. 27
      sideload/adamwathan/eloquent-oauth/src/UserStore.php
  56. 0
      sideload/adamwathan/eloquent-oauth/tests/.gitkeep
  57. 122
      sideload/adamwathan/eloquent-oauth/tests/AuthenticatorTest.php
  58. 38
      sideload/adamwathan/eloquent-oauth/tests/FunctionalTestCase.php
  59. 155
      sideload/adamwathan/eloquent-oauth/tests/IdentityStoreTest.php
  60. 54
      sideload/adamwathan/eloquent-oauth/tests/OAuthManagerTest.php
  61. 10
      sideload/adamwathan/eloquent-oauth/todo.md
  62. 4
      sideload/socialnorm/facebook/.gitignore
  63. 11
      sideload/socialnorm/facebook/.travis.yml
  64. 36
      sideload/socialnorm/facebook/composer.json
  65. 18
      sideload/socialnorm/facebook/phpunit.xml
  66. 3
      sideload/socialnorm/facebook/readme.md
  67. 98
      sideload/socialnorm/facebook/src/FacebookProvider.php
  68. 0
      sideload/socialnorm/facebook/tests/.gitkeep
  69. 78
      sideload/socialnorm/facebook/tests/FacebookProviderTest.php
  70. 12
      sideload/socialnorm/facebook/tests/TestCase.php
  71. 19
      sideload/socialnorm/facebook/tests/_fixtures/facebook_accesstoken.php
  72. 21
      sideload/socialnorm/facebook/tests/_fixtures/facebook_user.php
  73. 4
      sideload/socialnorm/github/.gitignore
  74. 11
      sideload/socialnorm/github/.travis.yml
  75. 30
      sideload/socialnorm/github/composer.json
  76. 18
      sideload/socialnorm/github/phpunit.xml
  77. 3
      sideload/socialnorm/github/readme.md
  78. 112
      sideload/socialnorm/github/src/GitHubProvider.php
  79. 0
      sideload/socialnorm/github/tests/.gitkeep
  80. 77
      sideload/socialnorm/github/tests/GitHubProviderTest.php
  81. 12
      sideload/socialnorm/github/tests/TestCase.php
  82. 27
      sideload/socialnorm/github/tests/_fixtures/github_accesstoken.php
  83. 33
      sideload/socialnorm/github/tests/_fixtures/github_email.php
  84. 34
      sideload/socialnorm/github/tests/_fixtures/github_user.php
  85. 4
      sideload/socialnorm/google/.gitignore
  86. 11
      sideload/socialnorm/google/.travis.yml
  87. 30
      sideload/socialnorm/google/composer.json
  88. 18
      sideload/socialnorm/google/phpunit.xml
  89. 3
      sideload/socialnorm/google/readme.md
  90. 78
      sideload/socialnorm/google/src/GoogleProvider.php
  91. 0
      sideload/socialnorm/google/tests/.gitkeep
  92. 75
      sideload/socialnorm/google/tests/GoogleProviderTest.php
  93. 12
      sideload/socialnorm/google/tests/TestCase.php
  94. 24
      sideload/socialnorm/google/tests/_fixtures/google_accesstoken.php
  95. 23
      sideload/socialnorm/google/tests/_fixtures/google_user.php
  96. 4
      sideload/socialnorm/socialnorm/.gitignore
  97. 11
      sideload/socialnorm/socialnorm/.travis.yml
  98. 32
      sideload/socialnorm/socialnorm/composer.json
  99. 18
      sideload/socialnorm/socialnorm/phpunit.xml
  100. 3
      sideload/socialnorm/socialnorm/readme.md
  101. Some files were not shown because too many files have changed in this diff Show More

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DataTable extends Model
{
//
}

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class DiscussionFollow extends Pivot
{
//
}

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TableComment extends Model
{
//
}

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class TableFavourite extends Pivot
{
//
}

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TableRevision extends Model
{
//
}

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserFollow extends Pivot
{
//
}

@ -0,0 +1,76 @@
<?php namespace AdamWathan\EloquentOAuth;
class Authenticator
{
protected $auth;
protected $users;
protected $identities;
public function __construct($auth, $users, $identities)
{
$this->auth = $auth;
$this->users = $users;
$this->identities = $identities;
}
public function login($providerAlias, $userDetails, $callback = null, $remember = false)
{
$user = $this->getUser($providerAlias, $userDetails);
$user = $this->runCallback($callback, $user, $userDetails);
$this->updateUser($user, $providerAlias, $userDetails);
$this->auth->login($user, $remember);
}
protected function getUser($providerAlias, $details)
{
if ($this->identities->userExists($providerAlias, $details)) {
return $this->getExistingUser($providerAlias, $details);
}
return $this->users->create();
}
protected function runCallback($callback, $user, $userDetails)
{
$callback = $callback ?: function () {};
$callbackUser = $callback($user, $userDetails);
return $callbackUser ?: $user;
}
protected function updateUser($user, $providerAlias, $details)
{
$this->users->store($user);
$this->storeProviderIdentity($user, $providerAlias, $details);
}
protected function getExistingUser($providerAlias, $details)
{
$identity = $this->identities->getByProvider($providerAlias, $details);
return $this->users->findByIdentity($identity);
}
protected function storeProviderIdentity($user, $providerAlias, $details)
{
if ($this->identities->userExists($providerAlias, $details)) {
$this->updateProviderIdentity($providerAlias, $details);
} else {
$this->addProviderIdentity($user, $providerAlias, $details);
}
}
protected function updateProviderIdentity($providerAlias, $details)
{
$identity = $this->identities->getByProvider($providerAlias, $details);
$identity->access_token = $details->access_token;
$this->identities->store($identity);
}
protected function addProviderIdentity($user, $providerAlias, $details)
{
$identity = new OAuthIdentity;
$identity->user_id = $user->getKey();
$identity->provider = $providerAlias;
$identity->provider_user_id = $details->id;
$identity->access_token = $details->access_token;
$this->identities->store($identity);
}
}

@ -0,0 +1,28 @@
<?php namespace AdamWathan\EloquentOAuth;
class EloquentIdentityStore implements IdentityStore
{
public function getByProvider($provider, $providerUser)
{
return OAuthIdentity::where('provider', $provider)
->where('provider_user_id', $providerUser->id)
->first();
}
public function flush($user, $provider)
{
OAuthIdentity::where('user_id', $user->getKey())
->where('provider', $provider)
->delete();
}
public function store($identity)
{
$identity->save();
}
public function userExists($provider, $providerUser)
{
return (bool) $this->getByProvider($provider, $providerUser);
}
}

@ -0,0 +1,11 @@
<?php namespace AdamWathan\EloquentOAuth\Facades;
use Illuminate\Support\Facades\Facade;
class OAuth extends Facade
{
protected static function getFacadeAccessor()
{
return 'adamwathan.oauth';
}
}

@ -0,0 +1,9 @@
<?php namespace AdamWathan\EloquentOAuth;
interface IdentityStore
{
public function getByProvider($provider, $providerUser);
public function flush($user, $provider);
public function store($identity);
public function userExists($provider, $providerUser);
}

@ -0,0 +1,26 @@
<?php namespace AdamWathan\EloquentOAuth;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Config;
/**
* @property $id
* @property $user_id
* @property $provider
* @property $provider_user_id
* @property $access_token
*/
class OAuthIdentity extends Eloquent
{
protected static $configuredTable = 'oauth_identities';
public static function configureTable($table)
{
static::$configuredTable = $table;
}
public function getTable()
{
return static::$configuredTable;
}
}

@ -0,0 +1,37 @@
<?php namespace AdamWathan\EloquentOAuth;
class OAuthManager
{
protected $redirect;
protected $authenticator;
protected $socialnorm;
public function __construct($redirect, $authenticator, $socialnorm)
{
$this->redirect = $redirect;
$this->authenticator = $authenticator;
$this->socialnorm = $socialnorm;
}
public function authorize($providerAlias)
{
return $this->redirect->to($this->socialnorm->authorize($providerAlias));
}
public function login($providerAlias, $callback = null)
{
$details = $this->socialnorm->getUser($providerAlias);
return $this->authenticator->login($providerAlias, $details, $callback, $remember = false);
}
public function loginForever($providerAlias, $callback = null)
{
$details = $this->socialnorm->getUser($providerAlias);
return $this->authenticator->login($providerAlias, $details, $callback, $remember = true);
}
public function registerProvider($alias, $provider)
{
$this->socialnorm->registerProvider($alias, $provider);
}
}

@ -0,0 +1,23 @@
<?php namespace AdamWathan\EloquentOAuth;
use SocialNorm\Session as SocialNormSession;
class Session implements SocialNormSession
{
private $store;
public function __construct($store)
{
$this->store = $store;
}
public function get($key)
{
return $this->store->get($key);
}
public function put($key, $value)
{
return $this->store->put($key, $value);
}
}

@ -0,0 +1,27 @@
<?php namespace AdamWathan\EloquentOAuth;
class UserStore
{
protected $model;
public function __construct($model)
{
$this->model = $model;
}
public function create()
{
$user = new $this->model;
return $user;
}
public function store($user)
{
return $user->save();
}
public function findByIdentity($identity)
{
return $identity->belongsTo($this->model, 'user_id')->firstOrFail();
}
}

@ -13,6 +13,7 @@
"doctrine/dbal": "^2.7",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.6.*",
"laravel/socialite": "^3.0",
"laravel/tinker": "^1.0",
"phpoffice/phpspreadsheet": "^1.3"
},
@ -30,7 +31,13 @@
],
"psr-4": {
"App\\": "app/",
"MightyPork\\": "porklib/"
"MightyPork\\": "porklib/",
"AdamWathan\\EloquentOAuth\\": "sideload/adamwathan/eloquent-oauth/src",
"AdamWathan\\EloquentOAuthL5\\": "sideload/adamwathan/eloquent-oauth-l5/src",
"SocialNorm\\Facebook\\": "sideload/socialnorm/facebook/src",
"SocialNorm\\GitHub\\": "sideload/socialnorm/github/src",
"SocialNorm\\Google\\": "sideload/socialnorm/google/src",
"SocialNorm\\": "sideload/socialnorm/socialnorm/src"
},
"files": [
"porklib/helpers.php",

358
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "865cf382701f66c0c92470cde8023805",
"content-hash": "44870bae9f6db81ba961bf49ae2f64ad",
"packages": [
{
"name": "barryvdh/laravel-ide-helper",
@ -845,6 +845,187 @@
],
"time": "2018-02-07T20:20:57+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.3.3",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba",
"reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba",
"shasum": ""
},
"require": {
"guzzlehttp/promises": "^1.0",
"guzzlehttp/psr7": "^1.4",
"php": ">=5.5"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0",
"psr/log": "^1.0"
},
"suggest": {
"psr/log": "Required for using the Log middleware"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "6.3-dev"
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"GuzzleHttp\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle is a PHP HTTP client library",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"rest",
"web service"
],
"time": "2018-04-22T15:46:56+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "v1.3.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646",
"reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646",
"shasum": ""
},
"require": {
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"time": "2016-12-20T10:07:11+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "1.4.2",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
"reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"request",
"response",
"stream",
"uri",
"url"
],
"time": "2017-03-20T17:10:46+00:00"
},
{
"name": "jakub-onderka/php-console-color",
"version": "0.1",
@ -1071,6 +1252,68 @@
],
"time": "2018-06-20T14:21:11+00:00"
},
{
"name": "laravel/socialite",
"version": "v3.0.12",
"source": {
"type": "git",
"url": "https://github.com/laravel/socialite.git",
"reference": "b5f465847b1d637efa86bbfe2fc1c9d2bd12f60f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/socialite/zipball/b5f465847b1d637efa86bbfe2fc1c9d2bd12f60f",
"reference": "b5f465847b1d637efa86bbfe2fc1c9d2bd12f60f",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "~6.0",
"illuminate/contracts": "~5.4",
"illuminate/http": "~5.4",
"illuminate/support": "~5.4",
"league/oauth1-client": "~1.0",
"php": ">=5.4.0"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.0|~5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
},
"laravel": {
"providers": [
"Laravel\\Socialite\\SocialiteServiceProvider"
],
"aliases": {
"Socialite": "Laravel\\Socialite\\Facades\\Socialite"
}
}
},
"autoload": {
"psr-4": {
"Laravel\\Socialite\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Laravel wrapper around OAuth 1 & OAuth 2 libraries.",
"keywords": [
"laravel",
"oauth"
],
"time": "2018-06-01T15:06:47+00:00"
},
{
"name": "laravel/tinker",
"version": "v1.0.7",
@ -1218,6 +1461,69 @@
],
"time": "2018-05-07T08:44:23+00:00"
},
{
"name": "league/oauth1-client",
"version": "1.7.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/oauth1-client.git",
"reference": "fca5f160650cb74d23fc11aa570dd61f86dcf647"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/oauth1-client/zipball/fca5f160650cb74d23fc11aa570dd61f86dcf647",
"reference": "fca5f160650cb74d23fc11aa570dd61f86dcf647",
"shasum": ""
},
"require": {
"guzzlehttp/guzzle": "^6.0",
"php": ">=5.5.0"
},
"require-dev": {
"mockery/mockery": "^0.9",
"phpunit/phpunit": "^4.0",
"squizlabs/php_codesniffer": "^2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"autoload": {
"psr-4": {
"League\\OAuth1\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ben Corlett",
"email": "bencorlett@me.com",
"homepage": "http://www.webcomm.com.au",
"role": "Developer"
}
],
"description": "OAuth 1.0 Client Library",
"keywords": [
"Authentication",
"SSO",
"authorization",
"bitbucket",
"identity",
"idp",
"oauth",
"oauth1",
"single sign on",
"trello",
"tumblr",
"twitter"
],
"time": "2016-08-17T00:36:58+00:00"
},
{
"name": "monolog/monolog",
"version": "1.23.0",
@ -1584,6 +1890,56 @@
],
"time": "2017-02-14T16:28:37+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06T14:39:51+00:00"
},
{
"name": "psr/log",
"version": "1.0.2",

@ -162,6 +162,9 @@ return [
MightyPork\Providers\BladeExtensionsProvider::class,
MightyPork\Providers\MacroServiceProvider::class,
// sideload
AdamWathan\EloquentOAuthL5\EloquentOAuthServiceProvider::class,
],
/*
@ -211,6 +214,8 @@ return [
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
// sideload
'SocialAuth' => AdamWathan\EloquentOAuth\Facades\OAuth::class,
],
// -------------- added keys --------------

@ -35,4 +35,24 @@ return [
'secret' => env('STRIPE_SECRET'),
],
'oauth_providers' => [
'facebook' => [
'client_id' => env('OAUTH_FACEBOOK_ID'),
'client_secret' => env('OAUTH_FACEBOOK_SECRET'),
'redirect_uri' => env('OAUTH_FACEBOOK_REDIRECT'),
'scope' => [],
],
'google' => [
'client_id' => env('OAUTH_GOOGLE_ID'),
'client_secret' => env('OAUTH_GOOGLE_SECRET'),
'redirect_uri' => env('OAUTH_GOOGLE_REDIRECT'),
'scope' => [],
],
'github' => [
'client_id' => env('OAUTH_GITHUB_ID'),
'client_secret' => env('OAUTH_GITHUB_SECRET'),
'redirect_uri' => env('OAUTH_GITHUB_REDIRECT'),
'scope' => [],
],
],
];

@ -0,0 +1,9 @@
<?php
use Faker\Generator as Faker;
$factory->define(\App\Models\DataTable::class, function (Faker $faker) {
return [
//
];
});

@ -15,11 +15,11 @@ class CreateUsersTable extends Migration
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
});
}

@ -0,0 +1,66 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDataTablesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('data_tables', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('owner_id');
$table->unsignedInteger('parent_data_table_id')->nullable();
$table->string('title');
$table->string('license');
$table->foreign('owner_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('parent_data_table_id')
->references('id')->on('data_tables')
->onDelete('set null');
});
Schema::create('table_revisions', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->boolean('approved');
$table->unsignedInteger('data_table_id');
$table->unsignedInteger('parent_revision_id')->nullable();
$table->unsignedInteger('author_id')->nullable();
$table->longText('content');
$table->foreign('data_table_id')
->references('id')->on('data_tables')
->onDelete('cascade');
$table->foreign('parent_revision_id')
->references('id')->on('table_revisions')
->onDelete('set null');
$table->foreign('author_id')
->references('id')->on('users')
->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('table_revisions');
Schema::dropIfExists('data_tables');
}
}

@ -0,0 +1,41 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTableCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('table_comments', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('data_table_id');
$table->unsignedInteger('author_id');
$table->foreign('data_table_id')
->references('id')->on('data_tables')
->onDelete('cascade');
$table->foreign('author_id')
->references('id')->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('table_comments');
}
}

@ -0,0 +1,41 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserFollowsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_follows', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('user_id');
$table->unsignedInteger('target_user_id');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('target_user_id')
->references('id')->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_follows');
}
}

@ -0,0 +1,41 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDiscussionFollowsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('discussion_follows', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('user_id');
$table->unsignedInteger('data_table_id');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('data_table_id')
->references('id')->on('data_tables')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('discussion_follows');
}
}

@ -0,0 +1,42 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTableFavouritesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('table_favourites', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('user_id');
$table->unsignedInteger('data_table_id');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('data_table_id')
->references('id')->on('data_tables')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('table_favourites');
}
}

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOauthIdentitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('oauth_identities', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('user_id');
$table->string('provider_user_id');
$table->string('provider');
$table->string('access_token');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('oauth_identities');
}
}

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->unique();
$table->unsignedInteger('user_id')->nullable();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sessions');
}
}

@ -62,6 +62,15 @@
</div>
</form>
</div>
<div class="card-body">
<a type="submit" href="{{route('oauth-github-authorize')}}" class="btn btn-dark">
{{ __('Register with GitHub') }}
</a>
<a type="submit" href="{{route('oauth-github-login')}}" class="btn btn-dark">
{{ __('Log in with GitHub') }}
</a>
</div>
</div>
</div>
</div>

@ -1,4 +1,6 @@
<?php
use SocialNorm\Exceptions\ApplicationRejectedException;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
/*
|--------------------------------------------------------------------------
@ -11,10 +13,65 @@
|
*/
Auth::routes();
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
// ----------------- SOCIAL LOGIN --------------------
Route::get('/auth/github/authorize', function() {
return SocialAuth::authorize('github');
})->name('oauth-github-authorize');
Route::get('/auth/github/callback', function() {
try {
SocialAuth::login('github');
} catch (ApplicationRejectedException $e) {
dd($e);
abort(401);
} catch (InvalidAuthorizationCodeException $e) {
dd($e);
abort(401);
}
return Redirect::intended();
})->name('oauth-github-login');
Route::get('/auth/google/authorize', function() {
return SocialAuth::authorize('google');
})->name('oauth-google-authorize');
Route::get('/auth/google/login', function() {
try {
SocialAuth::login('google');
} catch (ApplicationRejectedException $e) {
abort(401);
} catch (InvalidAuthorizationCodeException $e) {
abort(401);
}
return Redirect::intended();
})->name('oauth-google-login');
Route::get('/auth/facebook/authorize', function() {
return SocialAuth::authorize('facebook');
})->name('oauth-facebook-authorize');
Route::get('/auth/facebook/login', function() {
try {
SocialAuth::login('facebook');
} catch (ApplicationRejectedException $e) {
abort(401);
} catch (InvalidAuthorizationCodeException $e) {
abort(401);
}
return Redirect::intended();
})->name('oauth-facebook-login');

@ -0,0 +1,3 @@
/vendor
composer.phar
composer.lock

@ -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,24 @@
{
"name": "adamwathan/eloquent-oauth-l5",
"description": "Stupid simple OAuth authentication with Laravel and Eloquent",
"authors": [
{
"name": "Adam Wathan",
"email": "adam.wathan@gmail.com"
}
],
"license": "MIT",
"require": {
"php": ">=5.5.0",
"adamwathan/eloquent-oauth": "~8.1",
"guzzlehttp/guzzle": "^6.2.1"
},
"autoload": {
"psr-4": {
"AdamWathan\\EloquentOAuthL5\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^4.8"
}
}

@ -0,0 +1,46 @@
<?php
use App\User;
return [
'model' => User::class,
'table' => 'oauth_identities',
'providers' => [
'facebook' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/your/facebook/redirect',
'scope' => [],
],
'google' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/your/google/redirect',
'scope' => [],
],
'github' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/your/github/redirect',
'scope' => [],
],
'linkedin' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/your/linkedin/redirect',
'scope' => [],
],
'instagram' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/your/instagram/redirect',
'scope' => [],
],
'soundcloud' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/your/soundcloud/redirect',
'scope' => [],
],
],
];

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateOauthIdentitiesTable extends Migration
{
public function up()
{
$tableName = Config::get('eloquent-oauth.table');
Schema::create($tableName, function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('provider_user_id');
$table->string('provider');
$table->string('access_token');
$table->timestamps();
});
}
public function down()
{
$tableName = Config::get('eloquent-oauth.table');
Schema::drop($tableName);
}
}

@ -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=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

@ -0,0 +1,263 @@
# Eloquent OAuth L5
> Note: Use the [Laravel 4 package](https://github.com/adamwathan/eloquent-oauth-l4) if you are using Laravel 4.
Eloquent OAuth is a package for Laravel 5 designed to make authentication against various OAuth providers *ridiculously* brain-dead simple. Specify your client IDs and secrets in a config file, run a migration and after that it's just two method calls and you have OAuth integration.
#### Video Walkthrough
[![Screenshot](https://cloud.githubusercontent.com/assets/4323180/6274884/ac824c48-b848-11e4-8e4d-531e15f76bc0.png)](https://vimeo.com/120085196)
#### Basic example
```php
// Redirect to Facebook for authorization
Route::get('facebook/authorize', function() {
return SocialAuth::authorize('facebook');
});
// Facebook redirects here after authorization
Route::get('facebook/login', function() {
// Automatically log in existing users
// or create a new user if necessary.
SocialAuth::login('facebook');
// Current user is now available via Auth facade
$user = Auth::user();
return Redirect::intended();
});
```
#### Supported Providers
- Facebook
- GitHub
- Google
- LinkedIn
- Instagram
- Soundcloud
>Feel free to open an issue if you would like support for a particular provider, or even better, submit a pull request.
## Installation
#### Add this package using Composer
From the command line inside your project directory, simply type:
`composer require adamwathan/eloquent-oauth-l5`
#### Update your config
Add the service provider to the `providers` array in `config/app.php`:
```php
'providers' => [
// ...
AdamWathan\EloquentOAuthL5\EloquentOAuthServiceProvider::class,
// ...
]
```
Add the facade to the `aliases` array in `config/app.php`:
```php
'aliases' => [
// ...
'SocialAuth' => AdamWathan\EloquentOAuth\Facades\OAuth::class,
// ...
]
```
#### Publish the package configuration
Publish the configuration file and migrations by running the provided console command:
`php artisan eloquent-oauth:install`
Next, re-migrate your database:
`php artisan migrate`
> If you need to change the name of the table used to store OAuth identities, you can do so in the `eloquent-oauth` config file.
#### Configure the providers
Update your app information for the providers you are using in `config/eloquent-oauth.php`:
```php
'providers' => [
'facebook' => [
'client_id' => '12345678',
'client_secret' => 'y0ur53cr374ppk3y',
'redirect_uri' => 'https://example.com/facebook/login'),
'scope' => [],
]
]
```
> Each provider is preconfigured with the scope necessary to retrieve basic user information and the user's email address, so the scope array can usually be left empty unless you need specific additional permissions. Consult the provider's API documentation to find out what permissions are available for the various services.
All done!
> Eloquent OAuth is designed to integrate with Laravel's Eloquent authentication driver, so be sure you are using the `eloquent` driver in `app/config/auth.php`. You can define your actual `User` model however you choose and add whatever behavior you need, just be sure to specify the model you are using with its fully qualified namespace in `app/config/auth.php` as well.
#### Custom providers
If you'd like to register a provider for a service that isn't supported out of the box, you can do so by first specifying the configuration needed for that provider in your `config/eloquent-oauth.php`:
```php
// config/eloquent-oauth.php
return [
'model' => User::class,
'table' => 'oauth_identities',
'providers' => [
'facebook' => [ /* ... */],
'google' => [ /* ... */],
'gumroad' => [
'client_id' => env('GUMROAD_CLIENT_ID'),
'client_secret' => env('GUMROAD_CLIENT_SECRET'),
'redirect_uri' => env('GUMROAD_REDIRECT_URI'),
'scope' => ['view_sales'],
],
],
];
```
Then specify which class should be used for that provider by extending `EloquentOAuthServiceProvider` with your own implementation that maps the provider name to a provider class:
```php
class MySocialAuthServiceProvider extends EloquentOAuthServiceProvider
{
protected function getProviderLookup()
{
// Merge the default providers if you like, or override entirely
// to skip loading those providers completely.
return array_merge($this->providerLookup, [
'gumroad' => GumroadProvider::class
]);
}
}
```
> Don't forget to use your extension of the provider in `config/app.php` instead of the one supplied by the package.
If your provider follows the same `__construct($config, $httpClient, $request)` parameter signature that the default providers use, you're done.
If not, make sure you bind your provider to the IOC container in another provider, and the package will make sure to fetch the implementation from the container instead of trying to construct it itself.
To get an idea of how the providers work, take a look at how the packaged providers are implemented:
- https://github.com/adamwathan/socialnorm-google
- https://github.com/adamwathan/socialnorm-github
- https://github.com/adamwathan/socialnorm-instagram
- https://github.com/adamwathan/socialnorm-soundcloud
- https://github.com/adamwathan/socialnorm-facebook
- https://github.com/adamwathan/socialnorm-linkedin
## Usage
Authentication against an OAuth provider is a multi-step process, but I have tried to simplify it as much as possible.
### Authorizing with the provider
First you will need to define the authorization route. This is the route that your "Login" button will point to, and this route redirects the user to the provider's domain to authorize your app. After authorization, the provider will redirect the user back to your second route, which handles the rest of the authentication process.
To authorize the user, simply return the `SocialAuth::authorize()` method directly from the route.
```php
Route::get('facebook/authorize', function() {
return SocialAuth::authorize('facebook');
});
```
### Authenticating within your app
Next you need to define a route for authenticating against your app with the details returned by the provider.
For basic cases, you can simply call `SocialAuth::login()` with the provider name you are authenticating with. If the user
rejected your application, this method will throw an `ApplicationRejectedException` which you can catch and handle
as necessary.
The `login` method will create a new user if necessary, or update an existing user if they have already used your application
before.
Once the `login` method succeeds, the user will be authenticated and available via `Auth::user()` just like if they
had logged in through your application normally.
```php
use SocialNorm\Exceptions\ApplicationRejectedException;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
Route::get('facebook/login', function() {
try {
SocialAuth::login('facebook');
} catch (ApplicationRejectedException $e) {
// User rejected application
} catch (InvalidAuthorizationCodeException $e) {
// Authorization was attempted with invalid
// code,likely forgery attempt
}
// Current user is now available via Auth facade
$user = Auth::user();
return Redirect::intended();
});
```
If you need to do anything with the newly created user, you can pass an optional closure as the second
argument to the `login` method. This closure will receive the `$user` instance and a `ProviderUserDetails`
object that contains basic information from the OAuth provider, including:
- User ID
- Nickname
- Full Name
- Email
- Avatar URL
- Access Token
```php
SocialAuth::login('facebook', function($user, $details) {
$user->nickname = $details->nickname;
$user->name = $details->full_name;
$user->profile_image = $details->avatar;
$user->save();
});
```
> Note: The Instagram and Soundcloud APIs do not allow you to retrieve the user's email address, so unfortunately that field will always be `null` for those providers.
### Advanced: Storing additional data
Remember: One of the goals of the Eloquent OAuth package is to normalize the data received across all supported providers, so that you can count on those specific data items (explained above) being available in the `$details` object.
But, each provider offers its own sets of additional data. If you need to access or store additional data beyond the basics of what Eloquent OAuth's default `ProviderUserDetails` object supplies, you need to do two things:
1. Request it from the provider, by extending its scope:
Say for example we want to collect the user's gender when they login using Facebook.
In the `config/eloquent-oauth.php` file, set the `[scope]` in the `facebook` provider section to include the `public_profile` scope, like this:
```php
'scope' => ['public_profile'],
```
> For available scopes with each provider, consult that provider's API documentation.
> Note: By increasing the scope you will be asking the user to grant access to additional information. They will be informed of the scopes you're requesting. If you ask for too much unnecessary data, they may refuse. So exercise restraint when requesting additional scopes.
2. Now where you do your `SocialAuth::login`, store the to your `$user` object by accessing the `$details->raw()['KEY']` data:
```php
SocialAuth::login('facebook', function($user, $details) (
$user->gender = $details->raw()['gender'];
$user->save();
});
```
> Tip: You can see what the available keys are by testing with `dd($details->raw());` inside that same closure.

@ -0,0 +1,127 @@
<?php namespace AdamWathan\EloquentOAuthL5;
use Illuminate\Support\ServiceProvider;
use GuzzleHttp\Client as HttpClient;
use SocialNorm\SocialNorm;
use SocialNorm\ProviderRegistry;
use SocialNorm\Request;
use SocialNorm\StateGenerator;
use AdamWathan\EloquentOAuth\Authenticator;
use AdamWathan\EloquentOAuth\EloquentIdentityStore;
use AdamWathan\EloquentOAuth\IdentityStore;
use AdamWathan\EloquentOAuth\Session;
use AdamWathan\EloquentOAuth\OAuthIdentity;
use AdamWathan\EloquentOAuth\OAuthManager;
use AdamWathan\EloquentOAuth\UserStore;
class EloquentOAuthServiceProvider extends ServiceProvider {
protected $providerLookup = [
'facebook' => 'SocialNorm\Facebook\FacebookProvider',
'github' => 'SocialNorm\GitHub\GitHubProvider',
'google' => 'SocialNorm\Google\GoogleProvider',
];
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// $this->configureOAuthIdentitiesTable();
$this->registerIdentityStore();
$this->registerOAuthManager();
// $this->registerCommands();
}
protected function registerIdentityStore()
{
$this->app->singleton('AdamWathan\EloquentOAuth\IdentityStore', function ($app) {
return new EloquentIdentityStore;
});
}
protected function registerOAuthManager()
{
$this->app->singleton('adamwathan.oauth', function ($app) {
$providerRegistry = new ProviderRegistry;
$session = new Session($app['session']);
$request = new Request($app['request']->all());
$stateGenerator = new StateGenerator;
$socialnorm = new SocialNorm($providerRegistry, $session, $request, $stateGenerator);
$this->registerProviders($socialnorm, $request);
// take user model from the config file
$users = new UserStore($app['config']['auth.providers.users.model']);
$authenticator = new Authenticator(
$app['Illuminate\Contracts\Auth\Guard'],
$users,
$app['AdamWathan\EloquentOAuth\IdentityStore']
);
$oauth = new OAuthManager($app['redirect'], $authenticator, $socialnorm);
return $oauth;
});
}
protected function registerProviders($socialnorm, $request)
{
if (! $providerAliases = $this->app['config']['services.oauth_providers']) {
return;
}
foreach ($providerAliases as $alias => $config) {
if (isset($this->getProviderLookup()[$alias])) {
$providerClass = $this->getProviderLookup()[$alias];
if ($this->app->bound($providerClass)) {
$provider = $this->app->make($providerClass);
} else {
$provider = new $providerClass($config, new HttpClient, $request);
}
$socialnorm->registerProvider($alias, $provider);
}
}
}
protected function getProviderLookup()
{
return $this->providerLookup;
}
// protected function configureOAuthIdentitiesTable()
// {
// OAuthIdentity::configureTable($this->app['config']['eloquent-oauth.table']);
// }
// /**
// * Registers some utility commands with artisan
// * @return void
// */
// public function registerCommands()
// {
// $this->app->bind('command.eloquent-oauth.install', 'AdamWathan\EloquentOAuthL5\Installation\InstallCommand');
// $this->commands('command.eloquent-oauth.install');
// }
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['adamwathan.oauth'];
}
}

@ -0,0 +1,5 @@
<?php namespace AdamWathan\EloquentOAuthL5\Installation;
use Exception;
class FileExistsException extends Exception {}

@ -0,0 +1,70 @@
<?php namespace AdamWathan\EloquentOAuthL5\Installation;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Composer as Composer51;
use Illuminate\Support\Composer as Composer52;
use Symfony\Component\Console\Input\InputOption;
class InstallCommand extends Command
{
protected $filesystem;
protected $composer;
protected $name = 'eloquent-oauth:install';
protected $description = 'Install package config and migrations';
public function __construct(Filesystem $filesystem)
{
parent::__construct();
$this->filesystem = $filesystem;
if (class_exists(Composer52::class)) {
$this->composer = app(Composer52::class);
} else {
$this->composer = app(Composer51::class);
}
}
public function handle()
{
try {
$this->publishConfig();
$this->publishMigrations();
$this->composer->dumpAutoloads();
$this->comment('Package configuration and migrations installed!');
} catch (FileExistsException $e) {
$this->error('It looks like this package has already been installed. Use --force to override.');
}
}
public function publishConfig()
{
$this->publishFile(__DIR__ . '/../../config/eloquent-oauth.php', config_path() . '/eloquent-oauth.php');
$this->info('Configuration published.');
}
public function publishMigrations()
{
$name = 'create_oauth_identities_table';
$path = $this->laravel['path.database'] . '/migrations';
$fullPath = $this->laravel['migration.creator']->create($name, $path);
$this->filesystem->put($fullPath, $this->filesystem->get(__DIR__ . '/../../migrations/create_oauth_identities_table.stub'));
}
public function publishFile($from, $to)
{
if ($this->filesystem->exists($to) && !$this->option('force')) {
throw new FileExistsException;
}
$this->filesystem->copy($from, $to);
}
protected function getOptions()
{
return [
['force', null, InputOption::VALUE_NONE, 'Overwrite any existing files.'],
];
}
}

@ -0,0 +1,10 @@
# To Do
- Add tests for individual provider implementations
- Probably extract providers into their own packages
- Add documentation for creating your own providers
- Delete created user and OAuth identity if anything goes wrong that would leave the user in an "unfinished" state after initial creation
- Add exception handling for "user creation failed" (unique constraints or just database errors, whatever)
- Maybe stop storing access token? Don't actually ever use it again, it's totally single use...
- Remove hard dependency on Session\Store, replace with some sort of "CrossRequestPersistanceInterface" or something
- Look for more opportunities to add abstractions to different provider implementations. Had to do some crappy stuff with the LinkedIn provider.
- Twitter support, going to be interesting...

@ -0,0 +1,3 @@
/vendor
composer.phar
composer.lock

@ -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,39 @@
{
"name": "adamwathan/eloquent-oauth",
"description": "Stupid simple OAuth authentication with Laravel and Eloquent",
"authors": [
{
"name": "Adam Wathan",
"email": "adam.wathan@gmail.com"
}
],
"license": "MIT",
"require": {
"php": ">=5.5.0",
"illuminate/auth": "4.*|5.*",
"illuminate/session": "4.*|5.*",
"illuminate/database": "4.*|5.*",
"illuminate/http": "4.*|5.*",
"illuminate/routing": "4.*|5.*",
"illuminate/support": "4.*|5.*",
"socialnorm/socialnorm": "^0.2",
"socialnorm/facebook": "^0.2",
"socialnorm/github": "^0.2",
"socialnorm/linkedin": "^0.2",
"socialnorm/google": "^0.2",
"socialnorm/instagram": "^0.2",
"socialnorm/soundcloud": "^0.2"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "^4.8"
},
"autoload": {
"psr-4": {
"AdamWathan\\EloquentOAuth\\": "src/"
},
"classmap": [
"tests/FunctionalTestCase.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=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

@ -0,0 +1,159 @@
# Eloquent OAuth
[![Code Climate](https://codeclimate.com/github/adamwathan/eloquent-oauth/badges/gpa.svg)](https://codeclimate.com/github/adamwathan/eloquent-oauth)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/adamwathan/eloquent-oauth/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/adamwathan/eloquent-oauth/?branch=master)
[![Build Status](https://api.travis-ci.org/adamwathan/eloquent-oauth.svg)](https://travis-ci.org/adamwathan/eloquent-oauth)
> - Use the [Laravel 4 wrapper](https://github.com/adamwathan/eloquent-oauth-l4) for easy integration with Laravel 4.
> - Use the [Laravel 5 wrapper](https://github.com/adamwathan/eloquent-oauth-l5) for easy integration with Laravel 5.
Eloquent OAuth is a package for Laravel designed to make authentication against various OAuth providers *ridiculously* brain-dead simple. Specify your client IDs and secrets in a config file, run a migration and after that it's just two method calls and you have OAuth integration.
#### Video Walkthrough
[![Screenshot](https://cloud.githubusercontent.com/assets/4323180/6274884/ac824c48-b848-11e4-8e4d-531e15f76bc0.png)](https://vimeo.com/120085196)
#### Basic example
```php
// Redirect to Facebook for authorization
Route::get('facebook/authorize', function() {
return OAuth::authorize('facebook');
});
// Facebook redirects here after authorization
Route::get('facebook/login', function() {
// Automatically log in existing users
// or create a new user if necessary.
OAuth::login('facebook');
// Current user is now available via Auth facade
$user = Auth::user();
return Redirect::intended();
});
```
#### Supported Providers
- Facebook
- GitHub
- Google
- LinkedIn
- Instagram
- SoundCloud
>Feel free to open an issue if you would like support for a particular provider, or even better, submit a pull request.
## Installation
Check the appropriate wrapper package for installation instructions for your version of Laravel.
- [Laravel 4 wrapper](https://github.com/adamwathan/eloquent-oauth-l4)
- [Laravel 5 wrapper](https://github.com/adamwathan/eloquent-oauth-l5)
## Usage
Authentication against an OAuth provider is a multi-step process, but I have tried to simplify it as much as possible.
### Authorizing with the provider
First you will need to define the authorization route. This is the route that your "Login" button will point to, and this route redirects the user to the provider's domain to authorize your app. After authorization, the provider will redirect the user back to your second route, which handles the rest of the authentication process.
To authorize the user, simply return the `OAuth::authorize()` method directly from the route.
```php
Route::get('facebook/authorize', function() {
return OAuth::authorize('facebook');
});
```
### Authenticating within your app
Next you need to define a route for authenticating against your app with the details returned by the provider.
For basic cases, you can simply call `OAuth::login()` with the provider name you are authenticating with. If the user
rejected your application, this method will throw an `ApplicationRejectedException` which you can catch and handle
as necessary.
The `login` method will create a new user if necessary, or update an existing user if they have already used your application
before.
Once the `login` method succeeds, the user will be authenticated and available via `Auth::user()` just like if they
had logged in through your application normally.
```php
use SocialNorm\Exceptions\ApplicationRejectedException;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
Route::get('facebook/login', function() {
try {
OAuth::login('facebook');
} catch (ApplicationRejectedException $e) {
// User rejected application
} catch (InvalidAuthorizationCodeException $e) {
// Authorization was attempted with invalid
// code,likely forgery attempt
}
// Current user is now available via Auth facade
$user = Auth::user();
return Redirect::intended();
});
```
If you need to do anything with the newly created user, you can pass an optional closure as the second
argument to the `login` method. This closure will receive the `$user` instance and a `SocialNorm\User`
object that contains basic information from the OAuth provider, including:
- `id`
- `nickname`
- `full_name`
- `avatar`
- `email`
- `access_token`
```php
OAuth::login('facebook', function($user, $details) {
$user->nickname = $details->nickname;
$user->name = $details->full_name;
$user->profile_image = $details->avatar;
$user->save();
});
```
> Note: The Instagram and Soundcloud APIs do not allow you to retrieve the user's email address, so unfortunately that field will always be `null` for those provider.
### Advanced: Storing additional data
Remember: One of the goals of the Eloquent OAuth package is to normalize the data received across all supported providers, so that you can count on those specific data items (explained above) being available in the `$details` object.
But, each provider offers its own sets of additional data. If you need to access or store additional data beyond the basics of what Eloquent OAuth's default `ProviderUserDetails` object supplies, you need to do two things:
1. Request it from the provider, by extending its scope:
Say for example we want to collect the user's gender when they login using Facebook.
In the `config/eloquent-oauth.php` file, set the `[scope]` in the `facebook` provider section to include the `public_profile` scope, like this:
```php
'scope' => ['email', 'public_profile'],
```
> For available scopes with each provider, consult that provider's API documentation.
> NOTE: By increasing the scope you will be asking the user to grant access to additional information. They will be informed of the scopes you're requesting. If you ask for too much unnecessary data, they may refuse. So exercise restraint when requesting additional scopes.
2. Now where you do your `OAuth::login`, store the to your `$user` object by accessing the `$details->raw()['KEY']` data:
```php
OAuth::login('facebook', function($user, $details) (
$user->gender = $details->raw()['gender']; // Or whatever the key is
$user->save();
});
```
> TIP: You can see what the available keys are by testing with `dd($details->raw());` inside that same closure.

@ -0,0 +1,83 @@
<?php namespace AdamWathan\EloquentOAuth;
use SocialNorm\User;
class Authenticator
{
protected $auth;
protected $users;
protected $identities;
public function __construct($auth, $users, $identities)
{
$this->auth = $auth;
$this->users = $users;
$this->identities = $identities;
}
public function login($providerAlias, $userDetails, $callback = null)
{
$user = $this->getUser($providerAlias, $userDetails);
$user = $this->runCallback($callback, $user, $userDetails);
$this->updateUser($user, $providerAlias, $userDetails);
$this->auth->login($user);
}
protected function getUser($providerAlias, \SocialNorm\User $details)
{
if ($this->identities->userExists($providerAlias, $details)) {
return $this->getExistingUser($providerAlias, $details);
}
/** @var \App\Models\User $newuser */
$newuser = $this->users->create();
$newuser->name = $details->nickname ?: ($details->full_name ?: $details->email);
$newuser->email = $details->email;
return $newuser;
}
protected function runCallback($callback, $user, $userDetails)
{
$callback = $callback ?: function () {};
$callbackUser = $callback($user, $userDetails);
return $callbackUser ?: $user;
}
protected function updateUser($user, $providerAlias, $details)
{
$this->users->store($user);
$this->storeProviderIdentity($user, $providerAlias, $details);
}
protected function getExistingUser($providerAlias, $details)
{
$identity = $this->identities->getByProvider($providerAlias, $details);
return $this->users->findByIdentity($identity);
}
protected function storeProviderIdentity($user, $providerAlias, $details)
{
if ($this->identities->userExists($providerAlias, $details)) {
$this->updateProviderIdentity($providerAlias, $details);
} else {
$this->addProviderIdentity($user, $providerAlias, $details);
}
}
protected function updateProviderIdentity($providerAlias, $details)
{
$identity = $this->identities->getByProvider($providerAlias, $details);
$identity->access_token = $details->access_token;
$this->identities->store($identity);
}
protected function addProviderIdentity($user, $providerAlias, $details)
{
$identity = new OAuthIdentity;
$identity->user_id = $user->getKey();
$identity->provider = $providerAlias;
$identity->provider_user_id = $details->id;
$identity->access_token = $details->access_token;
$this->identities->store($identity);
}
}

@ -0,0 +1,28 @@
<?php namespace AdamWathan\EloquentOAuth;
class EloquentIdentityStore implements IdentityStore
{
public function getByProvider($provider, $providerUser)
{
return OAuthIdentity::where('provider', $provider)
->where('provider_user_id', $providerUser->id)
->first();
}
public function flush($user, $provider)
{
OAuthIdentity::where('user_id', $user->getKey())
->where('provider', $provider)
->delete();
}
public function store($identity)
{
$identity->save();
}
public function userExists($provider, $providerUser)
{
return (bool) $this->getByProvider($provider, $providerUser);
}
}

@ -0,0 +1,11 @@
<?php namespace AdamWathan\EloquentOAuth\Facades;
use Illuminate\Support\Facades\Facade;
class OAuth extends Facade
{
protected static function getFacadeAccessor()
{
return 'adamwathan.oauth';
}
}

@ -0,0 +1,9 @@
<?php namespace AdamWathan\EloquentOAuth;
interface IdentityStore
{
public function getByProvider($provider, $providerUser);
public function flush($user, $provider);
public function store($identity);
public function userExists($provider, $providerUser);
}

@ -0,0 +1,26 @@
<?php namespace AdamWathan\EloquentOAuth;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Config;
/**
* @property $id
* @property $user_id
* @property $provider
* @property $provider_user_id
* @property $access_token
*/
class OAuthIdentity extends Eloquent
{
protected static $configuredTable = 'oauth_identities';
// public static function configureTable($table)
// {
// static::$configuredTable = $table;
// }
public function getTable()
{
return static::$configuredTable;
}
}

@ -0,0 +1,38 @@
<?php namespace AdamWathan\EloquentOAuth;
use SocialNorm\SocialNorm;
class OAuthManager
{
/** @var string */
protected $redirect;
/** @var Authenticator */
protected $authenticator;
/** @var SocialNorm */
protected $socialnorm;
public function __construct($redirect, $authenticator, $socialnorm)
{
$this->redirect = $redirect;
$this->authenticator = $authenticator;
$this->socialnorm = $socialnorm;
}
public function authorize($providerAlias)
{
return $this->redirect->to($this->socialnorm->authorize($providerAlias));
}
public function login($providerAlias, $callback = null)
{
$details = $this->socialnorm->getUser($providerAlias);
return $this->authenticator->login($providerAlias, $details, $callback);
}
public function registerProvider($alias, $provider)
{
$this->socialnorm->registerProvider($alias, $provider);
}
}

@ -0,0 +1,23 @@
<?php namespace AdamWathan\EloquentOAuth;
use SocialNorm\Session as SocialNormSession;
class Session implements SocialNormSession
{
private $store;
public function __construct($store)
{
$this->store = $store;
}
public function get($key)
{
return $this->store->get($key);
}
public function put($key, $value)
{
return $this->store->put($key, $value);
}
}

@ -0,0 +1,27 @@
<?php namespace AdamWathan\EloquentOAuth;
class UserStore
{
protected $model;
public function __construct($model)
{
$this->model = $model;
}
public function create()
{
$user = new $this->model;
return $user;
}
public function store($user)
{
return $user->save();
}
public function findByIdentity($identity)
{
return $identity->belongsTo($this->model, 'user_id')->firstOrFail();
}
}

@ -0,0 +1,122 @@
<?php
use Mockery as M;
use AdamWathan\EloquentOAuth\Authenticator;
use AdamWathan\EloquentOAuth\OAuthIdentity;
use SocialNorm\User as SocialNormUser;
class AuthenticatorTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
M::close();
}
public function test_login_creates_new_user_if_no_matching_user_exists()
{
$providerAlias = 'provider';
$auth = M::spy();
$users = M::spy();
$identities = M::spy();
$userDetails = new SocialNormUser([]);
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$identities->shouldReceive('userExists')->andReturn(false);
$users->shouldReceive('create')->andReturn($user);
$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails);
$users->shouldHaveReceived('create');
$users->shouldHaveReceived('store')->with($user);
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($user);
}
public function test_login_uses_existing_user_if_matching_user_exists()
{
$providerAlias = 'provider';
$userDetails = new SocialNormUser([]);
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$auth = M::spy();
$users = M::spy([
'findByIdentity' => $user
]);
$identities = M::spy([
'userExists' => true,
'getByProvider' => new OAuthIdentity,
]);
$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails);
$users->shouldNotHaveReceived('create');
$users->shouldHaveReceived('store')->with($user);
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($user);
}
public function test_if_a_user_is_returned_from_the_callback_that_user_is_used()
{
$providerAlias = 'provider';
$userDetails = new SocialNormUser([]);
$user = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$auth = M::spy();
$users = M::spy([
'findByIdentity' => $user
]);
$identities = M::spy([
'userExists' => true,
'getByProvider' => new OAuthIdentity,
]);
$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails, function () use ($otherUser) {
return $otherUser;
});
$users->shouldNotHaveReceived('create');
$users->shouldHaveReceived('store')->with($otherUser);
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($otherUser);
}
public function test_if_nothing_is_returned_from_the_callback_the_found_or_created_user_is_used()
{
$providerAlias = 'provider';
$userDetails = new SocialNormUser([]);
$foundUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$otherUser = M::mock('Illuminate\Contracts\Auth\Authenticatable')->shouldIgnoreMissing();
$auth = M::spy();
$users = M::spy([
'findByIdentity' => $foundUser
]);
$identities = M::spy([
'userExists' => true,
'getByProvider' => new OAuthIdentity,
]);
$authenticator = new Authenticator($auth, $users, $identities);
$authenticator->login('provider', $userDetails, function () {
return;
});
$users->shouldNotHaveReceived('create');
$users->shouldHaveReceived('store')->with($foundUser);
$identities->shouldHaveReceived('store');
$auth->shouldHaveReceived('login')->with($foundUser);
}
}

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Capsule\Manager as DB;
abstract class FunctionalTestCase extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->configureDatabase();
$this->migrateIdentitiesTable();
}
protected function configureDatabase()
{
$db = new DB;
$db->addConnection(array(
'driver' => 'sqlite',
'database' => ':memory:',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
$db->bootEloquent();
$db->setAsGlobal();
}
public function migrateIdentitiesTable()
{
DB::schema()->create('oauth_identities', function($table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('provider_user_id');
$table->string('provider');
$table->string('access_token');
$table->timestamps();
});
}
}

@ -0,0 +1,155 @@
<?php
use AdamWathan\EloquentOAuth\OAuthIdentity;
use AdamWathan\EloquentOAuth\EloquentIdentityStore;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Mockery as M;
use SocialNorm\User as UserDetails;
class IdentityStoreTest extends FunctionalTestCase
{
public function setUp()
{
parent::setUp();
Eloquent::unguard();
}
public function test_get_by_provider()
{
OAuthIdentity::create(array(
'user_id' => 1,
'provider' => 'facebook',
'provider_user_id' => 'foobar',
'access_token' => 'abc123',
));
OAuthIdentity::create(array(
'user_id' => 2,
'provider' => 'facebook',
'provider_user_id' => 'bazfoo',
'access_token' => 'def456',
));
$details = new UserDetails(array(
'access_token' => 'new-token',
'id' => 'bazfoo',
'nickname' => 'john.doe',
'full_name' => 'John Doe',
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new EloquentIdentityStore;
$identity = $identities->getByProvider('facebook', $details);
$this->assertEquals(2, $identity->user_id);
$this->assertEquals('facebook', $identity->provider);
$this->assertEquals('bazfoo', $identity->provider_user_id);
$this->assertEquals('def456', $identity->access_token);
}
public function test_get_by_provider_when_no_match()
{
OAuthIdentity::create(array(
'user_id' => 1,
'provider' => 'facebook',
'provider_user_id' => 'foobar',
'access_token' => 'abc123',
));
OAuthIdentity::create(array(
'user_id' => 2,
'provider' => 'facebook',
'provider_user_id' => 'bazfoo',
'access_token' => 'def456',
));
$details = new UserDetails(array(
'access_token' => 'new-token',
'id' => 'missing-id',
'nickname' => 'john.doe',
'full_name' => 'John Doe',
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new EloquentIdentityStore;
$identity = $identities->getByProvider('facebook', $details);
$this->assertNull($identity);
}
public function test_flush()
{
OAuthIdentity::create(array(
'user_id' => 1,
'provider' => 'facebook',
'provider_user_id' => 'foobar',
'access_token' => 'abc123',
));
OAuthIdentity::create(array(
'user_id' => 2,
'provider' => 'facebook',
'provider_user_id' => 'bazfoo',
'access_token' => 'def456',
));
$this->assertEquals(1, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count());
$identities = new EloquentIdentityStore;
$user = M::mock();
$user->shouldReceive('getKey')->andReturn(2);
$identities->flush($user, 'facebook');
$this->assertEquals(0, OAuthIdentity::where('provider', 'facebook')->where('user_id', 2)->count());
}
public function test_store()
{
$identity = new OAuthIdentity(array(
'user_id' => 1,
'provider' => 'facebook',
'provider_user_id' => 'foobar',
'access_token' => 'abc123',
));
$this->assertEquals(0, OAuthIdentity::count());
$identities = new EloquentIdentityStore;
$identities->store($identity);
$this->assertEquals(1, OAuthIdentity::count());
}
public function test_user_exists_returns_true_when_user_exists()
{
OAuthIdentity::create(array(
'user_id' => 2,
'provider' => 'facebook',
'provider_user_id' => 'bazfoo',
'access_token' => 'def456',
));
$details = new UserDetails(array(
'access_token' => 'new-token',
'id' => 'bazfoo',
'nickname' => 'john.doe',
'full_name' => 'John Doe',
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new EloquentIdentityStore;
$this->assertTrue($identities->userExists('facebook', $details));
}
public function test_user_exists_returns_false_when_user_doesnt_exist()
{
OAuthIdentity::create(array(
'user_id' => 2,
'provider' => 'facebook',
'provider_user_id' => 'foobar',
'access_token' => 'def456',
));
$details = new UserDetails(array(
'access_token' => 'new-token',
'id' => 'bazfoo',
'nickname' => 'john.doe',
'full_name' => 'John Doe',
'email' => 'john.doe@example.com',
'avatar' => 'http://example.com/photos/john_doe.jpg',
));
$identities = new EloquentIdentityStore;
$this->assertFalse($identities->userExists('facebook', $details));
}
}

@ -0,0 +1,54 @@
<?php
use Mockery as M;
use AdamWathan\EloquentOAuth\OAuthManager;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Routing\RouteCollection;
use Illuminate\Http\Request;
class OAuthManagerTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
M::close();
}
public function test_it_returns_a_redirect_to_the_authorize_url()
{
$redirector = $this->buildRedirector();
$authenticator = M::mock('AdamWathan\EloquentOAuth\Authenticator');
$socialnorm = M::mock('SocialNorm\SocialNorm');
$socialnorm->shouldReceive('authorize')->with('example')->andReturn('http://example.com/authorize');
$oauth = new OAuthManager($redirector, $authenticator, $socialnorm);
$response = $oauth->authorize('example');
$this->assertEquals('http://example.com/authorize', $response->getTargetUrl());
}
public function test_it_logs_the_user_in()
{
$providerAlias = 'twitbook';
$socialnormUser = new SocialNorm\User([]);
$callback = function () {};
$redirector = $this->buildRedirector();
$authenticator = M::spy('AdamWathan\EloquentOAuth\Authenticator');
$socialnorm = M::mock('SocialNorm\SocialNorm');
$socialnorm->shouldReceive('getUser')
->with($providerAlias)
->andReturn($socialnormUser);
$oauth = new OAuthManager($redirector, $authenticator, $socialnorm);
$oauth->login($providerAlias, $callback);
$authenticator->shouldHaveReceived('login')->with($providerAlias, $socialnormUser, $callback);
}
private function buildRedirector()
{
return new Redirector(new UrlGenerator(new RouteCollection, new Request));
}
}

@ -0,0 +1,10 @@
# To Do
- Add tests for individual provider implementations
- Probably extract providers into their own packages
- Add documentation for creating your own providers
- Delete created user and OAuth identity if anything goes wrong that would leave the user in an "unfinished" state after initial creation
- Add exception handling for "user creation failed" (unique constraints or just database errors, whatever)
- Maybe stop storing access token? Don't actually ever use it again, it's totally single use...
- Remove hard dependency on Session\Store, replace with some sort of "CrossRequestPersistanceInterface" or something
- Look for more opportunities to add abstractions to different provider implementations. Had to do some crappy stuff with the LinkedIn provider.
- Twitter support, going to be interesting...

@ -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,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"
]
}
}

@ -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 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}'
];

@ -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"}'
];

@ -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/google",
"description": "Google 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\\Google\\": "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 Google Provider
@todo: Add docs :)

@ -0,0 +1,78 @@
<?php namespace SocialNorm\Google;
use SocialNorm\Exceptions\InvalidAuthorizationCodeException;
use SocialNorm\Providers\OAuth2Provider;
class GoogleProvider extends OAuth2Provider
{
protected $authorizeUrl = "https://accounts.google.com/o/oauth2/auth";
protected $accessTokenUrl = "https://accounts.google.com/o/oauth2/token";
protected $userDataUrl = "https://www.googleapis.com/userinfo/v2/me";
protected $scope = [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
];
protected $headers = [
'authorize' => [],
'access_token' => [
'Content-Type' => 'application/x-www-form-urlencoded'
],
'user_details' => [],
];
protected function compileScopes()
{
return implode(' ', $this->scope);
}
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 parseUserDataResponse($response)
{
return json_decode($response, true);
}
protected function userId()
{
return $this->getProviderUserData('id');
}
protected function nickname()
{
return $this->getProviderUserData('email');
}
protected function fullName()
{
return $this->getProviderUserData('given_name') . ' ' . $this->getProviderUserData('family_name');
}
protected function avatar()
{
return $this->getProviderUserData('picture');
}
protected function email()
{
return $this->getProviderUserData('email');
}
}

@ -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"}'
];

@ -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,32 @@
{
"name": "socialnorm/socialnorm",
"description": "Normalize user details between various OAuth providers",
"authors": [
{
"name": "Adam Wathan",
"email": "adam.wathan@gmail.com"
}
],
"license": "MIT",
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0"
},
"require-dev": {
"mockery/mockery": "~0.8",
"phpunit/phpunit": "^4.8"
},
"autoload": {
"psr-4": {
"SocialNorm\\": "src/"
}
},
"autoload-dev": {
"classmap": [
"tests/_support"
],
"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
Will be something eventually...

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save