| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SamAsEnd\NeedsAutoRehash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Auth\CreatesUserProviders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Auth\DatabaseUserProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Auth\EloquentUserProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Auth\Events\Attempting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Contracts\Auth\Authenticatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Contracts\Auth\Factory as AuthContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Contracts\Config\Repository as ConfigContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Contracts\Container\BindingResolutionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Illuminate\Contracts\Container\Container as ContainerContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Illuminate\Contracts\Hashing\Hasher as HashContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SamAsEnd\NeedsAutoRehash\Providers\DatabaseUserProviderWithPasswordUpdate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SamAsEnd\NeedsAutoRehash\Providers\EloquentUserProviderWithPasswordUpdate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SamAsEnd\NeedsAutoRehash\Providers\ProviderWithPasswordUpdate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class FromAttemptPasswordReHasher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     use CreatesUserProviders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var ContainerContract */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var AuthContract */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var HashContract */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** @var ConfigContract */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** @var ProviderWithPasswordUpdate */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ContainerContract $container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         AuthContract $auth, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         HashContract $hash, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ConfigContract $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 5 |  |         $this->app = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 5 |  |         $this->auth = $auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |         $this->hash = $hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 5 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 5 |  |         $this->provider = $this->getUserProviderWithPasswordUpdate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |     protected function getUserProviderWithPasswordUpdate(): ProviderWithPasswordUpdate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 5 |  |         $provider = $this->createUserProvider($this->getUserProviderName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 5 |  |         if ($provider instanceof DatabaseUserProvider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             return new DatabaseUserProviderWithPasswordUpdate($provider); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 4 |  |         if ($provider instanceof EloquentUserProvider) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |             return new EloquentUserProviderWithPasswordUpdate($provider); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |             return $this->app->make(ProviderWithPasswordUpdate::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         } catch (BindingResolutionException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |             throw new UnexpectedProviderException('ProviderWithPasswordUpdate could not be found.', $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |     public function handle(Attempting $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |         $user = $this->provider->retrieveByCredentials($event->credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |         if (!is_null($user) && $this->validCredentials($event) && $this->passwordNeedsRehash($user)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |             $this->passwordUpdateRehash($user, $event->credentials['password']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |     protected function validCredentials(Attempting $attempting): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |         return $this->auth->guard($attempting->guard)->validate($attempting->credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 4 |  |     protected function passwordNeedsRehash(Authenticatable $user) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 4 |  |         return $this->hash->needsRehash($user->getAuthPassword(), $this->getHashingOptions()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |     protected function passwordUpdateRehash(Authenticatable $user, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |         $this->provider->updatePassword($user, $password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 5 |  |     protected function getUserProviderName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 5 |  |         $guard = $this->config->get('auth.defaults.guard'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 5 |  |             $this->getDefaultUserProvider() ?? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 5 |  |             $this->config->get('auth.guards.'.$guard.'.provider'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  |     protected function getHashingOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 4 |  |         $driver = $this->config->get('hashing.driver'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  |         return $this->config->get('hashing.'.$driver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |