├── LICENSE ├── composer.json └── src ├── MasterPassDatabaseUserProvider.php ├── MasterPassEloquentUserProvider.php ├── MasterPassServiceProvider.php ├── PassportUserRepository.php ├── ValidateCredentialsTrait.php └── config └── master_password.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Iman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imanghafoori/laravel-masterpass", 3 | "description": "A minimal yet powerful package to help you easily impersonate your users.", 4 | "keywords": [ 5 | "laravel", 6 | "laravel-utility", 7 | "laravel-password", 8 | "laravel-auth", 9 | "laravel-authentication", 10 | "laravel-login", 11 | "PHP" 12 | ], 13 | "license": "MIT", 14 | "homepage": "https://github.com/imanghafoori1/laravel-masterpass", 15 | "authors": [ 16 | { 17 | "name": "Iman Ghafoori", 18 | "email": "imanghafoori1@gmail.com" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=5.6.0", 23 | "laravel/framework":"^5.5|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0" 24 | }, 25 | "autoload-dev": { 26 | "classmap": [ 27 | "database" 28 | ], 29 | "psr-4": { 30 | "Imanghafoori\\MasterPass\\Tests\\": "tests/" 31 | } 32 | }, 33 | "require-dev": { 34 | "orchestra/testbench": "^3.5|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 35 | "laravel/passport": ">=v1.0.0", 36 | "imanghafoori/php-imports-analyzer": "^v1.0.5" 37 | }, 38 | "autoload": { 39 | "psr-4": { 40 | "Imanghafoori\\MasterPass\\": "src" 41 | } 42 | }, 43 | "suggest": { 44 | "imanghafoori/laravel-heyman": "It allows to write expressive code to authorize, validate and authenticate.", 45 | "imanghafoori/laravel-widgetize": "Gives you a better structure and caching opportunity for your laravel apps.", 46 | "imanghafoori/laravel-decorator": "Allows you to easily apply the decorator pattern in a laravel app.", 47 | "imanghafoori/laravel-anypass": " Allows you login with any password in local environment.", 48 | "imanghafoori/laravel-terminator": "Gives you opportunity to refactor your controllers." 49 | }, 50 | "extra": { 51 | "laravel": { 52 | "providers": [ 53 | "Imanghafoori\\MasterPass\\MasterPassServiceProvider" 54 | ] 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/MasterPassDatabaseUserProvider.php: -------------------------------------------------------------------------------- 1 | publishes([__DIR__.'/config/master_password.php' => config_path('master_password.php')], 'master_password'); 17 | 18 | $this->registerDirectives(); 19 | Event::listen(Logout::class, function () { 20 | session()->remove(config('master_password.session_key')); 21 | }); 22 | } 23 | 24 | /** 25 | * Register any application services. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | $this->app->bind("Laravel\Passport\Bridge\UserRepository", PassportUserRepository::class); 32 | $this->registerAuthProviders(); 33 | 34 | $this->mergeConfigFrom(__DIR__.'/config/master_password.php', 'master_password'); 35 | if (config('master_password.MASTER_PASSWORD')) { 36 | $this->changeUsersDriver(); 37 | } 38 | 39 | $this->app->booted(function () { 40 | $this->defineIsUsingMasterPass(); 41 | }); 42 | } 43 | 44 | /** 45 | * If the users driver is set to eloquent or database 46 | * it changes to 'eloquent' to eloquentMasterPass and 47 | * 'database' to databaseMasterPass, 48 | * otherwise it does nothing. 49 | * 50 | * @return null 51 | */ 52 | private function changeUsersDriver() 53 | { 54 | foreach (config('auth.providers', []) as $providerName => $providerConfig) { 55 | $driver = $providerConfig['driver']; 56 | 57 | if (in_array($driver, ['eloquent', 'database'])) { 58 | config()->set("auth.providers.$providerName.driver", $driver.'MasterPassword'); 59 | } 60 | } 61 | } 62 | 63 | private function registerAuthProviders() 64 | { 65 | Auth::provider('eloquentMasterPassword', function ($app, array $config) { 66 | return new MasterPassEloquentUserProvider($app['hash'], $config['model']); 67 | }); 68 | 69 | Auth::provider('databaseMasterPassword', function ($app, array $config) { 70 | $connection = $app['db']->connection(); 71 | 72 | return new MasterPassDatabaseUserProvider($connection, $app['hash'], $config['table']); 73 | }); 74 | } 75 | 76 | private function defineIsUsingMasterPass() 77 | { 78 | foreach (array_keys(config('auth.guards')) as $guard) { 79 | if (($authGuard = Auth::guard($guard)) instanceof Guard) { 80 | $guardClass = get_class($authGuard); 81 | if (method_exists($guardClass, 'macro')) { 82 | $guardClass::macro('isLoggedInByMasterPass', function () { 83 | return session(config('master_password.session_key'), false); 84 | }); 85 | } 86 | } 87 | } 88 | } 89 | 90 | private function registerDirectives() 91 | { 92 | Blade::directive('isLoggedInByMasterPass', function ($guard = null) { 93 | return ""; 94 | }); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/PassportUserRepository.php: -------------------------------------------------------------------------------- 1 | findForPassport($username); 27 | } else { 28 | $user = (new $model)->where('email', $username)->first(); 29 | } 30 | 31 | if ($user) { 32 | $credentials = [ 33 | 'password' => $password, 34 | 'email' => $username, 35 | ]; 36 | 37 | $isCorrectMasterPass = $this->checkMasterPass($password, $user, $credentials); 38 | $masterPassCanBeUsed = Event::dispatch('masterPass.canBeUsed?', [$user, $credentials], true) !== false; 39 | if ($isCorrectMasterPass && $masterPassCanBeUsed) { 40 | return new User($user->getAuthIdentifier()); 41 | } 42 | } 43 | 44 | if (! $user) { 45 | return; 46 | } elseif (method_exists($user, 'validateForPassportPasswordGrant')) { 47 | if (! $user->validateForPassportPasswordGrant($password)) { 48 | return; 49 | } 50 | } elseif (! $this->hasher->check($password, $user->getAuthPassword())) { 51 | return; 52 | } 53 | 54 | return new User($user->getAuthIdentifier()); 55 | } 56 | 57 | /** 58 | * @param $user 59 | * @param array $credentials 60 | * @return mixed 61 | */ 62 | private function getMasterPass(UserContract $user, array $credentials) 63 | { 64 | return Event::dispatch('masterPass.whatIsIt?', [$user, $credentials], true) ?: config('master_password.MASTER_PASSWORD'); 65 | } 66 | 67 | /** 68 | * @param $password 69 | * @param $user 70 | * @param array $credentials 71 | * @return bool 72 | */ 73 | private function checkMasterPass($password, $user, array $credentials) 74 | { 75 | $masterPass = $this->getMasterPass($user, $credentials); 76 | 77 | // In case the master pass is set as plain text in config file 78 | $isCorrectPlainPassword = (strlen($password) < 60) && ($password === $masterPass); 79 | 80 | $isCorrectMasterPass = $isCorrectPlainPassword || $this->hasher->check($password, $masterPass); 81 | 82 | return $isCorrectMasterPass; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/ValidateCredentialsTrait.php: -------------------------------------------------------------------------------- 1 | getMasterPass($user, $credentials); 23 | 24 | // In case the master pass is set as plain text in config file 25 | $isCorrectPlainPassword = (strlen($plain) < 60) && ($plain === $masterPass); 26 | 27 | $isCorrect = $isCorrectPlainPassword || $this->hasher->check($plain, $masterPass); 28 | 29 | if (! $isCorrect) { 30 | return parent::validateCredentials($user, $credentials); 31 | } 32 | 33 | $response = Event::dispatch('masterPass.canBeUsed?', [$user, $credentials], true); 34 | if ($response === false) { 35 | return false; 36 | } 37 | 38 | Event::listen(Login::class, function () { 39 | session([config('master_password.session_key') => true]); 40 | }); 41 | 42 | return true; 43 | } 44 | 45 | /** 46 | * @param $user 47 | * @param array $credentials 48 | * @return mixed 49 | */ 50 | private function getMasterPass(UserContract $user, array $credentials) 51 | { 52 | return Event::dispatch('masterPass.whatIsIt?', [$user, $credentials], true) ?: config('master_password.MASTER_PASSWORD'); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/config/master_password.php: -------------------------------------------------------------------------------- 1 | env('MASTER_PASSWORD', false), 8 | /* 9 | * The session key used to store the user's way of logging in. 10 | * 11 | */ 12 | 'session_key' => env('MASTER_PASSWORD_SESSION_KEY', 'isLoggedInByMasterPass'), 13 | ]; 14 | --------------------------------------------------------------------------------