| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Licensed under The MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For full copyright and license information, please see the LICENSE.txt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Redistributions of files must retain the above copyright notice. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link          http://cakephp.org CakePHP(tm) Project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license       http://www.opensource.org/licenses/mit-license.php MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | namespace Phauthentic\Authentication\Authenticator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use ArrayAccess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use ArrayObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Phauthentic\Authentication\Authenticator\Storage\StorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Phauthentic\Authentication\Identifier\IdentifierInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Session Authenticator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class SessionAuthenticator extends AbstractAuthenticator implements PersistenceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var array<string, string> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected array $credentialFields = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         IdentifierInterface::CREDENTIAL_USERNAME => 'username', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected bool $verify = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var \Phauthentic\Authentication\Authenticator\Storage\StorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected StorageInterface $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 56 |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         IdentifierInterface $identifiers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 56 |  |         StorageInterface $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 56 |  |         parent::__construct($identifiers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 56 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Set the fields to use to verify a user by. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param array<string, string> $fields Credential fields. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function setCredentialFields(array $fields): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->credentialFields = $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Enable identity verification after it is retrieved from the session storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 8 |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 8 |  |     public function enableVerification(): self | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 8 |  |         $this->verify = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Disable identity verification after it is retrieved from the session storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function disableVerification(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->verify = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Authenticate a user using session data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param \Psr\Http\Message\ServerRequestInterface $request The request to authenticate with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 36 |  |      * @return \Phauthentic\Authentication\Authenticator\ResultInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 36 |  |     public function authenticate(ServerRequestInterface $request): ResultInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 36 |  |         $user = $this->storage->read($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 20 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (empty($user)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 16 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 8 |  |         if ($this->verify) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 8 |  |             $credentials = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             foreach ($this->credentialFields as $key => $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 8 |  |                 $credentials[$key] = $user[$field]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 8 |  |             $user = $this->identifier->identify($credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             if (empty($user)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 return new Result(null, Result::FAILURE_CREDENTIALS_INVALID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if (!($user instanceof ArrayAccess)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $user = new ArrayObject($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return new Result($user, Result::SUCCESS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 8 |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 8 |  |     public function clearIdentity(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this->storage->clear($request, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 12 |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 12 |  |     public function persistIdentity(ServerRequestInterface $request, ResponseInterface $response, ArrayAccess $data): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         return $this->storage->write($request, $response, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |