Conditions | 12 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
98 | public function authUser(array $user): int |
||
99 | { |
||
100 | // Early 100 "not responsible, check other services" if username or password is empty |
||
101 | if (!isset($this->login['uident_text']) || (string)$this->login['uident_text'] === '' |
||
102 | || !isset($this->login['uname']) || (string)$this->login['uname'] === '') { |
||
103 | return 100; |
||
104 | } |
||
105 | |||
106 | if (empty($this->db_user['table'])) { |
||
107 | throw new \RuntimeException('User database table not set', 1533159150); |
||
108 | } |
||
109 | |||
110 | $submittedUsername = (string)$this->login['uname']; |
||
111 | $submittedPassword = (string)$this->login['uident_text']; |
||
112 | $passwordHashInDatabase = $user['password']; |
||
113 | $userDatabaseTable = $this->db_user['table']; |
||
114 | |||
115 | $isReHashNeeded = false; |
||
116 | |||
117 | $saltFactory = GeneralUtility::makeInstance(PasswordHashFactory::class); |
||
118 | |||
119 | // Get a hashed password instance for the hash stored in db of this user |
||
120 | try { |
||
121 | $hashInstance = $saltFactory->get($passwordHashInDatabase, $this->pObj->loginType); |
||
122 | } catch (InvalidPasswordHashException $exception) { |
||
123 | // Could not find a responsible hash algorithm for given password. This is unusual since other |
||
124 | // authentication services would usually be called before this one with higher priority. We thus log |
||
125 | // the failed login but still return '100' to proceed with other services that may follow. |
||
126 | $message = 'Login-attempt from ###IP###, username \'%s\', no suitable hash method found!'; |
||
127 | $this->writeLogMessage($message, $submittedUsername); |
||
128 | $this->writelog(SystemLogType::LOGIN, SystemLogLoginAction::ATTEMPT, SystemLogErrorClassification::SECURITY_NOTICE, 1, $message, [$submittedUsername]); |
||
129 | $this->logger->info(sprintf($message, $submittedUsername)); |
||
130 | // Not responsible, check other services |
||
131 | return 100; |
||
132 | } |
||
133 | |||
134 | // An instance of the currently configured salted password mechanism |
||
135 | // Don't catch InvalidPasswordHashException here: Only install tool should handle those configuration failures |
||
136 | $defaultHashInstance = $saltFactory->getDefaultHashInstance($this->pObj->loginType); |
||
137 | |||
138 | // We found a hash class that can handle this type of hash |
||
139 | $isValidPassword = $hashInstance->checkPassword($submittedPassword, $passwordHashInDatabase); |
||
140 | if ($isValidPassword) { |
||
141 | if ($hashInstance->isHashUpdateNeeded($passwordHashInDatabase) |
||
142 | || $defaultHashInstance != $hashInstance |
||
143 | ) { |
||
144 | // Lax object comparison intended: Rehash if old and new salt objects are not |
||
145 | // instances of the same class. |
||
146 | $isReHashNeeded = true; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | if (!$isValidPassword) { |
||
151 | // Failed login attempt - wrong password |
||
152 | $this->writeLogMessage($this->pObj->loginType . ' Authentication failed - wrong password for username \'%s\'', $submittedUsername); |
||
153 | $message = 'Login-attempt from ###IP###, username \'%s\', password not accepted!'; |
||
154 | $this->writelog(SystemLogType::LOGIN, SystemLogLoginAction::ATTEMPT, SystemLogErrorClassification::SECURITY_NOTICE, 1, $message, [$submittedUsername]); |
||
155 | $this->logger->info(sprintf($message, $submittedUsername)); |
||
156 | // Responsible, authentication failed, do NOT check other services |
||
157 | return 0; |
||
158 | } |
||
159 | |||
160 | if ($isReHashNeeded) { |
||
161 | // Given password validated but a re-hash is needed. Do so. |
||
162 | $this->updatePasswordHashInDatabase( |
||
163 | $userDatabaseTable, |
||
164 | (int)$user['uid'], |
||
165 | $defaultHashInstance->getHashedPassword($submittedPassword) |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | // Responsible, authentication ok. Log successful login and return 'auth ok, do NOT check other services' |
||
170 | $this->writeLogMessage($this->pObj->loginType . ' Authentication successful for username \'%s\'', $submittedUsername); |
||
171 | return 200; |
||
172 | } |
||
213 |