From d74b76da3e5edab75ee35984b32d1be97792ebd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 10 Jul 2018 21:55:47 +0200 Subject: [PATCH] fixing up the authenticator to allow identiy chaining --- app/SocialLogin/Authenticator.php | 76 ------------- app/SocialLogin/EloquentIdentityStore.php | 28 ----- app/SocialLogin/Facades/OAuth.php | 11 -- app/SocialLogin/IdentityStore.php | 9 -- app/SocialLogin/OAuthIdentity.php | 26 ----- app/SocialLogin/OAuthManager.php | 37 ------- app/SocialLogin/Session.php | 23 ---- app/SocialLogin/UserStore.php | 27 ----- composer.json | 4 +- composer.lock | 12 +-- config/services.php | 7 ++ .../2014_10_12_000000_create_users_table.php | 2 +- resources/views/auth/login.blade.php | 10 +- routes/web.php | 62 ++++++----- .../src/EloquentOAuthServiceProvider.php | 3 +- .../eloquent-oauth/src/Authenticator.php | 55 ++++++---- .../src/EloquentIdentityStore.php | 42 ++++++-- .../eloquent-oauth/src/IdentityStore.php | 12 ++- .../eloquent-oauth/src/OAuthIdentity.php | 13 ++- .../eloquent-oauth/src/OAuthManager.php | 18 +++- .../adamwathan/eloquent-oauth/src/Session.php | 13 ++- .../eloquent-oauth/src/UserStore.php | 22 ++-- .../facebook/src/FacebookProvider.php | 1 + .../socialnorm/google/src/GoogleProvider.php | 4 +- .../socialnorm/socialnorm/src/Provider.php | 4 +- .../socialnorm/src/ProviderRegistry.php | 7 +- .../src/{User.php => ProviderUser.php} | 8 +- .../src/Providers/OAuth2Provider.php | 10 +- .../socialnorm/socialnorm/src/SocialNorm.php | 12 ++- .../socialnorm/src/StateGenerator.php | 2 +- sideload/socialnorm/stackoverflow/.gitignore | 4 + sideload/socialnorm/stackoverflow/.travis.yml | 11 ++ .../socialnorm/stackoverflow/composer.json | 29 +++++ sideload/socialnorm/stackoverflow/phpunit.xml | 18 ++++ sideload/socialnorm/stackoverflow/readme.md | 3 + .../src/StackOverflowProvider.php | 100 ++++++++++++++++++ .../socialnorm/stackoverflow/tests/.gitkeep | 0 .../tests/GoogleProviderTest.php | 75 +++++++++++++ .../stackoverflow/tests/TestCase.php | 12 +++ .../tests/_fixtures/google_accesstoken.php | 24 +++++ .../tests/_fixtures/google_user.php | 23 ++++ 41 files changed, 513 insertions(+), 346 deletions(-) delete mode 100644 app/SocialLogin/Authenticator.php delete mode 100644 app/SocialLogin/EloquentIdentityStore.php delete mode 100644 app/SocialLogin/Facades/OAuth.php delete mode 100644 app/SocialLogin/IdentityStore.php delete mode 100644 app/SocialLogin/OAuthIdentity.php delete mode 100644 app/SocialLogin/OAuthManager.php delete mode 100644 app/SocialLogin/Session.php delete mode 100644 app/SocialLogin/UserStore.php rename sideload/socialnorm/socialnorm/src/{User.php => ProviderUser.php} (88%) create mode 100644 sideload/socialnorm/stackoverflow/.gitignore create mode 100644 sideload/socialnorm/stackoverflow/.travis.yml create mode 100644 sideload/socialnorm/stackoverflow/composer.json create mode 100644 sideload/socialnorm/stackoverflow/phpunit.xml create mode 100644 sideload/socialnorm/stackoverflow/readme.md create mode 100644 sideload/socialnorm/stackoverflow/src/StackOverflowProvider.php create mode 100644 sideload/socialnorm/stackoverflow/tests/.gitkeep create mode 100644 sideload/socialnorm/stackoverflow/tests/GoogleProviderTest.php create mode 100644 sideload/socialnorm/stackoverflow/tests/TestCase.php create mode 100644 sideload/socialnorm/stackoverflow/tests/_fixtures/google_accesstoken.php create mode 100644 sideload/socialnorm/stackoverflow/tests/_fixtures/google_user.php diff --git a/app/SocialLogin/Authenticator.php b/app/SocialLogin/Authenticator.php deleted file mode 100644 index df06677..0000000 --- a/app/SocialLogin/Authenticator.php +++ /dev/null @@ -1,76 +0,0 @@ -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); - } -} diff --git a/app/SocialLogin/EloquentIdentityStore.php b/app/SocialLogin/EloquentIdentityStore.php deleted file mode 100644 index abca964..0000000 --- a/app/SocialLogin/EloquentIdentityStore.php +++ /dev/null @@ -1,28 +0,0 @@ -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); - } -} diff --git a/app/SocialLogin/Facades/OAuth.php b/app/SocialLogin/Facades/OAuth.php deleted file mode 100644 index 4496850..0000000 --- a/app/SocialLogin/Facades/OAuth.php +++ /dev/null @@ -1,11 +0,0 @@ -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); - } -} diff --git a/app/SocialLogin/Session.php b/app/SocialLogin/Session.php deleted file mode 100644 index 5264629..0000000 --- a/app/SocialLogin/Session.php +++ /dev/null @@ -1,23 +0,0 @@ -store = $store; - } - - public function get($key) - { - return $this->store->get($key); - } - - public function put($key, $value) - { - return $this->store->put($key, $value); - } -} diff --git a/app/SocialLogin/UserStore.php b/app/SocialLogin/UserStore.php deleted file mode 100644 index fd6e9d5..0000000 --- a/app/SocialLogin/UserStore.php +++ /dev/null @@ -1,27 +0,0 @@ -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(); - } -} diff --git a/composer.json b/composer.json index 09a28ce..741a0ec 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "laravel/framework": "5.6.*", "laravel/socialite": "^3.0", "laravel/tinker": "^1.0", - "phpoffice/phpspreadsheet": "^1.3" + "phpoffice/phpspreadsheet": "^1.3", + "guzzlehttp/guzzle": "^6.0" }, "require-dev": { "filp/whoops": "^2.0", @@ -37,6 +38,7 @@ "SocialNorm\\Facebook\\": "sideload/socialnorm/facebook/src", "SocialNorm\\GitHub\\": "sideload/socialnorm/github/src", "SocialNorm\\Google\\": "sideload/socialnorm/google/src", + "SocialNorm\\StackOverflow\\": "sideload/socialnorm/stackoverflow/src", "SocialNorm\\": "sideload/socialnorm/socialnorm/src" }, "files": [ diff --git a/composer.lock b/composer.lock index cb08add..2f930d9 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "44870bae9f6db81ba961bf49ae2f64ad", + "content-hash": "cdb2e12195da014433178600b1a1899f", "packages": [ { "name": "barryvdh/laravel-ide-helper", @@ -1115,16 +1115,16 @@ }, { "name": "laravel/framework", - "version": "v5.6.26", + "version": "v5.6.27", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "7047df295e77cecb6a2f84736a732af66cc6789c" + "reference": "2fe661f2444410a576aa40054ad9b7fe0bb5cee5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/7047df295e77cecb6a2f84736a732af66cc6789c", - "reference": "7047df295e77cecb6a2f84736a732af66cc6789c", + "url": "https://api.github.com/repos/laravel/framework/zipball/2fe661f2444410a576aa40054ad9b7fe0bb5cee5", + "reference": "2fe661f2444410a576aa40054ad9b7fe0bb5cee5", "shasum": "" }, "require": { @@ -1250,7 +1250,7 @@ "framework", "laravel" ], - "time": "2018-06-20T14:21:11+00:00" + "time": "2018-07-10T13:47:01+00:00" }, { "name": "laravel/socialite", diff --git a/config/services.php b/config/services.php index 136eb40..6aa4239 100644 --- a/config/services.php +++ b/config/services.php @@ -54,5 +54,12 @@ return [ 'redirect_uri' => env('OAUTH_GITHUB_REDIRECT'), 'scope' => [], ], + 'stack' => [ + 'client_id' => env('OAUTH_STACK_ID'), + 'client_secret' => env('OAUTH_STACK_SECRET'), + 'redirect_uri' => env('OAUTH_STACK_REDIRECT'), + 'key' => env('OAUTH_STACK_KEY'), + 'scope' => [], + ], ], ]; diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 1220646..f98c88a 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -17,7 +17,7 @@ class CreateUsersTable extends Migration $table->increments('id'); $table->timestamps(); $table->string('name'); - $table->string('email')->unique(); + $table->string('email')->unique()->nullable(); $table->string('password')->nullable(); $table->rememberToken(); }); diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index 2f8282b..c266525 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -65,15 +65,21 @@