| Conditions | 13 |
| Paths | 21 |
| Total Lines | 92 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 123 | protected function login(string $username, string $password): array |
||
| 124 | { |
||
| 125 | assert(is_string($username)); |
||
| 126 | assert(is_string($password)); |
||
| 127 | |||
| 128 | |||
| 129 | $db = $this->connect(); |
||
| 130 | |||
| 131 | try { |
||
| 132 | $sth = $db->prepare($this->query); |
||
| 133 | } catch (\PDOException $e) { |
||
| 134 | throw new \Exception('sqlauth:'.$this->authId. |
||
| 135 | ': - Failed to prepare query: '.$e->getMessage()); |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | try { |
||
| 140 | $sth->execute(['username' => $username]); |
||
| 141 | } catch (\PDOException $e) { |
||
| 142 | throw new \Exception('sqlauth:'.$this->authId. |
||
| 143 | ': - Failed to execute sql: '.$this->query.' query: '.$e->getMessage()); |
||
| 144 | } |
||
| 145 | |||
| 146 | try { |
||
| 147 | $data = $sth->fetchAll(\PDO::FETCH_ASSOC); |
||
| 148 | } catch (\PDOException $e) { |
||
| 149 | throw new \Exception('sqlauth:'.$this->authId. |
||
| 150 | ': - Failed to fetch result set: '.$e->getMessage()); |
||
| 151 | } |
||
| 152 | |||
| 153 | \SimpleSAML\Logger::info('sqlauth:'.$this->authId.': Got '.count($data). |
||
| 154 | ' rows from database'); |
||
| 155 | |||
| 156 | if (count($data) === 0) { |
||
| 157 | // No rows returned - invalid username/password |
||
| 158 | \SimpleSAML\Logger::error('sqlauth:'.$this->authId. |
||
| 159 | ': No rows in result set. Probably wrong username/password.'); |
||
| 160 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Sanity check, passwordhash must be in each resulting tuple and must have |
||
| 165 | * the same value in every tuple. |
||
| 166 | * |
||
| 167 | * Note that $pwhash will contain the passwordhash value after this loop. |
||
| 168 | */ |
||
| 169 | $pwhash = null; |
||
| 170 | foreach ($data as $row) { |
||
| 171 | if (!array_key_exists($this->passwordhashcolumn, $row) |
||
| 172 | || is_null($row[$this->passwordhashcolumn])) |
||
| 173 | { |
||
| 174 | \SimpleSAML\Logger::error('sqlauth:'.$this->authId. |
||
| 175 | ': column ' . $this->passwordhashcolumn . ' must be in every result tuple.'); |
||
| 176 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 177 | } |
||
| 178 | if( $pwhash ) { |
||
| 179 | if( $pwhash != $row[$this->passwordhashcolumn] ) { |
||
| 180 | \SimpleSAML\Logger::error('sqlauth:'.$this->authId. |
||
| 181 | ': column ' . $this->passwordhashcolumn . ' must be THE SAME in every result tuple.'); |
||
| 182 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $pwhash = $row[$this->passwordhashcolumn]; |
||
| 186 | } |
||
| 187 | /** |
||
| 188 | * This should never happen as the count(data) test above would have already thrown. |
||
| 189 | * But checking twice doesn't hurt. |
||
| 190 | */ |
||
| 191 | if( is_null($pwhash)) { |
||
| 192 | if( $pwhash != $row[$this->passwordhashcolumn] ) { |
||
|
|
|||
| 193 | \SimpleSAML\Logger::error('sqlauth:'.$this->authId. |
||
| 194 | ': column ' . $this->passwordhashcolumn . ' does not contain a password hash.'); |
||
| 195 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * VERIFICATION! |
||
| 201 | * Now to check if the password the user supplied is actually valid |
||
| 202 | */ |
||
| 203 | if( !password_verify( $password, $pwhash )) { |
||
| 204 | \SimpleSAML\Logger::error('sqlauth:'.$this->authId. ': password is incorrect.'); |
||
| 205 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | $attributes = $this->extractAttributes( $data, array($this->passwordhashcolumn) ); |
||
| 210 | |||
| 211 | \SimpleSAML\Logger::info('sqlauth:'.$this->authId.': Attributes: '. |
||
| 212 | implode(',', array_keys($attributes))); |
||
| 213 | |||
| 214 | return $attributes; |
||
| 215 | } |
||
| 217 |