From b51aaa405694eb6ba7f442a6d8e6904c68ac325c Mon Sep 17 00:00:00 2001 From: Damien Guard Date: Tue, 8 Oct 2019 10:18:29 -0700 Subject: [PATCH 01/13] Setup the CODEOWNERS for pull request reviews --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 6a263cd8..073d5ffc 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @auth0/sdk-team-approvers +* @auth0/dx-sdk-approver From 46599e5dbe821fb4a396161d9aa06f7d756ec0bc Mon Sep 17 00:00:00 2001 From: Damien Guard Date: Tue, 8 Oct 2019 12:08:43 -0700 Subject: [PATCH 02/13] Setup the CODEOWNERS for pull request reviews --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 073d5ffc..c9ff4921 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @auth0/dx-sdk-approver +* @auth0/dx-sdks-approver From b469e2abb9c7a5a15b08c640f020fc1f7a5366d0 Mon Sep 17 00:00:00 2001 From: Nico Stapelbroek Date: Thu, 10 Oct 2019 21:30:33 +0200 Subject: [PATCH 03/13] Bind SessionState handler interface in contianer Binding the interface allows for an easier replacement with a different implmentation lateron. --- src/Auth0/Login/Auth0Service.php | 10 ++++++---- src/Auth0/Login/LoginServiceProvider.php | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Auth0/Login/Auth0Service.php b/src/Auth0/Login/Auth0Service.php index bc8e1372..8a47ed70 100644 --- a/src/Auth0/Login/Auth0Service.php +++ b/src/Auth0/Login/Auth0Service.php @@ -3,6 +3,7 @@ namespace Auth0\Login; use Auth0\SDK\API\Helpers\State\SessionStateHandler; +use Auth0\SDK\API\Helpers\State\StateHandler; use Auth0\SDK\Auth0; use Auth0\SDK\Helpers\Cache\CacheHandler; use Auth0\SDK\JWTVerifier; @@ -31,13 +32,14 @@ class Auth0Service * * @param array $auth0Config * @param StoreInterface $sessionStorage + * @param StateHandler|null $stateHandler * * @throws \Auth0\SDK\Exception\CoreException */ public function __construct( array $auth0Config = null, StoreInterface $sessionStorage = null, - SessionStateHandler $sessionStateHandler = null + StateHandler $stateHandler = null ) { // Backwards compatible fallbacks @@ -47,12 +49,12 @@ public function __construct( if (!$sessionStorage instanceof StoreInterface) { $sessionStorage = new LaravelSessionStore(); } - if (!$sessionStateHandler instanceof SessionStateHandler) { - $sessionStateHandler = new SessionStateHandler($sessionStorage); + if (!$stateHandler instanceof StateHandler) { + $stateHandler = new SessionStateHandler($sessionStorage); } $auth0Config['store'] = $sessionStorage; - $auth0Config['state_handler'] = $sessionStateHandler; + $auth0Config['state_handler'] = $stateHandler; $this->auth0 = new Auth0($auth0Config); } diff --git a/src/Auth0/Login/LoginServiceProvider.php b/src/Auth0/Login/LoginServiceProvider.php index 47fd620f..61299634 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -5,6 +5,7 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Helpers\InformationHeaders; use Auth0\SDK\API\Helpers\State\SessionStateHandler; +use Auth0\SDK\API\Helpers\State\StateHandler; use Auth0\SDK\Store\StoreInterface; use Illuminate\Support\ServiceProvider; @@ -49,7 +50,7 @@ public function register() return new LaravelSessionStore(); }); - $this->app->bind(SessionStateHandler::class, function ($app) { + $this->app->bind(StateHandler::class, function ($app) { return new SessionStateHandler($app->make(LaravelSessionStore::class)); }); From 5b2a01101d33b27789702303351a816f07e762d0 Mon Sep 17 00:00:00 2001 From: Nico Stapelbroek Date: Tue, 15 Oct 2019 17:05:09 +0200 Subject: [PATCH 04/13] Make Auth0Service constructor arguments required Dropped BC constructor logic to stimulate moving all constructor and configuration logic to a centralized place e.g. a service provider. --- src/Auth0/Login/Auth0Service.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/Auth0/Login/Auth0Service.php b/src/Auth0/Login/Auth0Service.php index 8a47ed70..1f134ecb 100644 --- a/src/Auth0/Login/Auth0Service.php +++ b/src/Auth0/Login/Auth0Service.php @@ -2,14 +2,12 @@ namespace Auth0\Login; -use Auth0\SDK\API\Helpers\State\SessionStateHandler; use Auth0\SDK\API\Helpers\State\StateHandler; use Auth0\SDK\Auth0; use Auth0\SDK\Helpers\Cache\CacheHandler; use Auth0\SDK\JWTVerifier; use Auth0\SDK\Store\StoreInterface; use Config; -use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Http\RedirectResponse; @@ -37,22 +35,11 @@ class Auth0Service * @throws \Auth0\SDK\Exception\CoreException */ public function __construct( - array $auth0Config = null, - StoreInterface $sessionStorage = null, - StateHandler $stateHandler = null + array $auth0Config, + StoreInterface $sessionStorage, + StateHandler $stateHandler ) { - // Backwards compatible fallbacks - if (!$auth0Config instanceof Repository && !is_array($auth0Config)) { - $auth0Config = config('laravel-auth0'); - } - if (!$sessionStorage instanceof StoreInterface) { - $sessionStorage = new LaravelSessionStore(); - } - if (!$stateHandler instanceof StateHandler) { - $stateHandler = new SessionStateHandler($sessionStorage); - } - $auth0Config['store'] = $sessionStorage; $auth0Config['state_handler'] = $stateHandler; $this->auth0 = new Auth0($auth0Config); From e23b707252b4977fb2d03981d40f57b176e353dc Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Fri, 6 Dec 2019 15:59:41 -0800 Subject: [PATCH 05/13] Adding back config check for state and store --- src/Auth0/Login/Auth0Service.php | 19 +++++++++++++++---- src/Auth0/Login/LoginServiceProvider.php | 3 +-- tests/Auth0ServiceTest.php | 4 +++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Auth0/Login/Auth0Service.php b/src/Auth0/Login/Auth0Service.php index cc6f1e4f..b8046993 100644 --- a/src/Auth0/Login/Auth0Service.php +++ b/src/Auth0/Login/Auth0Service.php @@ -2,6 +2,7 @@ namespace Auth0\Login; +use Auth0\SDK\API\Helpers\State\SessionStateHandler; use Auth0\SDK\API\Helpers\State\StateHandler; use Auth0\SDK\Auth0; use Auth0\SDK\Helpers\Cache\CacheHandler; @@ -28,16 +29,26 @@ class Auth0Service * Auth0Service constructor. * * @param array $auth0Config - * @param StoreInterface $sessionStorage + * @param StoreInterface|null $store * @param StateHandler|null $stateHandler */ public function __construct( array $auth0Config, - StoreInterface $sessionStorage, - StateHandler $stateHandler + StoreInterface $store = null, + StateHandler $stateHandler = null ) { - $auth0Config['store'] = $sessionStorage; + $store = isset( $auth0Config['store'] ) ? $auth0Config['store'] : $store; + if (false !== $store && !$store instanceof StoreInterface) { + $store = new LaravelSessionStore(); + } + + $stateHandler = isset( $auth0Config['state_handler'] ) ? $auth0Config['state_handler'] : $stateHandler; + if (false !== $stateHandler && !$stateHandler instanceof StateHandler) { + $stateHandler = new SessionStateHandler($store); + } + + $auth0Config['store'] = $store; $auth0Config['state_handler'] = $stateHandler; $this->auth0 = new Auth0($auth0Config); } diff --git a/src/Auth0/Login/LoginServiceProvider.php b/src/Auth0/Login/LoginServiceProvider.php index ad2660ab..ddb0c2cb 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -6,14 +6,13 @@ use Auth0\SDK\API\Helpers\InformationHeaders; use Auth0\SDK\API\Helpers\State\StateHandler; use Auth0\SDK\API\Helpers\State\SessionStateHandler; -use Auth0\SDK\API\Helpers\State\StateHandler; use Auth0\SDK\Store\StoreInterface; use Illuminate\Support\ServiceProvider; class LoginServiceProvider extends ServiceProvider { - const SDK_VERSION = "5.3.1"; + const SDK_VERSION = "7.0.0"; /** * Bootstrap the application events. diff --git a/tests/Auth0ServiceTest.php b/tests/Auth0ServiceTest.php index 7b3a21bb..ae9b4c10 100644 --- a/tests/Auth0ServiceTest.php +++ b/tests/Auth0ServiceTest.php @@ -3,9 +3,11 @@ use Auth0\Login\Auth0Service; use Auth0\Login\Facade\Auth0 as Auth0Facade; +use Auth0\Login\LaravelSessionStore; use Auth0\Login\LoginServiceProvider as Auth0ServiceProvider; use Auth0\SDK\API\Helpers\State\DummyStateHandler; use Auth0\SDK\Store\EmptyStore; +use Auth0\SDK\Store\SessionStore; use Orchestra\Testbench\TestCase as OrchestraTestCase; use Session; @@ -27,7 +29,7 @@ public static function setUpBeforeClass() public function testThatServiceUsesSessionStoreByDefault() { Session::put('auth0__user', '__test_user__'); - $service = new Auth0Service(self::$defaultConfig); + $service = new Auth0Service(self::$defaultConfig, new LaravelSessionStore(), new DummyStateHandler()); $user = $service->getUser(); $this->assertArrayHasKey('profile', $user); From 420cd6ce945b4e93d3f935e03ae03170f23cafe3 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Wed, 22 Jan 2020 21:15:06 -0800 Subject: [PATCH 06/13] Update PHP SDK to v7.0.0 --- .phpcs.xml.dist | 2 +- composer.json | 7 +-- phpunit.xml.dist | 2 +- src/Auth0/Login/Auth0Service.php | 63 +++++++++--------------- src/Auth0/Login/LaravelCacheWrapper.php | 48 ------------------ src/Auth0/Login/LaravelSessionStore.php | 15 +++--- src/Auth0/Login/LoginServiceProvider.php | 8 +-- tests/Auth0ServiceTest.php | 43 +++++++++++----- tests/bootstrap.php | 7 +++ 9 files changed, 73 insertions(+), 122 deletions(-) delete mode 100644 src/Auth0/Login/LaravelCacheWrapper.php create mode 100644 tests/bootstrap.php diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index b53b72b5..c1aa8dda 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -20,7 +20,7 @@ PHPCompatibility sniffs to check for PHP cross-version incompatible code. https://github.com/PHPCompatibility/PHPCompatibility --> - + diff --git a/composer.json b/composer.json index 0bf45f31..cbd4c7dd 100644 --- a/composer.json +++ b/composer.json @@ -2,14 +2,15 @@ "name": "auth0/login", "description": "Laravel plugin that helps authenticate with the auth0 service", "license": "MIT", + "prefer-stable": true, "require": { - "php": ">=5.5.0", + "php": "^7.1", + "auth0/auth0-php": "^7.0", "illuminate/support": "5.* | ^6.0", - "auth0/auth0-php": "^5.6.0", "illuminate/contracts": "5.* | ^6.0" }, "require-dev": { - "phpunit/phpunit": "^4 | ^7", + "phpunit/phpunit": "^7", "squizlabs/php_codesniffer": "^3.2", "phpcompatibility/php-compatibility": "^8.1", "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 75116c9d..106b0fd6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ -make('cache.store'); } + $auth0Config['cache_handler'] = $cache; - $auth0Config['store'] = $store; - $auth0Config['state_handler'] = $stateHandler; $this->auth0 = new Auth0($auth0Config); } @@ -156,35 +160,14 @@ public function rememberUser($value = null) /** * @param $encUser + * @param array $verifierOptions * - * @return mixed + * @return object + * @throws \Auth0\SDK\Exception\InvalidTokenException */ - public function decodeJWT($encUser) + public function decodeJWT($encUser, array $verifierOptions = []) { - try { - $cache = \App::make(CacheHandler::class); - } catch (BindingResolutionException $e) { - $cache = null; - } - - $secret_base64_encoded = config('laravel-auth0.secret_base64_encoded'); - - if (is_null($secret_base64_encoded)) { - $secret_base64_encoded = true; - } - - $verifier = new JWTVerifier([ - 'valid_audiences' => [config('laravel-auth0.client_id'), config('laravel-auth0.api_identifier')], - 'supported_algs' => config('laravel-auth0.supported_algs', ['HS256']), - 'client_secret' => config('laravel-auth0.client_secret'), - 'authorized_iss' => config('laravel-auth0.authorized_issuers'), - 'secret_base64_encoded' => $secret_base64_encoded, - 'cache' => $cache, - 'guzzle_options' => config('laravel-auth0.guzzle_options'), - ]); - - $this->apiuser = $verifier->verifyAndDecode($encUser); - + $this->apiuser = (object) $this->auth0->decodeIdToken($encUser, $verifierOptions); return $this->apiuser; } diff --git a/src/Auth0/Login/LaravelCacheWrapper.php b/src/Auth0/Login/LaravelCacheWrapper.php deleted file mode 100644 index d6b03fc6..00000000 --- a/src/Auth0/Login/LaravelCacheWrapper.php +++ /dev/null @@ -1,48 +0,0 @@ -cache = $laravelCache; - } - - /** - * @param $key - * - * @return mixed - */ - public function get($key) - { - return $this->cache->get($key); - } - - /** - * @param $key - */ - public function delete($key) - { - $this->cache->forget($key); - } - - /** - * @param $key - * @param $value - */ - public function set($key, $value) - { - $this->cache->forever($key, $value); - } -} diff --git a/src/Auth0/Login/LaravelSessionStore.php b/src/Auth0/Login/LaravelSessionStore.php index 7f379a4e..9c69e5e3 100644 --- a/src/Auth0/Login/LaravelSessionStore.php +++ b/src/Auth0/Login/LaravelSessionStore.php @@ -2,7 +2,6 @@ namespace Auth0\Login; -use Session; use Auth0\SDK\Store\StoreInterface; class LaravelSessionStore implements StoreInterface @@ -12,16 +11,14 @@ class LaravelSessionStore implements StoreInterface /** * Persists $value on $_SESSION, identified by $key. * - * @see Auth0SDK\BaseAuth0 - * * @param string $key * @param mixed $value */ - public function set($key, $value) + public function set(string $key, $value) { $key_name = $this->getSessionKeyName($key); - Session::put($key_name, $value); + \session([$key_name, $value]); } /** @@ -30,11 +27,11 @@ public function set($key, $value) * * @return mixed */ - public function get($key, $default = null) + public function get(string $key, $default = null) { $key_name = $this->getSessionKeyName($key); - return Session::get($key_name, $default); + return \session($key_name, $default); } /** @@ -44,11 +41,11 @@ public function get($key, $default = null) * * @param string $key */ - public function delete($key) + public function delete(string $key) { $key_name = $this->getSessionKeyName($key); - Session::forget($key_name); + \session([$key_name, null]); } /** diff --git a/src/Auth0/Login/LoginServiceProvider.php b/src/Auth0/Login/LoginServiceProvider.php index ddb0c2cb..00167c22 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -4,8 +4,6 @@ use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Helpers\InformationHeaders; -use Auth0\SDK\API\Helpers\State\StateHandler; -use Auth0\SDK\API\Helpers\State\SessionStateHandler; use Auth0\SDK\Store\StoreInterface; use Illuminate\Support\ServiceProvider; @@ -50,16 +48,12 @@ public function register() return new LaravelSessionStore(); }); - $this->app->bind(StateHandler::class, function ($app) { - return new SessionStateHandler($app->make(LaravelSessionStore::class)); - }); - // Bind the auth0 name to a singleton instance of the Auth0 Service $this->app->singleton(Auth0Service::class, function ($app) { return new Auth0Service( $app->make('config')->get('laravel-auth0'), $app->make(StoreInterface::class), - $app->make(StateHandler::class) + $app->make('cache.store') ); }); $this->app->singleton('auth0', function () { diff --git a/tests/Auth0ServiceTest.php b/tests/Auth0ServiceTest.php index ae9b4c10..6b4d8f8e 100644 --- a/tests/Auth0ServiceTest.php +++ b/tests/Auth0ServiceTest.php @@ -3,13 +3,12 @@ use Auth0\Login\Auth0Service; use Auth0\Login\Facade\Auth0 as Auth0Facade; -use Auth0\Login\LaravelSessionStore; use Auth0\Login\LoginServiceProvider as Auth0ServiceProvider; -use Auth0\SDK\API\Helpers\State\DummyStateHandler; -use Auth0\SDK\Store\EmptyStore; +use Auth0\SDK\Exception\InvalidTokenException; use Auth0\SDK\Store\SessionStore; +use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Cache; use Orchestra\Testbench\TestCase as OrchestraTestCase; -use Session; class Auth0ServiceTest extends OrchestraTestCase { @@ -23,13 +22,19 @@ public static function setUpBeforeClass() 'client_id' => '__test_client_id__', 'client_secret' => '__test_client_secret__', 'redirect_uri' => 'https://example.com/callback', + 'transient_store' => new SessionStore(), ]; } + public function tearDown() : void + { + Cache::flush(); + } + public function testThatServiceUsesSessionStoreByDefault() { - Session::put('auth0__user', '__test_user__'); - $service = new Auth0Service(self::$defaultConfig, new LaravelSessionStore(), new DummyStateHandler()); + session(['auth0__user' => '__test_user__']); + $service = new Auth0Service(self::$defaultConfig); $user = $service->getUser(); $this->assertArrayHasKey('profile', $user); @@ -38,12 +43,9 @@ public function testThatServiceUsesSessionStoreByDefault() public function testThatServiceSetsEmptyStoreFromConfigAndConstructor() { - Session::put('auth0__user', '__test_user__'); + session(['auth0__user' => '__test_user__']); - $service = new Auth0Service(self::$defaultConfig + ['store' => false, 'state_handler' => false]); - $this->assertNull($service->getUser()); - - $service = new Auth0Service(self::$defaultConfig, new EmptyStore(), new DummyStateHandler()); + $service = new Auth0Service(self::$defaultConfig + ['store' => false]); $this->assertNull($service->getUser()); $service = new Auth0Service(self::$defaultConfig); @@ -52,11 +54,10 @@ public function testThatServiceSetsEmptyStoreFromConfigAndConstructor() public function testThatServiceLoginReturnsRedirect() { - $service = new Auth0Service(self::$defaultConfig); $redirect = $service->login(); - $this->assertInstanceOf( \Illuminate\Http\RedirectResponse::class, $redirect ); + $this->assertInstanceOf( RedirectResponse::class, $redirect ); $targetUrl = parse_url($redirect->getTargetUrl()); @@ -68,6 +69,22 @@ public function testThatServiceLoginReturnsRedirect() $this->assertContains('client_id=__test_client_id__', $targetUrlQuery); } + /** + * @throws InvalidTokenException + */ + public function testThatServiceCanUseLaravelCache() + { + $cache_key = md5('https://__invalid_domain__/.well-known/jwks.json'); + cache([$cache_key => [uniqid()]], 10); + session(['auth0__nonce' => uniqid()]); + + $service = new Auth0Service(['domain' => '__invalid_domain__'] + self::$defaultConfig); + + // Without the cache set above, would expect a cURL error for a bad domain. + $this->expectException(InvalidTokenException::class); + $service->decodeJWT(uniqid()); + } + /* * Test suite helpers */ diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..c57341d0 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,7 @@ + Date: Thu, 2 Apr 2020 21:12:20 -0700 Subject: [PATCH 07/13] Fix session management --- .DS_Store | Bin 6148 -> 0 bytes src/Auth0/Login/LaravelSessionStore.php | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 62ea156b38675e478786fce541f8a66928831c39..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK-HOvd6h70|HEj_hx+r*)ONC`=(WST9ZdX`vrEFvuakVB*pd;B#Nz$!S3b_;? z!~f?Hz4HO|O}wk;%*^Vx)eCP#+&ST#@66>B2jgt0$Nv@#W1i)W##dG7R8*#q7);Tz z-_doh#zLW@1Jj2Ork~9835Ch0ruloSklJC+G;#aGd#;XQ{EL|0>>5PQ(fhk%s9 K1f#%TRp2{j!JHTX diff --git a/src/Auth0/Login/LaravelSessionStore.php b/src/Auth0/Login/LaravelSessionStore.php index 9c69e5e3..c2eaf784 100644 --- a/src/Auth0/Login/LaravelSessionStore.php +++ b/src/Auth0/Login/LaravelSessionStore.php @@ -18,7 +18,7 @@ public function set(string $key, $value) { $key_name = $this->getSessionKeyName($key); - \session([$key_name, $value]); + \session([$key_name => $value]); } /** @@ -45,7 +45,7 @@ public function delete(string $key) { $key_name = $this->getSessionKeyName($key); - \session([$key_name, null]); + \session([$key_name => null]); } /** From 392a19256da3a14c25376bd65241fc32437db393 Mon Sep 17 00:00:00 2001 From: giannidhooge Date: Tue, 3 Mar 2020 16:31:02 +0100 Subject: [PATCH 08/13] laravel 7 support --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index cbd4c7dd..aa2aad49 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,8 @@ "require": { "php": "^7.1", "auth0/auth0-php": "^7.0", - "illuminate/support": "5.* | ^6.0", - "illuminate/contracts": "5.* | ^6.0" + "illuminate/support": "5.* | ^6.0 | ^7.0", + "illuminate/contracts": "5.* | ^6.0 | ^7.0" }, "require-dev": { "phpunit/phpunit": "^7", From 5a29f98b7fa1b84689ed6203e713418feedca163 Mon Sep 17 00:00:00 2001 From: Tamrael Date: Sat, 4 Apr 2020 01:45:18 +0200 Subject: [PATCH 09/13] implement auth0 guard (#166) Add the auth0 guard middleware --- README.md | 30 ++++++++++++++++++++++++ composer.json | 4 ++-- phpunit.xml.dist | 6 ++++- src/Auth0/Login/LoginServiceProvider.php | 12 ++++++++++ tests/Auth0ServiceTest.php | 21 ++++++++++++++++- tests/Unit/Auth0JWTUserTest.php | 2 +- tests/bootstrap.php | 7 ------ 7 files changed, 70 insertions(+), 12 deletions(-) delete mode 100644 tests/bootstrap.php diff --git a/README.md b/README.md index db218cfc..52b9bff6 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,36 @@ You can implement your own cache strategy by creating a new class that implement You can customize the way you handle the users in your application by creating your own `UserRepository`. This class should implement the `Auth0\Login\Contract\Auth0UserRepository` contract. Please see the [Custom User Handling section of the Laravel Quickstart](https://auth0.com/docs/quickstart/webapp/laravel#optional-custom-user-handling) for the latest example. +### Using auth guard + +To protect APIs using an access token generated by Auth0, there is an `auth0` API guard provided ([Laravel documentation on guards](https://laravel.com/docs/7.x/authentication#adding-custom-guards)). To use this guard, add it to `config/auth.php` with the driver `auth0`: +``` +'guards' => [ + ... + 'auth0' => [ + 'driver' => 'auth0', + 'provider' => 'auth0', + ], +], + +'providers' => [ + ... + 'auth0' => [ + 'driver' => 'auth0', + ], +], +``` + +Once that has been added, add the guard to the middleware of any API route and check authentication during the request: +``` +// get user +auth('auth0')->user(); +// check if logged in +auth('auth0')->check(); +// protect routes via middleware use +Route::group(['middleware' => 'auth:auth0'], function () {}); +``` + ## Installation Install this plugin into a new or existing project using [Composer](https://getcomposer.org/doc/00-intro.md): diff --git a/composer.json b/composer.json index aa2aad49..dd87e6ba 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,11 @@ "illuminate/contracts": "5.* | ^6.0 | ^7.0" }, "require-dev": { - "phpunit/phpunit": "^7", + "phpunit/phpunit": "^7|^8|^9", "squizlabs/php_codesniffer": "^3.2", "phpcompatibility/php-compatibility": "^8.1", "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", - "orchestra/testbench": "^3.8" + "orchestra/testbench": "^3.8|^4.0|^5.0" }, "scripts": { "test": "SHELL_INTERACTIVE=1 \"vendor/bin/phpunit\" --coverage-text ", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 106b0fd6..9228d485 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ -src/ + + + + diff --git a/src/Auth0/Login/LoginServiceProvider.php b/src/Auth0/Login/LoginServiceProvider.php index 00167c22..0af793ed 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -2,9 +2,13 @@ namespace Auth0\Login; +use Auth0\Login\Contract\Auth0UserRepository as Auth0UserRepositoryContract; +use Auth0\Login\Repository\Auth0UserRepository; use Auth0\SDK\API\Helpers\ApiClient; use Auth0\SDK\API\Helpers\InformationHeaders; use Auth0\SDK\Store\StoreInterface; +use Illuminate\Auth\RequestGuard; +use Illuminate\Http\Request; use Illuminate\Support\ServiceProvider; class LoginServiceProvider extends ServiceProvider @@ -21,6 +25,12 @@ public function boot() return $app->make(Auth0UserProvider::class); }); + \Auth::extend('auth0', function ($app, $name, $config) { + return new RequestGuard(function (Request $request, Auth0UserProvider $provider) { + return $provider->retrieveByCredentials(['api_token' => $request->bearerToken()]); + }, $app['request'], $app['auth']->createUserProvider($config['provider'])); + }); + $this->publishes([ __DIR__.'/../../config/config.php' => config_path('laravel-auth0.php'), ]); @@ -48,6 +58,8 @@ public function register() return new LaravelSessionStore(); }); + $this->app->bind(Auth0UserRepositoryContract::class, Auth0UserRepository::class); + // Bind the auth0 name to a singleton instance of the Auth0 Service $this->app->singleton(Auth0Service::class, function ($app) { return new Auth0Service( diff --git a/tests/Auth0ServiceTest.php b/tests/Auth0ServiceTest.php index 6b4d8f8e..74254aa2 100644 --- a/tests/Auth0ServiceTest.php +++ b/tests/Auth0ServiceTest.php @@ -1,6 +1,7 @@ decodeJWT(uniqid()); } + public function testThatGuardAuthenticatesUsers() + { + $this->assertTrue(\Auth('auth0')->guest()); + + $user = new Auth0JWTUser((object)['sub' => 'x']); + + \Auth('auth0')->setUser($user); + + $this->assertTrue(\Auth('auth0')->check()); + } + /* * Test suite helpers */ @@ -100,4 +112,11 @@ protected function getPackageAliases($app) 'Auth0' => Auth0Facade::class, ]; } + + protected function getEnvironmentSetUp($app) + { + $app['config']->set('auth.guards.auth0', ['driver' => 'auth0', 'provider' => 'auth0']); + $app['config']->set('auth.providers.auth0', ['driver' => 'auth0']); + $app['config']->set('laravel-auth0', self::$defaultConfig); + } } diff --git a/tests/Unit/Auth0JWTUserTest.php b/tests/Unit/Auth0JWTUserTest.php index bb6d677f..fd85df97 100644 --- a/tests/Unit/Auth0JWTUserTest.php +++ b/tests/Unit/Auth0JWTUserTest.php @@ -12,7 +12,7 @@ class Auth0JWTUserTest extends TestCase */ protected $auth0JwtUser; - public function setUp() + public function setUp(): void { parent::setUp(); $this->auth0JwtUser = new Auth0JWTUser((object)[ diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index c57341d0..00000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,7 +0,0 @@ - Date: Thu, 9 Apr 2020 07:59:19 -0700 Subject: [PATCH 10/13] feat: enforce array type in user model constructor and return type of repo --- src/Auth0/Login/Auth0JWTUser.php | 4 ++-- src/Auth0/Login/Auth0Service.php | 4 ++-- src/Auth0/Login/Auth0User.php | 4 ++-- .../Login/Contract/Auth0UserRepository.php | 18 ++++++++++-------- .../Login/Repository/Auth0UserRepository.php | 17 +++++++++-------- tests/Auth0ServiceTest.php | 2 +- tests/Unit/Auth0JWTUserTest.php | 2 +- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/Auth0/Login/Auth0JWTUser.php b/src/Auth0/Login/Auth0JWTUser.php index 7ac637e8..ce5ddb9b 100644 --- a/src/Auth0/Login/Auth0JWTUser.php +++ b/src/Auth0/Login/Auth0JWTUser.php @@ -15,9 +15,9 @@ class Auth0JWTUser implements \Illuminate\Contracts\Auth\Authenticatable * * @param $userInfo */ - public function __construct($userInfo) + public function __construct(array $userInfo) { - $this->userInfo = get_object_vars($userInfo); + $this->userInfo = $userInfo; } /** diff --git a/src/Auth0/Login/Auth0Service.php b/src/Auth0/Login/Auth0Service.php index 1c922781..2d22401e 100644 --- a/src/Auth0/Login/Auth0Service.php +++ b/src/Auth0/Login/Auth0Service.php @@ -162,12 +162,12 @@ public function rememberUser($value = null) * @param $encUser * @param array $verifierOptions * - * @return object + * @return array * @throws \Auth0\SDK\Exception\InvalidTokenException */ public function decodeJWT($encUser, array $verifierOptions = []) { - $this->apiuser = (object) $this->auth0->decodeIdToken($encUser, $verifierOptions); + $this->apiuser = $this->auth0->decodeIdToken($encUser, $verifierOptions); return $this->apiuser; } diff --git a/src/Auth0/Login/Auth0User.php b/src/Auth0/Login/Auth0User.php index 67351bfe..9420e2e6 100644 --- a/src/Auth0/Login/Auth0User.php +++ b/src/Auth0/Login/Auth0User.php @@ -14,10 +14,10 @@ class Auth0User implements \Illuminate\Contracts\Auth\Authenticatable /** * Auth0User constructor. * - * @param $userInfo + * @param array $userInfo * @param $accessToken */ - public function __construct($userInfo, $accessToken) + public function __construct(array $userInfo, $accessToken) { $this->userInfo = $userInfo; $this->accessToken = $accessToken; diff --git a/src/Auth0/Login/Contract/Auth0UserRepository.php b/src/Auth0/Login/Contract/Auth0UserRepository.php index a87fbc14..439101e0 100644 --- a/src/Auth0/Login/Contract/Auth0UserRepository.php +++ b/src/Auth0/Login/Contract/Auth0UserRepository.php @@ -2,26 +2,28 @@ namespace Auth0\Login\Contract; +use \Illuminate\Contracts\Auth\Authenticatable; + interface Auth0UserRepository { /** - * @param stdClass $jwt with the data provided in the JWT + * @param array $decodedJwt with the data provided in the JWT * - * @return \Illuminate\Contracts\Auth\Authenticatable + * @return Authenticatable */ - public function getUserByDecodedJWT($jwt); + public function getUserByDecodedJWT(array $decodedJwt) : Authenticatable; /** * @param array $userInfo representing the user profile and user accessToken * - * @return \Illuminate\Contracts\Auth\Authenticatable + * @return Authenticatable */ - public function getUserByUserInfo($userInfo); + public function getUserByUserInfo(array $userInfo) : Authenticatable; /** - * @param $identifier the user id + * @param string|int|null $identifier the user id * - * @return \Illuminate\Contracts\Auth\Authenticatable + * @return Authenticatable|null */ - public function getUserByIdentifier($identifier); + public function getUserByIdentifier($identifier) : ?Authenticatable; } diff --git a/src/Auth0/Login/Repository/Auth0UserRepository.php b/src/Auth0/Login/Repository/Auth0UserRepository.php index af64182e..dc6b8b90 100644 --- a/src/Auth0/Login/Repository/Auth0UserRepository.php +++ b/src/Auth0/Login/Repository/Auth0UserRepository.php @@ -5,17 +5,18 @@ use Auth0\Login\Auth0User; use Auth0\Login\Auth0JWTUser; use Auth0\Login\Contract\Auth0UserRepository as Auth0UserRepositoryContract; +use Illuminate\Contracts\Auth\Authenticatable; class Auth0UserRepository implements Auth0UserRepositoryContract { /** - * @param \Auth0\Login\Contract\stdClass $jwt + * @param array $decodedJwt * * @return Auth0JWTUser */ - public function getUserByDecodedJWT($jwt) + public function getUserByDecodedJWT(array $decodedJwt) : Authenticatable { - return new Auth0JWTUser($jwt); + return new Auth0JWTUser($decodedJwt); } /** @@ -23,23 +24,23 @@ public function getUserByDecodedJWT($jwt) * * @return Auth0User */ - public function getUserByUserInfo($userInfo) + public function getUserByUserInfo(array $userInfo) : Authenticatable { return new Auth0User($userInfo['profile'], $userInfo['accessToken']); } /** - * @param \Auth0\Login\Contract\the $identifier + * @param string|int|null $identifier * - * @return Auth0User|\Illuminate\Contracts\Auth\Authenticatable|null + * @return Authenticatable|null */ - public function getUserByIdentifier($identifier) + public function getUserByIdentifier($identifier) : ?Authenticatable { // Get the user info of the user logged in (probably in session) $user = \App::make('auth0')->getUser(); if ($user === null) { - return; + return null; } // Build the user diff --git a/tests/Auth0ServiceTest.php b/tests/Auth0ServiceTest.php index 74254aa2..3d2711cc 100644 --- a/tests/Auth0ServiceTest.php +++ b/tests/Auth0ServiceTest.php @@ -90,7 +90,7 @@ public function testThatGuardAuthenticatesUsers() { $this->assertTrue(\Auth('auth0')->guest()); - $user = new Auth0JWTUser((object)['sub' => 'x']); + $user = new Auth0JWTUser(['sub' => 'x']); \Auth('auth0')->setUser($user); diff --git a/tests/Unit/Auth0JWTUserTest.php b/tests/Unit/Auth0JWTUserTest.php index fd85df97..e96cea7d 100644 --- a/tests/Unit/Auth0JWTUserTest.php +++ b/tests/Unit/Auth0JWTUserTest.php @@ -15,7 +15,7 @@ class Auth0JWTUserTest extends TestCase public function setUp(): void { parent::setUp(); - $this->auth0JwtUser = new Auth0JWTUser((object)[ + $this->auth0JwtUser = new Auth0JWTUser([ "name" => "John Doe", "iss" => "http://auth0.com", "sub" => "someone@example.com", From 0de80429d3b06ea957e81a5d20ae66e3ba11ff68 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Thu, 9 Apr 2020 09:46:35 -0700 Subject: [PATCH 11/13] Merge in remaining 5.4.0 changes --- CHANGELOG.md | 12 ++++++++++++ README.md | 2 +- src/Auth0/Login/Auth0JWTUser.php | 2 +- src/Auth0/Login/Auth0User.php | 2 +- src/Auth0/Login/LoginServiceProvider.php | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bdcb5b6..cf4af8b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Change Log +## [5.4.0](https://github.com/auth0/laravel-auth0/tree/5.4.0) (2020-03-27) +[Full Changelog](https://github.com/auth0/laravel-auth0/compare/5.3.1...5.4.0) + +**Closed issues** +- Laravel 7.0 supported release. [\#171](https://github.com/auth0/laravel-auth0/issues/171) + +**Fixed** +- Fixed PHPDocs [\#170](https://github.com/auth0/laravel-auth0/pull/170) ([YAhiru](https://github.com/YAhiru)) + +**Added** +- Laravel 7 support [\#167](https://github.com/auth0/laravel-auth0/pull/167) ([giannidhooge](https://github.com/giannidhooge)) + ## [5.3.1](https://github.com/auth0/laravel-auth0/tree/5.3.1) (2019-11-14) [Full Changelog](https://github.com/auth0/laravel-auth0/compare/5.3.0...5.3.1) diff --git a/README.md b/README.md index 52b9bff6..c473e67c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This plugin helps you integrate your [Laravel](https://laravel.com/) WebApp with [Auth0](https://auth0.com/) to achieve Single Sign On with a few simple steps. -- Master targets Laravel 6 compatibility. +- Master targets compatibility with Laravel 5.7 and above. - The 3.x branch (not maintained) targets Laravel 5.2 compatibility. - The 2.x branch (not maintained) targets Laravel 5.0 and 5.1 compatibility. - If you are working with an older version (Laravel 4.x), use version 1.0.* (not maintained) diff --git a/src/Auth0/Login/Auth0JWTUser.php b/src/Auth0/Login/Auth0JWTUser.php index ce5ddb9b..013a2239 100644 --- a/src/Auth0/Login/Auth0JWTUser.php +++ b/src/Auth0/Login/Auth0JWTUser.php @@ -71,7 +71,7 @@ public function getRememberTokenName() /** * Add a generic getter to get all the properties of the userInfo. * - * @return the related value or null if it is not set + * @return mixed the related value or null if it is not set */ public function __get($name) { diff --git a/src/Auth0/Login/Auth0User.php b/src/Auth0/Login/Auth0User.php index 9420e2e6..3fe607b2 100644 --- a/src/Auth0/Login/Auth0User.php +++ b/src/Auth0/Login/Auth0User.php @@ -15,7 +15,7 @@ class Auth0User implements \Illuminate\Contracts\Auth\Authenticatable * Auth0User constructor. * * @param array $userInfo - * @param $accessToken + * @param string|null $accessToken */ public function __construct(array $userInfo, $accessToken) { diff --git a/src/Auth0/Login/LoginServiceProvider.php b/src/Auth0/Login/LoginServiceProvider.php index 0af793ed..7f7eb7d1 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -14,7 +14,7 @@ class LoginServiceProvider extends ServiceProvider { - const SDK_VERSION = "7.0.0"; + const SDK_VERSION = "5.4.0"; /** * Bootstrap the application events. From 65c78bd08b686da2fdeb93f0ad0162517347cdc9 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Thu, 9 Apr 2020 09:48:54 -0700 Subject: [PATCH 12/13] SDK version and CHANGELOG --- CHANGELOG.md | 20 ++++++++++++++++++++ src/Auth0/Login/LoginServiceProvider.php | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf4af8b8..855f1157 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,25 @@ # Change Log +## [6.0.0](https://github.com/auth0/laravel-auth0/tree/6.0.0) (2020-04-09) +[Full Changelog](https://github.com/auth0/laravel-auth0/compare/5.4.0...6.0.0) + +**This is a major release and includes breaking changes!** This release also includes a major version change for the PHP SDK that it relies on. Please see the [migration guide](https://github.com/auth0/auth0-PHP/blob/master/MIGRATE-v5-TO-v7.md) for the PHP SDK for more information. + +**Closed issues** +- auth0-PHP 7.0 - State and nonce handling [\#163](https://github.com/auth0/laravel-auth0/issues/163) +- Cannot use actingAs unit tests functionality [\#161](https://github.com/auth0/laravel-auth0/issues/161) + +**Added** +- Implement auth0 guard [\#166](https://github.com/auth0/laravel-auth0/pull/166) ([Tamrael](https://github.com/Tamrael)) + +**Changed** +- Use array for Auth0JWTUser and add repo return types [\#176](https://github.com/auth0/laravel-auth0/pull/176) ([joshcanhelp](https://github.com/joshcanhelp)) +- Update PHP SDK to v7.0.0 [\#162](https://github.com/auth0/laravel-auth0/pull/162) ([joshcanhelp](https://github.com/joshcanhelp)) +- Bind SessionState handler interface in container [\#147](https://github.com/auth0/laravel-auth0/pull/147) ([nstapelbroek](https://github.com/nstapelbroek)) + +**Fixed** +- Fix Laravel session management [\#174](https://github.com/auth0/laravel-auth0/pull/174) ([joshcanhelp](https://github.com/joshcanhelp)) + ## [5.4.0](https://github.com/auth0/laravel-auth0/tree/5.4.0) (2020-03-27) [Full Changelog](https://github.com/auth0/laravel-auth0/compare/5.3.1...5.4.0) diff --git a/src/Auth0/Login/LoginServiceProvider.php b/src/Auth0/Login/LoginServiceProvider.php index 7f7eb7d1..88d89a8f 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -14,7 +14,7 @@ class LoginServiceProvider extends ServiceProvider { - const SDK_VERSION = "5.4.0"; + const SDK_VERSION = "6.0.0"; /** * Bootstrap the application events. From 2da4a7a62e4d35e7015fca78fc70e2d2979ff288 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Thu, 9 Apr 2020 09:52:52 -0700 Subject: [PATCH 13/13] Adjust PHP SDK to current minor --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dd87e6ba..3530fb2f 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "prefer-stable": true, "require": { "php": "^7.1", - "auth0/auth0-php": "^7.0", + "auth0/auth0-php": "^7.1", "illuminate/support": "5.* | ^6.0 | ^7.0", "illuminate/contracts": "5.* | ^6.0 | ^7.0" },