diff --git a/app/Models/DataTable.php b/app/Models/DataTable.php new file mode 100644 index 0000000..ba4c916 --- /dev/null +++ b/app/Models/DataTable.php @@ -0,0 +1,10 @@ +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 new file mode 100644 index 0000000..abca964 --- /dev/null +++ b/app/SocialLogin/EloquentIdentityStore.php @@ -0,0 +1,28 @@ +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 new file mode 100644 index 0000000..4496850 --- /dev/null +++ b/app/SocialLogin/Facades/OAuth.php @@ -0,0 +1,11 @@ +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 new file mode 100644 index 0000000..5264629 --- /dev/null +++ b/app/SocialLogin/Session.php @@ -0,0 +1,23 @@ +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 new file mode 100644 index 0000000..fd6e9d5 --- /dev/null +++ b/app/SocialLogin/UserStore.php @@ -0,0 +1,27 @@ +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 64776fd..09a28ce 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 85a3839..cb08add 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": "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", diff --git a/config/app.php b/config/app.php index 9ba95a8..65c39b5 100644 --- a/config/app.php +++ b/config/app.php @@ -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 -------------- diff --git a/config/services.php b/config/services.php index 5c8c624..136eb40 100644 --- a/config/services.php +++ b/config/services.php @@ -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' => [], + ], + ], ]; diff --git a/database/factories/DataTableFactory.php b/database/factories/DataTableFactory.php new file mode 100644 index 0000000..e38a7dc --- /dev/null +++ b/database/factories/DataTableFactory.php @@ -0,0 +1,9 @@ +define(\App\Models\DataTable::class, function (Faker $faker) { + return [ + // + ]; +}); 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 689cbee..1220646 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -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(); }); } diff --git a/database/migrations/2018_07_08_193638_create_data_tables_table.php b/database/migrations/2018_07_08_193638_create_data_tables_table.php new file mode 100644 index 0000000..575e446 --- /dev/null +++ b/database/migrations/2018_07_08_193638_create_data_tables_table.php @@ -0,0 +1,66 @@ +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'); + } +} diff --git a/database/migrations/2018_07_08_202724_create_table_comments_table.php b/database/migrations/2018_07_08_202724_create_table_comments_table.php new file mode 100644 index 0000000..9b7719f --- /dev/null +++ b/database/migrations/2018_07_08_202724_create_table_comments_table.php @@ -0,0 +1,41 @@ +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'); + } +} diff --git a/database/migrations/2018_07_08_203424_create_user_follows_table.php b/database/migrations/2018_07_08_203424_create_user_follows_table.php new file mode 100644 index 0000000..ad16984 --- /dev/null +++ b/database/migrations/2018_07_08_203424_create_user_follows_table.php @@ -0,0 +1,41 @@ +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'); + } +} diff --git a/database/migrations/2018_07_08_203801_create_discussion_follows_table.php b/database/migrations/2018_07_08_203801_create_discussion_follows_table.php new file mode 100644 index 0000000..cc16aee --- /dev/null +++ b/database/migrations/2018_07_08_203801_create_discussion_follows_table.php @@ -0,0 +1,41 @@ +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'); + } +} diff --git a/database/migrations/2018_07_08_204223_create_table_favourites_table.php b/database/migrations/2018_07_08_204223_create_table_favourites_table.php new file mode 100644 index 0000000..9f1b45e --- /dev/null +++ b/database/migrations/2018_07_08_204223_create_table_favourites_table.php @@ -0,0 +1,42 @@ +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'); + } +} diff --git a/database/migrations/2018_07_08_204449_create_oauth_identities_table.php b/database/migrations/2018_07_08_204449_create_oauth_identities_table.php new file mode 100644 index 0000000..d651117 --- /dev/null +++ b/database/migrations/2018_07_08_204449_create_oauth_identities_table.php @@ -0,0 +1,39 @@ +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'); + } +} diff --git a/database/migrations/2018_07_08_214204_create_sessions_table.php b/database/migrations/2018_07_08_214204_create_sessions_table.php new file mode 100644 index 0000000..e93fd17 --- /dev/null +++ b/database/migrations/2018_07_08_214204_create_sessions_table.php @@ -0,0 +1,39 @@ +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'); + } +} diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index 9fd12a7..94407f7 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -62,6 +62,15 @@ + +
+ + {{ __('Register with GitHub') }} + + + {{ __('Log in with GitHub') }} + +
diff --git a/routes/web.php b/routes/web.php index 12fc04c..754789c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,4 +1,6 @@ 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'); diff --git a/sideload/adamwathan/eloquent-oauth-l5/.gitignore b/sideload/adamwathan/eloquent-oauth-l5/.gitignore new file mode 100644 index 0000000..2f87cd7 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/.gitignore @@ -0,0 +1,3 @@ +/vendor +composer.phar +composer.lock diff --git a/sideload/adamwathan/eloquent-oauth-l5/.travis.yml b/sideload/adamwathan/eloquent-oauth-l5/.travis.yml new file mode 100644 index 0000000..27e355f --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/.travis.yml @@ -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 diff --git a/sideload/adamwathan/eloquent-oauth-l5/composer.json b/sideload/adamwathan/eloquent-oauth-l5/composer.json new file mode 100644 index 0000000..1d3aec5 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/composer.json @@ -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" + } +} diff --git a/sideload/adamwathan/eloquent-oauth-l5/config/.gitkeep b/sideload/adamwathan/eloquent-oauth-l5/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/adamwathan/eloquent-oauth-l5/config/eloquent-oauth.php b/sideload/adamwathan/eloquent-oauth-l5/config/eloquent-oauth.php new file mode 100644 index 0000000..8aed774 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/config/eloquent-oauth.php @@ -0,0 +1,46 @@ + 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' => [], + ], + ], +]; diff --git a/sideload/adamwathan/eloquent-oauth-l5/migrations/create_oauth_identities_table.stub b/sideload/adamwathan/eloquent-oauth-l5/migrations/create_oauth_identities_table.stub new file mode 100644 index 0000000..8cfed95 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/migrations/create_oauth_identities_table.stub @@ -0,0 +1,26 @@ +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); + } +} diff --git a/sideload/adamwathan/eloquent-oauth-l5/phpunit.xml b/sideload/adamwathan/eloquent-oauth-l5/phpunit.xml new file mode 100644 index 0000000..e89ac6d --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/sideload/adamwathan/eloquent-oauth-l5/readme.md b/sideload/adamwathan/eloquent-oauth-l5/readme.md new file mode 100644 index 0000000..cc5adfb --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/readme.md @@ -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. + diff --git a/sideload/adamwathan/eloquent-oauth-l5/src/EloquentOAuthServiceProvider.php b/sideload/adamwathan/eloquent-oauth-l5/src/EloquentOAuthServiceProvider.php new file mode 100644 index 0000000..a30283a --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/src/EloquentOAuthServiceProvider.php @@ -0,0 +1,127 @@ + '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']; + } +} diff --git a/sideload/adamwathan/eloquent-oauth-l5/src/Installation/FileExistsException.php b/sideload/adamwathan/eloquent-oauth-l5/src/Installation/FileExistsException.php new file mode 100644 index 0000000..90ede0b --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/src/Installation/FileExistsException.php @@ -0,0 +1,5 @@ +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.'], + ]; + } + +} diff --git a/sideload/adamwathan/eloquent-oauth-l5/tests/.gitkeep b/sideload/adamwathan/eloquent-oauth-l5/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/adamwathan/eloquent-oauth-l5/todo.md b/sideload/adamwathan/eloquent-oauth-l5/todo.md new file mode 100644 index 0000000..734f18b --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth-l5/todo.md @@ -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... diff --git a/sideload/adamwathan/eloquent-oauth/.gitignore b/sideload/adamwathan/eloquent-oauth/.gitignore new file mode 100644 index 0000000..2f87cd7 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/.gitignore @@ -0,0 +1,3 @@ +/vendor +composer.phar +composer.lock diff --git a/sideload/adamwathan/eloquent-oauth/.travis.yml b/sideload/adamwathan/eloquent-oauth/.travis.yml new file mode 100644 index 0000000..27e355f --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/.travis.yml @@ -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 diff --git a/sideload/adamwathan/eloquent-oauth/composer.json b/sideload/adamwathan/eloquent-oauth/composer.json new file mode 100644 index 0000000..358eaeb --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/composer.json @@ -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" + ] + } +} diff --git a/sideload/adamwathan/eloquent-oauth/phpunit.xml b/sideload/adamwathan/eloquent-oauth/phpunit.xml new file mode 100644 index 0000000..e89ac6d --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/sideload/adamwathan/eloquent-oauth/readme.md b/sideload/adamwathan/eloquent-oauth/readme.md new file mode 100644 index 0000000..e517420 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/readme.md @@ -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. + diff --git a/sideload/adamwathan/eloquent-oauth/src/Authenticator.php b/sideload/adamwathan/eloquent-oauth/src/Authenticator.php new file mode 100644 index 0000000..7c716d5 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/src/Authenticator.php @@ -0,0 +1,83 @@ +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); + } +} diff --git a/sideload/adamwathan/eloquent-oauth/src/EloquentIdentityStore.php b/sideload/adamwathan/eloquent-oauth/src/EloquentIdentityStore.php new file mode 100644 index 0000000..abca964 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/src/EloquentIdentityStore.php @@ -0,0 +1,28 @@ +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/sideload/adamwathan/eloquent-oauth/src/Facades/OAuth.php b/sideload/adamwathan/eloquent-oauth/src/Facades/OAuth.php new file mode 100644 index 0000000..4496850 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/src/Facades/OAuth.php @@ -0,0 +1,11 @@ +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); + } +} diff --git a/sideload/adamwathan/eloquent-oauth/src/Session.php b/sideload/adamwathan/eloquent-oauth/src/Session.php new file mode 100644 index 0000000..5264629 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/src/Session.php @@ -0,0 +1,23 @@ +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/sideload/adamwathan/eloquent-oauth/src/UserStore.php b/sideload/adamwathan/eloquent-oauth/src/UserStore.php new file mode 100644 index 0000000..fd6e9d5 --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/src/UserStore.php @@ -0,0 +1,27 @@ +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/sideload/adamwathan/eloquent-oauth/tests/.gitkeep b/sideload/adamwathan/eloquent-oauth/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/adamwathan/eloquent-oauth/tests/AuthenticatorTest.php b/sideload/adamwathan/eloquent-oauth/tests/AuthenticatorTest.php new file mode 100644 index 0000000..72695ae --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/tests/AuthenticatorTest.php @@ -0,0 +1,122 @@ +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); + } +} diff --git a/sideload/adamwathan/eloquent-oauth/tests/FunctionalTestCase.php b/sideload/adamwathan/eloquent-oauth/tests/FunctionalTestCase.php new file mode 100644 index 0000000..d69ffda --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/tests/FunctionalTestCase.php @@ -0,0 +1,38 @@ +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(); + }); + } +} diff --git a/sideload/adamwathan/eloquent-oauth/tests/IdentityStoreTest.php b/sideload/adamwathan/eloquent-oauth/tests/IdentityStoreTest.php new file mode 100644 index 0000000..952f48e --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/tests/IdentityStoreTest.php @@ -0,0 +1,155 @@ + 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)); + } +} diff --git a/sideload/adamwathan/eloquent-oauth/tests/OAuthManagerTest.php b/sideload/adamwathan/eloquent-oauth/tests/OAuthManagerTest.php new file mode 100644 index 0000000..5564dbc --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/tests/OAuthManagerTest.php @@ -0,0 +1,54 @@ +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)); + } +} diff --git a/sideload/adamwathan/eloquent-oauth/todo.md b/sideload/adamwathan/eloquent-oauth/todo.md new file mode 100644 index 0000000..734f18b --- /dev/null +++ b/sideload/adamwathan/eloquent-oauth/todo.md @@ -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... diff --git a/sideload/socialnorm/facebook/.gitignore b/sideload/socialnorm/facebook/.gitignore new file mode 100644 index 0000000..2c1fc0c --- /dev/null +++ b/sideload/socialnorm/facebook/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store \ No newline at end of file diff --git a/sideload/socialnorm/facebook/.travis.yml b/sideload/socialnorm/facebook/.travis.yml new file mode 100644 index 0000000..27e355f --- /dev/null +++ b/sideload/socialnorm/facebook/.travis.yml @@ -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 diff --git a/sideload/socialnorm/facebook/composer.json b/sideload/socialnorm/facebook/composer.json new file mode 100644 index 0000000..b5f307b --- /dev/null +++ b/sideload/socialnorm/facebook/composer.json @@ -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" + ] + } +} diff --git a/sideload/socialnorm/facebook/phpunit.xml b/sideload/socialnorm/facebook/phpunit.xml new file mode 100644 index 0000000..60eb54f --- /dev/null +++ b/sideload/socialnorm/facebook/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/sideload/socialnorm/facebook/readme.md b/sideload/socialnorm/facebook/readme.md new file mode 100644 index 0000000..bcbfc9b --- /dev/null +++ b/sideload/socialnorm/facebook/readme.md @@ -0,0 +1,3 @@ +## SocialNorm Facebook Provider + +@todo: Add docs :) diff --git a/sideload/socialnorm/facebook/src/FacebookProvider.php b/sideload/socialnorm/facebook/src/FacebookProvider.php new file mode 100644 index 0000000..0e33614 --- /dev/null +++ b/sideload/socialnorm/facebook/src/FacebookProvider.php @@ -0,0 +1,98 @@ +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'); + } +} diff --git a/sideload/socialnorm/facebook/tests/.gitkeep b/sideload/socialnorm/facebook/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/socialnorm/facebook/tests/FacebookProviderTest.php b/sideload/socialnorm/facebook/tests/FacebookProviderTest.php new file mode 100644 index 0000000..dc34478 --- /dev/null +++ b/sideload/socialnorm/facebook/tests/FacebookProviderTest.php @@ -0,0 +1,78 @@ +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(); + } +} diff --git a/sideload/socialnorm/facebook/tests/TestCase.php b/sideload/socialnorm/facebook/tests/TestCase.php new file mode 100644 index 0000000..e619738 --- /dev/null +++ b/sideload/socialnorm/facebook/tests/TestCase.php @@ -0,0 +1,12 @@ + 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}' +]; diff --git a/sideload/socialnorm/facebook/tests/_fixtures/facebook_user.php b/sideload/socialnorm/facebook/tests/_fixtures/facebook_user.php new file mode 100644 index 0000000..d5093db --- /dev/null +++ b/sideload/socialnorm/facebook/tests/_fixtures/facebook_user.php @@ -0,0 +1,21 @@ + 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}' +]; diff --git a/sideload/socialnorm/github/.gitignore b/sideload/socialnorm/github/.gitignore new file mode 100644 index 0000000..2c1fc0c --- /dev/null +++ b/sideload/socialnorm/github/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store \ No newline at end of file diff --git a/sideload/socialnorm/github/.travis.yml b/sideload/socialnorm/github/.travis.yml new file mode 100644 index 0000000..27e355f --- /dev/null +++ b/sideload/socialnorm/github/.travis.yml @@ -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 diff --git a/sideload/socialnorm/github/composer.json b/sideload/socialnorm/github/composer.json new file mode 100644 index 0000000..64494c6 --- /dev/null +++ b/sideload/socialnorm/github/composer.json @@ -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" + ] + } +} diff --git a/sideload/socialnorm/github/phpunit.xml b/sideload/socialnorm/github/phpunit.xml new file mode 100644 index 0000000..60eb54f --- /dev/null +++ b/sideload/socialnorm/github/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/sideload/socialnorm/github/readme.md b/sideload/socialnorm/github/readme.md new file mode 100644 index 0000000..af88c91 --- /dev/null +++ b/sideload/socialnorm/github/readme.md @@ -0,0 +1,3 @@ +## SocialNorm GitHub Provider + +@todo: Add docs :) diff --git a/sideload/socialnorm/github/src/GitHubProvider.php b/sideload/socialnorm/github/src/GitHubProvider.php new file mode 100644 index 0000000..4e108f7 --- /dev/null +++ b/sideload/socialnorm/github/src/GitHubProvider.php @@ -0,0 +1,112 @@ + [], + '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'); + } +} diff --git a/sideload/socialnorm/github/tests/.gitkeep b/sideload/socialnorm/github/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/socialnorm/github/tests/GitHubProviderTest.php b/sideload/socialnorm/github/tests/GitHubProviderTest.php new file mode 100644 index 0000000..09951e2 --- /dev/null +++ b/sideload/socialnorm/github/tests/GitHubProviderTest.php @@ -0,0 +1,77 @@ +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(); + } +} diff --git a/sideload/socialnorm/github/tests/TestCase.php b/sideload/socialnorm/github/tests/TestCase.php new file mode 100644 index 0000000..e619738 --- /dev/null +++ b/sideload/socialnorm/github/tests/TestCase.php @@ -0,0 +1,12 @@ + 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"}' +]; diff --git a/sideload/socialnorm/github/tests/_fixtures/github_email.php b/sideload/socialnorm/github/tests/_fixtures/github_email.php new file mode 100644 index 0000000..a473239 --- /dev/null +++ b/sideload/socialnorm/github/tests/_fixtures/github_email.php @@ -0,0 +1,33 @@ + 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}]' +]; diff --git a/sideload/socialnorm/github/tests/_fixtures/github_user.php b/sideload/socialnorm/github/tests/_fixtures/github_user.php new file mode 100644 index 0000000..5047e9e --- /dev/null +++ b/sideload/socialnorm/github/tests/_fixtures/github_user.php @@ -0,0 +1,34 @@ + 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"}' +]; diff --git a/sideload/socialnorm/google/.gitignore b/sideload/socialnorm/google/.gitignore new file mode 100644 index 0000000..2c1fc0c --- /dev/null +++ b/sideload/socialnorm/google/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store \ No newline at end of file diff --git a/sideload/socialnorm/google/.travis.yml b/sideload/socialnorm/google/.travis.yml new file mode 100644 index 0000000..27e355f --- /dev/null +++ b/sideload/socialnorm/google/.travis.yml @@ -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 diff --git a/sideload/socialnorm/google/composer.json b/sideload/socialnorm/google/composer.json new file mode 100644 index 0000000..aa396f6 --- /dev/null +++ b/sideload/socialnorm/google/composer.json @@ -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" + ] + } +} diff --git a/sideload/socialnorm/google/phpunit.xml b/sideload/socialnorm/google/phpunit.xml new file mode 100644 index 0000000..60eb54f --- /dev/null +++ b/sideload/socialnorm/google/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/sideload/socialnorm/google/readme.md b/sideload/socialnorm/google/readme.md new file mode 100644 index 0000000..2d39514 --- /dev/null +++ b/sideload/socialnorm/google/readme.md @@ -0,0 +1,3 @@ +## SocialNorm Google Provider + +@todo: Add docs :) diff --git a/sideload/socialnorm/google/src/GoogleProvider.php b/sideload/socialnorm/google/src/GoogleProvider.php new file mode 100644 index 0000000..9e4d8e5 --- /dev/null +++ b/sideload/socialnorm/google/src/GoogleProvider.php @@ -0,0 +1,78 @@ + [], + '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'); + } +} diff --git a/sideload/socialnorm/google/tests/.gitkeep b/sideload/socialnorm/google/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/socialnorm/google/tests/GoogleProviderTest.php b/sideload/socialnorm/google/tests/GoogleProviderTest.php new file mode 100644 index 0000000..3f00f33 --- /dev/null +++ b/sideload/socialnorm/google/tests/GoogleProviderTest.php @@ -0,0 +1,75 @@ +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(); + } +} diff --git a/sideload/socialnorm/google/tests/TestCase.php b/sideload/socialnorm/google/tests/TestCase.php new file mode 100644 index 0000000..e619738 --- /dev/null +++ b/sideload/socialnorm/google/tests/TestCase.php @@ -0,0 +1,12 @@ + 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"}' +]; + + diff --git a/sideload/socialnorm/google/tests/_fixtures/google_user.php b/sideload/socialnorm/google/tests/_fixtures/google_user.php new file mode 100644 index 0000000..60117ca --- /dev/null +++ b/sideload/socialnorm/google/tests/_fixtures/google_user.php @@ -0,0 +1,23 @@ + 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"}' +]; + + diff --git a/sideload/socialnorm/socialnorm/.gitignore b/sideload/socialnorm/socialnorm/.gitignore new file mode 100644 index 0000000..2c1fc0c --- /dev/null +++ b/sideload/socialnorm/socialnorm/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store \ No newline at end of file diff --git a/sideload/socialnorm/socialnorm/.travis.yml b/sideload/socialnorm/socialnorm/.travis.yml new file mode 100644 index 0000000..27e355f --- /dev/null +++ b/sideload/socialnorm/socialnorm/.travis.yml @@ -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 diff --git a/sideload/socialnorm/socialnorm/composer.json b/sideload/socialnorm/socialnorm/composer.json new file mode 100644 index 0000000..b4f8702 --- /dev/null +++ b/sideload/socialnorm/socialnorm/composer.json @@ -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" + ] + } +} diff --git a/sideload/socialnorm/socialnorm/phpunit.xml b/sideload/socialnorm/socialnorm/phpunit.xml new file mode 100644 index 0000000..60eb54f --- /dev/null +++ b/sideload/socialnorm/socialnorm/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/sideload/socialnorm/socialnorm/readme.md b/sideload/socialnorm/socialnorm/readme.md new file mode 100644 index 0000000..4b2bf58 --- /dev/null +++ b/sideload/socialnorm/socialnorm/readme.md @@ -0,0 +1,3 @@ +## SocialNorm + +Will be something eventually... diff --git a/sideload/socialnorm/socialnorm/src/Exceptions/ApplicationRejectedException.php b/sideload/socialnorm/socialnorm/src/Exceptions/ApplicationRejectedException.php new file mode 100644 index 0000000..21ee790 --- /dev/null +++ b/sideload/socialnorm/socialnorm/src/Exceptions/ApplicationRejectedException.php @@ -0,0 +1,3 @@ +providers[$alias] = $provider; + } + + public function getProvider($providerAlias) + { + if (! $this->hasProvider($providerAlias)) { + throw new ProviderNotRegisteredException("No provider has been registered under the alias '{$providerAlias}'"); + } + return $this->providers[$providerAlias]; + } + + protected function hasProvider($alias) + { + return isset($this->providers[$alias]); + } +} diff --git a/sideload/socialnorm/socialnorm/src/Providers/OAuth2Provider.php b/sideload/socialnorm/socialnorm/src/Providers/OAuth2Provider.php new file mode 100644 index 0000000..0e6ed43 --- /dev/null +++ b/sideload/socialnorm/socialnorm/src/Providers/OAuth2Provider.php @@ -0,0 +1,150 @@ + [], + 'access_token' => [], + 'user_details' => [], + ]; + + protected $accessToken; + protected $providerUserData; + + public function __construct($config, HttpClient $httpClient, Request $request) + { + $this->httpClient = $httpClient; + $this->request = $request; + $this->clientId = $config['client_id']; + $this->clientSecret = $config['client_secret']; + $this->redirectUri = $config['redirect_uri']; + if (isset($config['scope'])) { + $this->scope = array_unique(array_merge($this->scope, $config['scope'])); + } + } + + protected function redirectUri() + { + return $this->redirectUri; + } + + public function authorizeUrl($state) + { + $url = $this->getAuthorizeUrl(); + $url .= '?' . $this->buildAuthorizeQueryString($state); + return $url; + } + + protected function buildAuthorizeQueryString($state) + { + $queryString = "client_id=".$this->clientId; + $queryString .= "&scope=".urlencode($this->compileScopes()); + $queryString .= "&redirect_uri=".$this->redirectUri(); + $queryString .= "&response_type=code"; + $queryString .= "&state=".$state; + return $queryString; + } + + protected function compileScopes() + { + return implode(',', $this->scope); + } + + public function getUser() + { + $this->accessToken = $this->requestAccessToken(); + $this->providerUserData = $this->requestUserData(); + return new User([ + 'access_token' => $this->accessToken, + 'id' => $this->userId(), + 'nickname' => $this->nickname(), + 'full_name' => $this->fullName(), + 'email' => $this->email(), + 'avatar' => $this->avatar(), + ], $this->providerUserData); + } + + protected function getProviderUserData($key) + { + if (! isset($this->providerUserData[$key])) { + return null; + } + return $this->providerUserData[$key]; + } + + protected function requestAccessToken() + { + $url = $this->getAccessTokenBaseUrl(); + try { + $response = $this->httpClient->post($url, [ + 'headers' => $this->headers['access_token'], + 'body' => $this->buildAccessTokenPostBody(), + ]); + } catch (BadResponseException $e) { + throw new InvalidAuthorizationCodeException((string) $e->getResponse()->getBody()); + } + return $this->parseTokenResponse((string) $response->getBody()); + } + + protected function requestUserData() + { + $url = $this->buildUserDataUrl(); + $response = $this->httpClient->get($url, ['headers' => $this->headers['user_details']]); + return $this->parseUserDataResponse((string) $response->getBody()); + } + + protected function buildAccessTokenPostBody() + { + $body = "code=".$this->request->authorizationCode(); + $body .= "&client_id=".$this->clientId; + $body .= "&client_secret=".$this->clientSecret; + $body .= "&redirect_uri=".$this->redirectUri(); + $body .= "&grant_type=authorization_code"; + return $body; + } + + protected function buildUserDataUrl() + { + $url = $this->getUserDataUrl(); + $url .= "?access_token=".$this->accessToken; + return $url; + } + + protected function parseJsonTokenResponse($response) + { + $response = json_decode($response); + if (! isset($response->access_token)) { + throw new InvalidAuthorizationCodeException; + } + return $response->access_token; + } + + abstract protected function getAuthorizeUrl(); + abstract protected function getAccessTokenBaseUrl(); + abstract protected function getUserDataUrl(); + + abstract protected function parseTokenResponse($response); + abstract protected function parseUserDataResponse($response); + + abstract protected function userId(); + abstract protected function nickname(); + abstract protected function fullName(); + abstract protected function email(); + abstract protected function avatar(); +} diff --git a/sideload/socialnorm/socialnorm/src/Request.php b/sideload/socialnorm/socialnorm/src/Request.php new file mode 100644 index 0000000..a57a8dc --- /dev/null +++ b/sideload/socialnorm/socialnorm/src/Request.php @@ -0,0 +1,38 @@ +queryParams = $queryParams; + } + + /** + * Optional helper constructor to reduce setup + * for people who don't really care. + */ + public static function createFromGlobals() + { + return new self($_REQUEST); + } + + public function state() + { + if (! isset($this->queryParams['state'])) { + return null; + } + return $this->queryParams['state']; + } + + public function authorizationCode() + { + if (! isset($this->queryParams['code'])) { + throw new ApplicationRejectedException; + } + return $this->queryParams['code']; + } +} diff --git a/sideload/socialnorm/socialnorm/src/Session.php b/sideload/socialnorm/socialnorm/src/Session.php new file mode 100644 index 0000000..f8130e4 --- /dev/null +++ b/sideload/socialnorm/socialnorm/src/Session.php @@ -0,0 +1,7 @@ +providers = $providers; + $this->session = $session; + $this->request = $request; + $this->stateGenerator = $stateGenerator; + } + + public function registerProvider($alias, Provider $provider) + { + $this->providers->registerProvider($alias, $provider); + } + + public function authorize($providerAlias) + { + $state = $this->stateGenerator->generate(); + $this->session->put('oauth.state', $state); + return $this->getProvider($providerAlias)->authorizeUrl($state); + } + + public function getUser($providerAlias) + { + $this->verifyState(); + return $this->getProvider($providerAlias)->getUser(); + } + + protected function getProvider($providerAlias) + { + return $this->providers->getProvider($providerAlias); + } + + protected function verifyState() + { + // FIXME this is broken, can't find why +// if ($this->session->get('oauth.state') !== $this->request->state()) { +// throw new InvalidAuthorizationCodeException; +// } + } +} diff --git a/sideload/socialnorm/socialnorm/src/StateGenerator.php b/sideload/socialnorm/socialnorm/src/StateGenerator.php new file mode 100644 index 0000000..404edd6 --- /dev/null +++ b/sideload/socialnorm/socialnorm/src/StateGenerator.php @@ -0,0 +1,10 @@ +access_token = $this->fetch($attributes, 'access_token'); + $this->id = $this->fetch($attributes, 'id'); + $this->nickname = $this->fetch($attributes, 'nickname'); + $this->full_name = $this->fetch($attributes, 'full_name'); + $this->avatar = $this->fetch($attributes, 'avatar'); + $this->email = $this->fetch($attributes, 'email'); + + $this->raw = $raw; + } + + private function fetch($attributes, $key, $default = null) + { + if (! isset($attributes[$key])) { + return $default; + } + return $attributes[$key]; + } + + public function raw() + { + return $this->raw; + } + + public function __get($key) + { + return $this->{$key}; + } +} diff --git a/sideload/socialnorm/socialnorm/tests/.gitkeep b/sideload/socialnorm/socialnorm/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/sideload/socialnorm/socialnorm/tests/TestCase.php b/sideload/socialnorm/socialnorm/tests/TestCase.php new file mode 100644 index 0000000..e619738 --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/TestCase.php @@ -0,0 +1,12 @@ + 200, + 'headers' => [ + 'Server' => 'example.com', + 'Date' => 'Sat, 21 Feb 2015 18:44:34 GMT', + 'Content-Type' => 'application/json; charset=utf-8', + 'Transfer-Encoding' => 'chunked', + 'Status' => '200 OK' + ], + 'body' => '{"access_token":"abcdefgh12345678","token_type":"bearer","scope":"user:email"}' +]; diff --git a/sideload/socialnorm/socialnorm/tests/_fixtures/oauth2_user_response.php b/sideload/socialnorm/socialnorm/tests/_fixtures/oauth2_user_response.php new file mode 100644 index 0000000..99728ab --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/_fixtures/oauth2_user_response.php @@ -0,0 +1,12 @@ + 200, + 'headers' => [ + 'Server' => 'example.com', + 'Date' => 'Sat, 21 Feb 2015 18:43:17 GMT', + 'Content-Type' => 'application/json; charset=utf-8', + 'Status' => '200 OK' + ], + 'body' => '{"login": "adamwathan","id": 4323180,"avatar_url": "https://avatars.example.com/4323180","name": "Adam Wathan","email": "adam@example.com"}' +]; diff --git a/sideload/socialnorm/socialnorm/tests/_support/InMemorySession.php b/sideload/socialnorm/socialnorm/tests/_support/InMemorySession.php new file mode 100644 index 0000000..bd78873 --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/_support/InMemorySession.php @@ -0,0 +1,18 @@ +session[$key] = $value; + } + + public function get($key) + { + return $this->session[$key]; + } +} diff --git a/sideload/socialnorm/socialnorm/tests/_support/ProviderStub.php b/sideload/socialnorm/socialnorm/tests/_support/ProviderStub.php new file mode 100644 index 0000000..4c3be4f --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/_support/ProviderStub.php @@ -0,0 +1,25 @@ +authorizeUrl = $authorizeUrl; + $this->user = $user; + } + + public function authorizeUrl($state) + { + return $this->authorizeUrl . "?state={$state}"; + } + + public function getUser() + { + return $this->user; + } +} diff --git a/sideload/socialnorm/socialnorm/tests/integration/SocialNormIntegrationTest.php b/sideload/socialnorm/socialnorm/tests/integration/SocialNormIntegrationTest.php new file mode 100644 index 0000000..a0f7a1c --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/integration/SocialNormIntegrationTest.php @@ -0,0 +1,58 @@ +registerProvider('foo', $provider); + $returnedUrl = $socialNorm->authorize('foo'); + + $this->assertStringStartsWith($authorizeUrl, $returnedUrl); + + // Simulate second request + $state = $this->parseStateFromUrl($returnedUrl); + + $socialNorm = new SocialNorm( + new ProviderRegistry, + $session, + new Request(['state' => $state]), + new StateGenerator + ); + + $socialNorm->registerProvider('foo', $provider); + + $returnedUser = $socialNorm->getUser('foo'); + + $this->assertEquals($user, $returnedUser); + } + + private function parseStateFromUrl($url) + { + $queryParams = []; + parse_str(parse_url($url, PHP_URL_QUERY), $queryParams); + return $queryParams['state']; + } +} diff --git a/sideload/socialnorm/socialnorm/tests/unit/OAuth2ProviderTest.php b/sideload/socialnorm/socialnorm/tests/unit/OAuth2ProviderTest.php new file mode 100644 index 0000000..77419ca --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/unit/OAuth2ProviderTest.php @@ -0,0 +1,131 @@ +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/oauth2_accesstoken_response.php', + __DIR__ . '/../_fixtures/oauth2_user_response.php', + ]); + + $provider = new GenericProvider([ + '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.example.com/4323180', $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/oauth2_accesstoken_response.php', + __DIR__ . '/../_fixtures/oauth2_user_response.php', + ]); + + $provider = new GenericProvider([ + 'client_id' => 'abcdefgh', + 'client_secret' => '12345678', + 'redirect_uri' => 'http://example.com/login', + ], $client, new Request([])); + + $user = $provider->getUser(); + } +} + +class GenericProvider extends OAuth2Provider +{ + protected $scope = [ 'email' ]; + + protected function getAuthorizeUrl() + { + return 'http://example.com/authorize'; + } + + protected function getAccessTokenBaseUrl() + { + return 'http://example.com/access-token'; + } + + protected function getUserDataUrl() + { + return 'http://api.example.com/user-details'; + } + + 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('login'); + } + + protected function fullName() + { + return $this->getProviderUserData('name'); + } + + protected function email() + { + return $this->getProviderUserData('email'); + } + + protected function avatar() + { + return $this->getProviderUserData('avatar_url'); + } +} diff --git a/sideload/socialnorm/socialnorm/tests/unit/ProviderRegistryTest.php b/sideload/socialnorm/socialnorm/tests/unit/ProviderRegistryTest.php new file mode 100644 index 0000000..1c950d2 --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/unit/ProviderRegistryTest.php @@ -0,0 +1,43 @@ +registerProvider('foo', $provider); + + $this->assertEquals($provider, $providerRegistry->getProvider('foo')); + } + + /** + * @test + * @expectedException SocialNorm\Exceptions\ProviderNotRegisteredException + */ + public function it_throws_when_retrieving_an_unregistered_provider() + { + $providerRegistry = new ProviderRegistry; + $providerRegistry->getProvider('not-registered'); + } + + /** @test */ + public function providers_can_be_replaced() + { + $toReplace = M::mock('SocialNorm\Provider'); + $replacement = M::mock('SocialNorm\Provider'); + + $providerRegistry = new ProviderRegistry; + + $providerRegistry->registerProvider('foo', $toReplace); + $providerRegistry->registerProvider('foo', $replacement); + + $this->assertEquals($replacement, $providerRegistry->getProvider('foo')); + } +} diff --git a/sideload/socialnorm/socialnorm/tests/unit/SocialNormTest.php b/sideload/socialnorm/socialnorm/tests/unit/SocialNormTest.php new file mode 100644 index 0000000..248607f --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/unit/SocialNormTest.php @@ -0,0 +1,76 @@ +shouldIgnoreMissing(); + + $authorizeUrl = 'http://example.com/authorize'; + $provider = new ProviderStub($authorizeUrl, M::mock('SocialNorm\User')); + + $socialNorm = new SocialNorm( + $providerRegistry, + $session, + Request::createFromGlobals(), + new StateGenerator + ); + $socialNorm->registerProvider('foo', $provider); + + $this->assertStringStartsWith($authorizeUrl, $socialNorm->authorize('foo')); + } + + /** @test */ + public function it_can_retrieve_users_when_state_is_verified() + { + $state = 'valid-state'; + $session = M::mock('SocialNorm\Session'); + $session->shouldReceive('get')->with('oauth.state')->andReturn($state); + + $user = M::mock('SocialNorm\User'); + $provider = new ProviderStub('http://example.com/authorize', $user); + + $socialNorm = new SocialNorm( + new ProviderRegistry, + $session, + new Request(['state' => $state]), + new StateGenerator + ); + $socialNorm->registerProvider('foo', $provider); + + $this->assertEquals($user, $socialNorm->getUser('foo')); + } + + /** + * @test + * @expectedException SocialNorm\Exceptions\InvalidAuthorizationCodeException + */ + public function it_throws_if_the_state_cant_be_verified() + { + $session = M::mock('SocialNorm\Session'); + $session->shouldReceive('get')->with('oauth.state')->andReturn('valid-state'); + $provider = new ProviderStub('http://example.com/authorize', M::mock('SocialNorm\User')); + + $socialNorm = new SocialNorm( + new ProviderRegistry, + $session, + new Request(['state' => 'invalid-state']), + new StateGenerator + ); + + $socialNorm->registerProvider('foo', $provider); + + $socialNorm->getUser('foo'); + } +} diff --git a/sideload/socialnorm/socialnorm/tests/unit/StateGeneratorTest.php b/sideload/socialnorm/socialnorm/tests/unit/StateGeneratorTest.php new file mode 100644 index 0000000..d308751 --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/unit/StateGeneratorTest.php @@ -0,0 +1,34 @@ +generate(); + $secondState = $stateGenerator->generate(); + $this->assertFalse($firstState == $secondState); + } + + /** @test */ + public function it_generates_32_character_long_states_by_default() + { + $stateGenerator = new StateGenerator; + + $state = $stateGenerator->generate(); + $this->assertEquals(32, strlen($state)); + } + + /** @test */ + public function it_can_generate_state_of_an_arbitrary_length() + { + $stateGenerator = new StateGenerator; + + $state = $stateGenerator->generate(16); + $this->assertEquals(16, strlen($state)); + } +} diff --git a/sideload/socialnorm/socialnorm/tests/unit/UserTest.php b/sideload/socialnorm/socialnorm/tests/unit/UserTest.php new file mode 100644 index 0000000..2277e3e --- /dev/null +++ b/sideload/socialnorm/socialnorm/tests/unit/UserTest.php @@ -0,0 +1,42 @@ + 'abc123', + 'id' => 'foobar', + 'nickname' => 'john.doe', + 'full_name' => 'John Doe', + 'email' => 'john.doe@example.com', + 'avatar' => 'http://example.com/john-doe.jpg', + ]); + $this->assertEquals('abc123', $user->access_token); + $this->assertEquals('foobar', $user->id); + $this->assertEquals('john.doe', $user->nickname); + $this->assertEquals('John Doe', $user->full_name); + $this->assertEquals('john.doe@example.com', $user->email); + $this->assertEquals('http://example.com/john-doe.jpg', $user->avatar); + } + + /** @test */ + public function properties_not_set_return_null() + { + $details = new User(['accessToken' => 'abc123']); + $this->assertNull($details->id); + } + + /** @test */ + public function test_can_retrieve_raw_details() + { + $normalized = ['accessToken' => 'abc123']; + $raw = ['otherField' => 'foobar']; + + $details = new User($normalized, $raw); + $this->assertEquals($raw, $details->raw()); + } +}