├── ollieread └── multiauth │ ├── tests │ └── .gitkeep │ ├── .gitignore │ ├── .travis.yml │ ├── phpunit.xml │ ├── src │ └── Ollieread │ │ └── Multiauth │ │ ├── MultiauthServiceProvider.php │ │ ├── Reminders │ │ ├── PasswordBrokerManager.php │ │ ├── ReminderRepositoryInterface.php │ │ ├── ReminderServiceProvider.php │ │ ├── PasswordBroker.php │ │ └── DatabaseReminderRepository.php │ │ ├── Console │ │ ├── ClearRemindersCommand.php │ │ ├── stubs │ │ │ └── reminders.stub │ │ └── RemindersTableCommand.php │ │ ├── MultiManager.php │ │ ├── Guard.php │ │ └── AuthManager.php │ └── composer.json └── README.md /ollieread/multiauth/tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ollieread/multiauth/.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-5-MultiAuth 2 | This package handles Multiple Authentication in Laravel 5 (Fork of Ollieread MultiAuth). 3 | You can find the documentation here 4 | -------------------------------------------------------------------------------- /ollieread/multiauth/.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | 8 | before_script: 9 | - composer self-update 10 | - composer install --no-interaction --prefer-source --dev 11 | 12 | script: phpunit 13 | -------------------------------------------------------------------------------- /ollieread/multiauth/phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/MultiauthServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bindShared('auth', function ($app) { 14 | $app['auth.loaded'] = true; 15 | 16 | return new \Ollieread\Multiauth\MultiManager($app); 17 | }); 18 | } 19 | 20 | public function provides() 21 | { 22 | return array('auth'); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ollieread/multiauth/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ollieread/multiauth", 3 | "description": "Multiple auth driver for Laravel", 4 | "keywords": ["laravel", "multi", "auth"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Ollie Read", 9 | "email": "labs@ollieread.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.3.0", 14 | "illuminate/auth": "~4.1", 15 | "illuminate/console": "~4.1", 16 | "illuminate/database": "~4.1", 17 | "illuminate/filesystem": "~4.1", 18 | "illuminate/support": "~4.1" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "Ollieread\\Multiauth": "src/" 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Reminders/PasswordBrokerManager.php: -------------------------------------------------------------------------------- 1 | $provider) { 12 | $this->brokers[$type] = new PasswordBroker($type, $reminders, $provider, $mailer, $reminderViews[$type]); 13 | } 14 | } 15 | 16 | public function __call($name, $arguments = array()) { 17 | if(array_key_exists($name, $this->brokers)) { 18 | return $this->brokers[$name]; 19 | } 20 | } 21 | 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Console/ClearRemindersCommand.php: -------------------------------------------------------------------------------- 1 | laravel['auth.reminder.repository']->deleteExpired(); 29 | 30 | $this->info('Expired reminders cleared!'); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Console/stubs/reminders.stub: -------------------------------------------------------------------------------- 1 | string('type')->index(); 18 | $table->string('email')->index(); 19 | $table->string('token')->index(); 20 | $table->timestamp('created_at'); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::drop('password_reminders'); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/MultiManager.php: -------------------------------------------------------------------------------- 1 | app = $app; 20 | $this->config = $this->app['config']['auth.multi']; 21 | 22 | foreach($this->config as $key => $config) { 23 | $this->providers[$key] = new AuthManager($this->app, $key, $config); 24 | } 25 | } 26 | 27 | public function __call($name, $arguments = array()) { 28 | if(array_key_exists($name, $this->providers)) { 29 | return $this->providers[$name]; 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Reminders/ReminderRepositoryInterface.php: -------------------------------------------------------------------------------- 1 | name = $name; 16 | } 17 | 18 | public function getName() { 19 | return 'login_' . $this->name . '_' . md5(get_class($this)); 20 | } 21 | 22 | public function getRecallerName() { 23 | return 'remember_' . $this->name . '_' . md5(get_class($this)); 24 | } 25 | 26 | public function get() { 27 | return $this->user(); 28 | } 29 | 30 | public function impersonate($type, $id, $remember = false) { 31 | if($this->check()) { 32 | return Auth::$type()->loginUsingId($id, $remember); 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/AuthManager.php: -------------------------------------------------------------------------------- 1 | config = $config; 16 | $this->name = $name; 17 | } 18 | 19 | protected function createDriver($driver) { 20 | $guard = parent::createDriver($driver); 21 | 22 | $guard->setCookieJar($this->app['cookie']); 23 | $guard->setDispatcher($this->app['events']); 24 | 25 | return $guard->setRequest($this->app->refresh('request', $guard, 'setRequest')); 26 | } 27 | 28 | protected function callCustomCreator($driver) { 29 | $custom = parent::callCustomCreator($driver); 30 | 31 | if ($custom instanceof Guard) return $custom; 32 | 33 | return new Guard($custom, $this->app['session.store'], $this->name); 34 | } 35 | 36 | public function createDatabaseDriver() { 37 | $provider = $this->createDatabaseProvider(); 38 | 39 | return new Guard($provider, $this->app['session.store'], $this->name); 40 | } 41 | 42 | protected function createDatabaseProvider() { 43 | $connection = $this->app['db']->connection(); 44 | $table = $this->config['table']; 45 | 46 | return new DatabaseUserProvider($connection, $this->app['hash'], $table); 47 | } 48 | 49 | public function createEloquentDriver() { 50 | $provider = $this->createEloquentProvider(); 51 | 52 | return new Guard($provider, $this->app['session.store'], $this->name); 53 | } 54 | 55 | protected function createEloquentProvider() { 56 | $model = $this->config['model']; 57 | 58 | return new EloquentUserProvider($this->app['hash'], $model); 59 | } 60 | 61 | public function getDefaultDriver() { 62 | return $this->config['driver']; 63 | } 64 | 65 | public function setDefaultDriver($name) 66 | { 67 | $this->config['driver'] = $name; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Console/RemindersTableCommand.php: -------------------------------------------------------------------------------- 1 | files = $files; 40 | } 41 | 42 | /** 43 | * Execute the console command. 44 | * 45 | * @return void 46 | */ 47 | public function fire() 48 | { 49 | $fullPath = $this->createBaseMigration(); 50 | 51 | $this->files->put($fullPath, $this->getMigrationStub()); 52 | 53 | $this->info('Migration created successfully!'); 54 | 55 | $this->call('dump-autoload'); 56 | } 57 | 58 | /** 59 | * Create a base migration file for the reminders. 60 | * 61 | * @return string 62 | */ 63 | protected function createBaseMigration() 64 | { 65 | $name = 'create_password_reminders_table'; 66 | 67 | $path = $this->laravel['path'].'/database/migrations'; 68 | 69 | return $this->laravel['migration.creator']->create($name, $path); 70 | } 71 | 72 | /** 73 | * Get the contents of the reminder migration stub. 74 | * 75 | * @return string 76 | */ 77 | protected function getMigrationStub() 78 | { 79 | $stub = $this->files->get(__DIR__.'/stubs/reminders.stub'); 80 | 81 | return str_replace('password_reminders', $this->getTable(), $stub); 82 | } 83 | 84 | /** 85 | * Get the password reminder table name. 86 | * 87 | * @return string 88 | */ 89 | protected function getTable() 90 | { 91 | return $this->laravel['config']->get('auth.reminder.table'); 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Reminders/ReminderServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerPasswordBroker(); 25 | 26 | $this->registerReminderRepository(); 27 | 28 | $this->registerCommands(); 29 | } 30 | 31 | /** 32 | * Register the password broker instance. 33 | * 34 | * @return void 35 | */ 36 | protected function registerPasswordBroker() 37 | { 38 | $this->app->bindShared('auth.reminder', function($app) 39 | { 40 | // The reminder repository is responsible for storing the user e-mail addresses 41 | // and password reset tokens. It will be used to verify the tokens are valid 42 | // for the given e-mail addresses. We will resolve an implementation here. 43 | $reminders = $app['auth.reminder.repository']; 44 | 45 | $providers = $views = array(); 46 | 47 | foreach($app['config']['auth.multi'] as $type => $config) { 48 | $providers[$type] = $app['auth']->$type()->driver()->getProvider(); 49 | $views[$type] = isset($config['email']) ? $config['email'] : $app['config']['auth.reminder']['email']; 50 | } 51 | 52 | // The password broker uses the reminder repository to validate tokens and send 53 | // reminder e-mails, as well as validating that password reset process as an 54 | // aggregate service of sorts providing a convenient interface for resets. 55 | return new PasswordBrokerManager( 56 | 57 | $reminders, $app['mailer'], $views, $providers 58 | 59 | ); 60 | }); 61 | } 62 | 63 | /** 64 | * Register the reminder repository implementation. 65 | * 66 | * @return void 67 | */ 68 | protected function registerReminderRepository() 69 | { 70 | $this->app->bindShared('auth.reminder.repository', function($app) 71 | { 72 | $connection = $app['db']->connection(); 73 | 74 | // The database reminder repository is an implementation of the reminder repo 75 | // interface, and is responsible for the actual storing of auth tokens and 76 | // their e-mail addresses. We will inject this table and hash key to it. 77 | $table = $app['config']['auth.reminder.table']; 78 | 79 | $key = $app['config']['app.key']; 80 | 81 | $expire = $app['config']->get('auth.reminder.expire', 60); 82 | 83 | return new DbRepository($connection, $table, $key, $expire); 84 | }); 85 | } 86 | 87 | /** 88 | * Register the multiauth related console commands. 89 | * 90 | * @return void 91 | */ 92 | protected function registerCommands() 93 | { 94 | $this->app->bindShared('command.multiauth.reminders', function($app) 95 | { 96 | return new RemindersTableCommand($app['files']); 97 | }); 98 | 99 | $this->app->bindShared('command.multiauth.reminders.clear', function($app) 100 | { 101 | return new ClearRemindersCommand; 102 | }); 103 | 104 | $this->commands( 105 | 'command.multiauth.reminders', 'command.multiauth.reminders.clear' 106 | ); 107 | } 108 | 109 | /** 110 | * Get the services provided by the provider. 111 | * 112 | * @return array 113 | */ 114 | public function provides() 115 | { 116 | return array('auth.reminder'); 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Reminders/PasswordBroker.php: -------------------------------------------------------------------------------- 1 | users = $users; 20 | $this->mailer = $mailer; 21 | $this->reminders = $reminders; 22 | $this->reminderView = $reminderView; 23 | $this->type = $type; 24 | } 25 | 26 | /** 27 | * Send a password reminder to a user. 28 | * 29 | * @param array $credentials 30 | * @param Closure $callback 31 | * @return string 32 | */ 33 | public function remind(array $credentials, Closure $callback = null) 34 | { 35 | // First we will check to see if we found a user at the given credentials and 36 | // if we did not we will redirect back to this current URI with a piece of 37 | // "flash" data in the session to indicate to the developers the errors. 38 | $user = $this->getUser($credentials); 39 | 40 | if (is_null($user)) 41 | { 42 | return self::INVALID_USER; 43 | } 44 | 45 | // Once we have the reminder token, we are ready to send a message out to the 46 | // user with a link to reset their password. We will then redirect back to 47 | // the current URI having nothing set in the session to indicate errors. 48 | $token = $this->reminders->create($user, $this->type); 49 | 50 | $this->sendReminder($user, $token, $callback); 51 | 52 | return self::REMINDER_SENT; 53 | } 54 | 55 | /** 56 | * Send the password reminder e-mail. 57 | * 58 | * @param \Illuminate\Auth\Reminders\RemindableInterface $user 59 | * @param string $token 60 | * @param Closure $callback 61 | * @return void 62 | */ 63 | public function sendReminder(RemindableInterface $user, $token, Closure $callback = null) 64 | { 65 | // We will use the reminder view that was given to the broker to display the 66 | // password reminder e-mail. We'll pass a "token" variable into the views 67 | // so that it may be displayed for an user to click for password reset. 68 | $view = $this->reminderView; 69 | $type = $this->type; 70 | 71 | return $this->mailer->send($view, compact('token', 'user', 'type'), function($m) use ($user, $token, $type, $callback) 72 | { 73 | $m->to($user->getReminderEmail()); 74 | 75 | if ( ! is_null($callback)) call_user_func($callback, $m, $user, $type, $token); 76 | }); 77 | } 78 | 79 | /** 80 | * Reset the password for the given token. 81 | * 82 | * @param array $credentials 83 | * @param Closure $callback 84 | * @return mixed 85 | */ 86 | public function reset(array $credentials, Closure $callback) 87 | { 88 | // If the responses from the validate method is not a user instance, we will 89 | // assume that it is a redirect and simply return it from this method and 90 | // the user is properly redirected having an error message on the post. 91 | $user = $this->validateReset($credentials); 92 | 93 | if ( ! $user instanceof RemindableInterface) 94 | { 95 | return $user; 96 | } 97 | 98 | $pass = $credentials['password']; 99 | 100 | // Once we have called this callback, we will remove this token row from the 101 | // table and return the response from this callback so the user gets sent 102 | // to the destination given by the developers from the callback return. 103 | call_user_func($callback, $user, $pass); 104 | 105 | $this->reminders->delete($credentials['token'], $this->type); 106 | 107 | return self::PASSWORD_RESET; 108 | } 109 | 110 | /** 111 | * Validate a password reset for the given credentials. 112 | * 113 | * @param array $credentials 114 | * @return \Illuminate\Auth\Reminders\RemindableInterface 115 | */ 116 | protected function validateReset(array $credentials) 117 | { 118 | if (is_null($user = $this->getUser($credentials))) 119 | { 120 | return self::INVALID_USER; 121 | } 122 | 123 | if ( ! $this->validNewPasswords($credentials)) 124 | { 125 | return self::INVALID_PASSWORD; 126 | } 127 | 128 | if ( ! $this->reminders->exists($user, $credentials['token'], $this->type)) 129 | { 130 | return self::INVALID_TOKEN; 131 | } 132 | 133 | return $user; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /ollieread/multiauth/src/Ollieread/Multiauth/Reminders/DatabaseReminderRepository.php: -------------------------------------------------------------------------------- 1 | table = $table; 49 | $this->hashKey = $hashKey; 50 | $this->expires = $expires * 60; 51 | $this->connection = $connection; 52 | } 53 | 54 | /** 55 | * Create a new reminder record and token. 56 | * 57 | * @param \Illuminate\Auth\Reminders\RemindableInterface $user 58 | * @return string 59 | */ 60 | public function create(RemindableInterface $user, $type) 61 | { 62 | $email = $user->getReminderEmail(); 63 | 64 | // We will create a new, random token for the user so that we can e-mail them 65 | // a safe link to the password reset form. Then we will insert a record in 66 | // the database so that we can verify the token within the actual reset. 67 | $token = $this->createNewToken($user); 68 | 69 | $this->getTable()->insert($this->getPayload($email, $token, $type)); 70 | 71 | return $token; 72 | } 73 | 74 | /** 75 | * Build the record payload for the table. 76 | * 77 | * @param string $email 78 | * @param string $token 79 | * @return array 80 | */ 81 | protected function getPayload($email, $token, $type) 82 | { 83 | return array('type' => $type, 'email' => $email, 'token' => $token, 'created_at' => new Carbon); 84 | } 85 | 86 | /** 87 | * Determine if a reminder record exists and is valid. 88 | * 89 | * @param \Illuminate\Auth\Reminders\RemindableInterface $user 90 | * @param string $token 91 | * @return bool 92 | */ 93 | public function exists(RemindableInterface $user, $token, $type) 94 | { 95 | $email = $user->getReminderEmail(); 96 | 97 | $reminder = $this->getTable()->where('email', $email)->where('token', $token)->where('type', $type)->first(); 98 | 99 | return $reminder && ! $this->reminderExpired($reminder); 100 | } 101 | 102 | /** 103 | * Determine if the reminder has expired. 104 | * 105 | * @param object $reminder 106 | * @return bool 107 | */ 108 | protected function reminderExpired($reminder) 109 | { 110 | $createdPlusHour = strtotime($reminder->created_at) + $this->expires; 111 | 112 | return $createdPlusHour < $this->getCurrentTime(); 113 | } 114 | 115 | /** 116 | * Get the current UNIX timestamp. 117 | * 118 | * @return int 119 | */ 120 | protected function getCurrentTime() 121 | { 122 | return time(); 123 | } 124 | 125 | /** 126 | * Delete a reminder record by token. 127 | * 128 | * @param string $token 129 | * @return void 130 | */ 131 | public function delete($token, $type) 132 | { 133 | $this->getTable()->where('token', $token)->where('type', $type)->delete(); 134 | } 135 | 136 | /** 137 | * Delete expired reminders. 138 | * 139 | * @return void 140 | */ 141 | public function deleteExpired() 142 | { 143 | $expired = Carbon::now()->subSeconds($this->expires); 144 | 145 | $this->getTable()->where('created_at', '<', $expired)->delete(); 146 | } 147 | 148 | /** 149 | * Create a new token for the user. 150 | * 151 | * @param \Illuminate\Auth\Reminders\RemindableInterface $user 152 | * @return string 153 | */ 154 | public function createNewToken(RemindableInterface $user) 155 | { 156 | $email = $user->getReminderEmail(); 157 | 158 | $value = str_shuffle(sha1($email.spl_object_hash($this).microtime(true))); 159 | 160 | return hash_hmac('sha1', $value, $this->hashKey); 161 | } 162 | 163 | /** 164 | * Begin a new database query against the table. 165 | * 166 | * @return \Illuminate\Database\Query\Builder 167 | */ 168 | protected function getTable() 169 | { 170 | return $this->connection->table($this->table); 171 | } 172 | 173 | /** 174 | * Get the database connection instance. 175 | * 176 | * @return \Illuminate\Database\Connection 177 | */ 178 | public function getConnection() 179 | { 180 | return $this->connection; 181 | } 182 | 183 | } --------------------------------------------------------------------------------