From 4082a8a725f63600912968de60d5080b11f5b263 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:02:34 +0100 Subject: [PATCH 01/18] feat: add custom session guard with silent authentication methods --- src/SessionGuard.php | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/SessionGuard.php diff --git a/src/SessionGuard.php b/src/SessionGuard.php new file mode 100644 index 0000000..89d106c --- /dev/null +++ b/src/SessionGuard.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CyrildeWit\LaravelSilentAuthentication; + +use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Auth\SessionGuard as BaseSessionGuard; + +class SessionGuard extends BaseSessionGuard +{ + /** + * Log a user into the application without firing login specific events. + * + * @param \Illuminate\Contracts\Auth\Authenticatable $user + * @return void + */ + public function silentLogin(Authenticatable $user) + { + $this->updateSession($user->getAuthIdentifier()); + + $this->setUser($user); + } + + /** + * Log the user out of the application without refreshing the "remember me" + * token and firing logout specific events. + * + * @return void + */ + public function silentLogout() + { + $this->clearUserDataFromStorage(); + + $this->user = null; + + $this->loggedOut = true; + } +} From 6ab462bb2b07b12521ffb61f8fceaa51e8f955df Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:07:23 +0100 Subject: [PATCH 02/18] chore(readme): add subline --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 336bd94..412bd8e 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # Laravel Silent Authentication + +This Laravel >= 5.5 package allows you to silently authenticate users. From e1249446bf8767ceed3b388f89fff4437428b678 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:08:37 +0100 Subject: [PATCH 03/18] chore(git): add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ From b71c8213b57703cf0ddd997d7890101d0e2807ef Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:10:12 +0100 Subject: [PATCH 04/18] chore(git): add composer.lock to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 48b8bf9..d1502b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor/ +composer.lock From 6b1a377574d95a17e0a9f8ad7300ebffa51e88ee Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:31:38 +0100 Subject: [PATCH 05/18] feat: split SessionGuard into a trait and a plain class --- src/Guards/SessionGuard.php | 21 +++++++++++++++++++ .../SilentAuthentication.php} | 5 ++--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 src/Guards/SessionGuard.php rename src/{SessionGuard.php => Traits/SilentAuthentication.php} (87%) diff --git a/src/Guards/SessionGuard.php b/src/Guards/SessionGuard.php new file mode 100644 index 0000000..5ae98f6 --- /dev/null +++ b/src/Guards/SessionGuard.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CyrildeWit\LaravelSilentAuthentication\Guards; + +use Illuminate\Auth\SessionGuard as BaseSessionGuard; + +class SessionGuard extends BaseSessionGuard +{ + use SilentAuthentication; +} diff --git a/src/SessionGuard.php b/src/Traits/SilentAuthentication.php similarity index 87% rename from src/SessionGuard.php rename to src/Traits/SilentAuthentication.php index 89d106c..00b17bf 100644 --- a/src/SessionGuard.php +++ b/src/Traits/SilentAuthentication.php @@ -11,12 +11,11 @@ * file that was distributed with this source code. */ -namespace CyrildeWit\LaravelSilentAuthentication; +namespace CyrildeWit\LaravelSilentAuthentication\Traits; use Illuminate\Contracts\Auth\Authenticatable; -use Illuminate\Auth\SessionGuard as BaseSessionGuard; -class SessionGuard extends BaseSessionGuard +trait SilentAuthentication { /** * Log a user into the application without firing login specific events. From 86f90cf13c715e75b4c50561138bdc575cf4401a Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:36:31 +0100 Subject: [PATCH 06/18] feat: add config file --- config/silent-authentication.php | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 config/silent-authentication.php diff --git a/config/silent-authentication.php b/config/silent-authentication.php new file mode 100644 index 0000000..d73a97a --- /dev/null +++ b/config/silent-authentication.php @@ -0,0 +1,9 @@ + true, + + 'provider_name' => 'session', + +]; From b7e5eedb8bfac200e3ecb3170e70bbccd171584b Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:37:22 +0100 Subject: [PATCH 07/18] feat: add service provider --- src/SilentAuthenticationServiceProvider.php | 84 +++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/SilentAuthenticationServiceProvider.php diff --git a/src/SilentAuthenticationServiceProvider.php b/src/SilentAuthenticationServiceProvider.php new file mode 100644 index 0000000..7fbfa07 --- /dev/null +++ b/src/SilentAuthenticationServiceProvider.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace CyrildeWit\EloquentViewable; + +use Illuminate\Support\ServiceProvider; + +class SilentAuthenticationServiceProvider extends ServiceProvider +{ + /** + * Perform post-registration booting of services. + * + * @return void + */ + public function boot() + { + if ($this->app->runningInConsole()) { + $config = $this->app->config['silent-authentication']; + + $this->publishes([ + __DIR__.'/../config/silent-authentication.php' => $this->app->configPath('silent-authentication.php'), + ], 'config'); + } + } + + /** + * Register bindings in the container. + * + * @return void + */ + public function register() + { + $this->mergeConfigFrom( + __DIR__.'/../config/silent-authentication.php', + 'silent-authentication' + ); + + if (config('silent-authentication.enabled')) { + $this->registerSessionDriver(); + } + } + + /** + * Register the custom session driver which will + * @param void + * @return void + */ + protected function registerAuthDriver() + { + $auth = $this->app['auth']; + + $providerName = config('silent-authentication.provider_name'); + + $auth->extend($providerName, function (Application $app, $name, array $config) use ($auth) { + $provider = $auth->createUserProvider($config['provider']); + + $guard = new SessionGuard($name, $provider, $app['session.store']); + + if (method_exists($guard, 'setCookieJar')) { + $guard->setCookieJar($app['cookie']); + } + + if (method_exists($guard, 'setDispatcher')) { + $guard->setDispatcher($app['events']); + } + + if (method_exists($guard, 'setRequest')) { + $guard->setRequest($app->refresh('request', $guard, 'setRequest')); + } + + return $guard; + }); + } +} From d598a74448e9463bf68f68974a0f86de1f13ce69 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 15:41:49 +0100 Subject: [PATCH 08/18] feat: move trait to Guards folder --- src/Guards/SessionGuard.php | 1 + src/{ => Guards}/Traits/SilentAuthentication.php | 0 2 files changed, 1 insertion(+) rename src/{ => Guards}/Traits/SilentAuthentication.php (100%) diff --git a/src/Guards/SessionGuard.php b/src/Guards/SessionGuard.php index 5ae98f6..988a0ff 100644 --- a/src/Guards/SessionGuard.php +++ b/src/Guards/SessionGuard.php @@ -14,6 +14,7 @@ namespace CyrildeWit\LaravelSilentAuthentication\Guards; use Illuminate\Auth\SessionGuard as BaseSessionGuard; +use CyrildeWit\LaravelSilentAuthentication\Guards\Traits\SilentAuthentication; class SessionGuard extends BaseSessionGuard { diff --git a/src/Traits/SilentAuthentication.php b/src/Guards/Traits/SilentAuthentication.php similarity index 100% rename from src/Traits/SilentAuthentication.php rename to src/Guards/Traits/SilentAuthentication.php From c05d30d548d3febaefc643d607941ecfe94fcd44 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 16:00:08 +0100 Subject: [PATCH 09/18] chore(readme): add some basic documentation info --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/README.md b/README.md index 412bd8e..0886ab7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,58 @@ # Laravel Silent Authentication This Laravel >= 5.5 package allows you to silently authenticate users. + +## Documentation + +In this documentation, you will find some helpful information about the use of this Laravel package. + +### Table of contents + +1. [Getting Started](#getting-started) + * [Requirements](#requirements) + * [Installation](#installation) +2. [Usage](#usage) + * [](#preparing-your-model) + +## Getting Started + +### Requirements + +This package requires **PHP 7.1+** and **Laravel 5.5+**. + +Lumen is not supported! + +#### Version information + +| Version | Illuminate | Status | PHP Version | +|---------|------------|----------------|-------------| +| 1.0 | 5.5 - 5.7 | Active support | >= 7.1.0 | + +### Installation + +First, you need to install the package via Composer: + +```winbatch +composer require cyrildewit/laravel-silent-authentication +``` + +You can optionally publish the config file with: + +```winbatch +php artisan vendor:publish --provider="CyrildeWit\LaravelSilentAuthentication\SilentAuthenticationServiceProvider" --tag="config" +``` + +#### Register service provider manually + +If you prefer to register packages manually, you can add the following provider to your application's providers list. + +```php +// config/app.php + +'providers' => [ + // ... + CyrildeWit\LaravelSilentAuthentication\SilentAuthenticationServiceProvider::class, +]; +``` + +## Usage From a895a0fbaa368b60d098241ba9e92687ca926799 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 16:11:42 +0100 Subject: [PATCH 10/18] chore(readme): add overview section --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 0886ab7..2890eb3 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ This Laravel >= 5.5 package allows you to silently authenticate users. +## Overview + +I created this package for personal usage. It's based on insights from other open source packages and online blog posts. + ## Documentation In this documentation, you will find some helpful information about the use of this Laravel package. From 7718a12d8e8eda020a5ad9730c0a333bd86db993 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 16:11:59 +0100 Subject: [PATCH 11/18] feat(composer): add initial composer.json fie --- composer.json | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a0d6722 --- /dev/null +++ b/composer.json @@ -0,0 +1,62 @@ +{ + "name": "cyrildewit/laravel-silent-authentication", + "description": "Silent authentication methods for Laravel", + "type": "library", + "license": "MIT", + "homepage": "https://github.com/cyrildewit/laravel-silent-authentication", + "keywords": [ + "laravel", + "silent", + "authentication", + "login", + "logout", + "no events" + ], + "support": { + "email": "github@cyrildewit.nl", + "issues": "https://github.com/cyrildewit/laravel-impersonate/issues", + "wiki": "https://github.com/cyrildewit/laravel-impersonate/wiki", + "source": "https://github.com/cyrildewit/laravel-impersonate", + "docs": "https://github.com/cyrildewit/laravel-impersonate/wiki" + }, + "authors": [ + { + "name": "Cyril de Wit", + "email": "info@cyrildewit.nl", + "homepage": "http://cyrildewit.nl", + "role": "Developer" + } + ], + "require": { + "php": "^7.1", + "illuminate/auth": "5.5.*|5.6.*|5.7.*" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0", + "phpunit/phpunit": "^6.5|^7.0" + }, + "autoload": { + "psr-4": { + "CyrildeWit\\LaravelSilentAuthentication\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "CyrildeWit\\LaravelSilentAuthentication\\Tests\\": "tests/" + } + }, + "scripts": { + "test": "vendor/bin/phpunit" + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "CyrildeWit\\LaravelSilentAuthentication\\SilentAuthenticationServiceProvider" + ] + } + } +} From a268a7a07c797ee3a3fac899e6879bd9c4dd7c25 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Fri, 8 Feb 2019 16:12:11 +0100 Subject: [PATCH 12/18] fix: imports of classes --- src/Guards/Traits/SilentAuthentication.php | 2 +- src/SilentAuthenticationServiceProvider.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Guards/Traits/SilentAuthentication.php b/src/Guards/Traits/SilentAuthentication.php index 00b17bf..a67ae61 100644 --- a/src/Guards/Traits/SilentAuthentication.php +++ b/src/Guards/Traits/SilentAuthentication.php @@ -11,7 +11,7 @@ * file that was distributed with this source code. */ -namespace CyrildeWit\LaravelSilentAuthentication\Traits; +namespace CyrildeWit\LaravelSilentAuthentication\Guards\Traits; use Illuminate\Contracts\Auth\Authenticatable; diff --git a/src/SilentAuthenticationServiceProvider.php b/src/SilentAuthenticationServiceProvider.php index 7fbfa07..9691347 100644 --- a/src/SilentAuthenticationServiceProvider.php +++ b/src/SilentAuthenticationServiceProvider.php @@ -11,9 +11,11 @@ * file that was distributed with this source code. */ -namespace CyrildeWit\EloquentViewable; +namespace CyrildeWit\LaravelSilentAuthentication; +use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; +use CyrildeWit\LaravelSilentAuthentication\Guards\SessionGuard; class SilentAuthenticationServiceProvider extends ServiceProvider { @@ -55,7 +57,7 @@ public function register() * @param void * @return void */ - protected function registerAuthDriver() + protected function registerSessionDriver() { $auth = $this->app['auth']; From 19f309a6a484ec3919f681b0516c17fa4a6bee61 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Sat, 9 Feb 2019 10:01:25 +0100 Subject: [PATCH 13/18] chore(changelog): add release notes for v0.1.0 --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6f07d1d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Release Notes + +All notable changes to `Laravel Silent Authentication` will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [v0.1.0] (2019-02-09) + +### Added + +- Added config file that allows people to customize/disable the default scaffholding +- Added `SessionGuard` that extends the original class but uses the `SilentAuthentication` trait +- Added `SilentAuthentication` trait which will contain the methods to silently authenticate users From 49a3ae46648625bb4b7fbe878a13e3580a7f4999 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Sat, 9 Feb 2019 10:10:21 +0100 Subject: [PATCH 14/18] chore(readme): add some documentation about the usage --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2890eb3..9efcf3f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ In this documentation, you will find some helpful information about the use of t * [Requirements](#requirements) * [Installation](#installation) 2. [Usage](#usage) - * [](#preparing-your-model) + * [Default setup](#default-setup) + * [SilentAuthentication trait](#preparing-your-model) ## Getting Started @@ -60,3 +61,31 @@ If you prefer to register packages manually, you can add the following provider ``` ## Usage + +### Default setup + +This package will overwrite the default `SessionGuard` by default. The customized session guard uses the `SilentAuthentication` trait which will allow you silently authenticate users. + +If you're not interesting in this default or if it's breaking your application, you can disable it in the config file. + +### SilentAuthentication trait + +If you're already overwriting the default `SessionGuard` in your application, you can simply implement the `SilentAuthentication` trait. + +```php +use Illuminate\Auth\SessionGuard as BaseSessionGuard; +use CyrildeWit\LaravelSilentAuthentication\Guards\Traits\SilentAuthentication; + +class SessionGuard extends BaseSessionGuard +{ + use SilentAuthentication; +} +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG-2.0.md) for more information on what has changed recently. + +## License + +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. From 67e71e8bfe97622e545ca8a0d2d054f3934ac3f4 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Sat, 9 Feb 2019 10:11:29 +0100 Subject: [PATCH 15/18] feat: update config file structure --- config/silent-authentication.php | 7 +++++-- src/SilentAuthenticationServiceProvider.php | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/config/silent-authentication.php b/config/silent-authentication.php index d73a97a..f1f1b91 100644 --- a/config/silent-authentication.php +++ b/config/silent-authentication.php @@ -2,8 +2,11 @@ return [ - 'enabled' => true, + 'default_session_guard' => [ - 'provider_name' => 'session', + 'enabled' => true, + 'provider_name' => 'session', + + ], ]; diff --git a/src/SilentAuthenticationServiceProvider.php b/src/SilentAuthenticationServiceProvider.php index 9691347..028d775 100644 --- a/src/SilentAuthenticationServiceProvider.php +++ b/src/SilentAuthenticationServiceProvider.php @@ -47,7 +47,7 @@ public function register() 'silent-authentication' ); - if (config('silent-authentication.enabled')) { + if (config('silent-authentication.default_session_guard.enabled')) { $this->registerSessionDriver(); } } @@ -61,7 +61,7 @@ protected function registerSessionDriver() { $auth = $this->app['auth']; - $providerName = config('silent-authentication.provider_name'); + $providerName = config('silent-authentication.default_session_guard.provider_name'); $auth->extend($providerName, function (Application $app, $name, array $config) use ($auth) { $provider = $auth->createUserProvider($config['provider']); From 3f812946bad95052d0c7e1a01f6aa9c290211a76 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Sat, 9 Feb 2019 10:15:27 +0100 Subject: [PATCH 16/18] ci(travis): set TravisCI up --- .travis.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ad41f67 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,31 @@ +language: php + +php: + - 7.1 + - 7.2 + - 7.3 + - nightly + +env: + matrix: + - COMPOSER_FLAGS="--prefer-lowest" + - COMPOSER_FLAGS="" + +matrix: + fast_finish: true + allow_failures: + - php: nightly + +cache: + directories: + - $HOME/.composer/cache + +before_script: + - travis_retry composer self-update + - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source + +script: + - vendor/bin/phpunit + +after_success: + - bash <(curl -s https://codecov.io/bash) From 80d1f9935a0b3dd33fdf63188db8dc8f8e1b6ff0 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Sat, 9 Feb 2019 10:16:00 +0100 Subject: [PATCH 17/18] ci(styleci): set StyleCI up --- .styleci.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .styleci.yml diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..0285f17 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: laravel From 65aba8eb00f010e70e008914b84e32a18ba2f1b6 Mon Sep 17 00:00:00 2001 From: Cyril de Wit Date: Sat, 9 Feb 2019 10:18:28 +0100 Subject: [PATCH 18/18] style: fix docblock --- src/SilentAuthenticationServiceProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SilentAuthenticationServiceProvider.php b/src/SilentAuthenticationServiceProvider.php index 028d775..863464f 100644 --- a/src/SilentAuthenticationServiceProvider.php +++ b/src/SilentAuthenticationServiceProvider.php @@ -53,8 +53,8 @@ public function register() } /** - * Register the custom session driver which will - * @param void + * Register the custom session driver with silent authentication. + * * @return void */ protected function registerSessionDriver()