diff --git a/.circleci/config.yml b/.circleci/config.yml index 7df6fb9f..7dcd6264 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -20,12 +20,18 @@ commands: paths: - vendor -jobs: - php_7: - docker: +jobs: + php_7: + docker: - image: circleci/php:7.1 steps: - prepare + - run: + name: Check PHP Compatibility + command: composer phpcs + - run: + name: Run Tests + command: composer test snyk: docker: - image: snyk/snyk-cli:composer @@ -48,6 +54,6 @@ workflows: - php_7 - snyk: # Must define SNYK_TOKEN env - context: snyk-env - requires: - - php_7 \ No newline at end of file + context: snyk-env + requires: + - php_7 diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist new file mode 100644 index 00000000..b53b72b5 --- /dev/null +++ b/.phpcs.xml.dist @@ -0,0 +1,26 @@ + + + A custom coding standard for the Laravel Auth0 package + + ./src + + + + + + + + + + + + + + + + + + diff --git a/composer.json b/composer.json index 9840748a..0bf45f31 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,16 @@ "illuminate/contracts": "5.* | ^6.0" }, "require-dev": { - "phpunit/phpunit": "^4 | ^7" + "phpunit/phpunit": "^4 | ^7", + "squizlabs/php_codesniffer": "^3.2", + "phpcompatibility/php-compatibility": "^8.1", + "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "orchestra/testbench": "^3.8" + }, + "scripts": { + "test": "SHELL_INTERACTIVE=1 \"vendor/bin/phpunit\" --coverage-text ", + "phpcs": "\"vendor/bin/phpcs\"", + "sniffs": "\"vendor/bin/phpcs\" -e" }, "autoload": { "classmap": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a715a73b..75116c9d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,11 +19,4 @@ src/ - - - - - - - diff --git a/src/Auth0/Login/Auth0Service.php b/src/Auth0/Login/Auth0Service.php index bc8e1372..d72d226f 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\StateHandler; use Auth0\SDK\API\Helpers\State\SessionStateHandler; use Auth0\SDK\Auth0; use Auth0\SDK\Helpers\Cache\CacheHandler; @@ -36,23 +37,26 @@ class Auth0Service */ public function __construct( array $auth0Config = null, - StoreInterface $sessionStorage = null, - SessionStateHandler $sessionStateHandler = null + StoreInterface $store = null, + StateHandler $stateHandler = null ) { - // Backwards compatible fallbacks if (!$auth0Config instanceof Repository && !is_array($auth0Config)) { $auth0Config = config('laravel-auth0'); } - if (!$sessionStorage instanceof StoreInterface) { - $sessionStorage = new LaravelSessionStore(); + + $store = isset( $auth0Config['store'] ) ? $auth0Config['store'] : $store; + if (false !== $store && !$store instanceof StoreInterface) { + $store = new LaravelSessionStore(); } - if (!$sessionStateHandler instanceof SessionStateHandler) { - $sessionStateHandler = new SessionStateHandler($sessionStorage); + + $stateHandler = isset( $auth0Config['state_handler'] ) ? $auth0Config['state_handler'] : $stateHandler; + if (false !== $stateHandler && !$stateHandler instanceof StateHandler) { + $stateHandler = new SessionStateHandler($store); } - $auth0Config['store'] = $sessionStorage; - $auth0Config['state_handler'] = $sessionStateHandler; + $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 47fd620f..7187e9da 100644 --- a/src/Auth0/Login/LoginServiceProvider.php +++ b/src/Auth0/Login/LoginServiceProvider.php @@ -4,6 +4,7 @@ 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; @@ -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)); }); @@ -58,7 +59,7 @@ public function register() return new Auth0Service( $app->make('config')->get('laravel-auth0'), $app->make(StoreInterface::class), - $app->make(SessionStateHandler::class) + $app->make(StateHandler::class) ); }); $this->app->singleton('auth0', function () { diff --git a/tests/Auth0ServiceTest.php b/tests/Auth0ServiceTest.php new file mode 100644 index 00000000..7b3a21bb --- /dev/null +++ b/tests/Auth0ServiceTest.php @@ -0,0 +1,84 @@ + 'test.auth0.com', + 'client_id' => '__test_client_id__', + 'client_secret' => '__test_client_secret__', + 'redirect_uri' => 'https://example.com/callback', + ]; + } + + public function testThatServiceUsesSessionStoreByDefault() + { + Session::put('auth0__user', '__test_user__'); + $service = new Auth0Service(self::$defaultConfig); + $user = $service->getUser(); + + $this->assertArrayHasKey('profile', $user); + $this->assertEquals('__test_user__', $user['profile']); + } + + public function testThatServiceSetsEmptyStoreFromConfigAndConstructor() + { + Session::put('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()); + $this->assertNull($service->getUser()); + + $service = new Auth0Service(self::$defaultConfig); + $this->assertIsArray($service->getUser()); + } + + public function testThatServiceLoginReturnsRedirect() + { + + $service = new Auth0Service(self::$defaultConfig); + $redirect = $service->login(); + + $this->assertInstanceOf( \Illuminate\Http\RedirectResponse::class, $redirect ); + + $targetUrl = parse_url($redirect->getTargetUrl()); + + $this->assertEquals('test.auth0.com', $targetUrl['host']); + + $targetUrlQuery = explode('&', $targetUrl['query']); + + $this->assertContains('redirect_uri=https%3A%2F%2Fexample.com%2Fcallback', $targetUrlQuery); + $this->assertContains('client_id=__test_client_id__', $targetUrlQuery); + } + + /* + * Test suite helpers + */ + + protected function getPackageProviders($app) + { + return [Auth0ServiceProvider::class]; + } + + protected function getPackageAliases($app) + { + return [ + 'Auth0' => Auth0Facade::class, + ]; + } +}