| Conditions | 12 |
| Paths | 36 |
| Total Lines | 80 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 |
||
| 82 | protected function login(string $username, string $password): array |
||
| 83 | { |
||
| 84 | $this->verifyUserNameWithRegex($username); |
||
| 85 | |||
| 86 | $db = $this->connect(); |
||
| 87 | $params = ['username' => $username]; |
||
| 88 | $attributes = []; |
||
| 89 | |||
| 90 | $numQueries = count($this->query); |
||
| 91 | for ($x = 0; $x < $numQueries; $x++) { |
||
| 92 | $data = $this->executeQuery($db, $this->query[$x], $params); |
||
| 93 | |||
| 94 | Logger::info('sqlauth:' . $this->authId . ': Got ' . count($data) . |
||
| 95 | ' rows from database'); |
||
| 96 | |||
| 97 | if ($x === 0) { |
||
| 98 | if (count($data) === 0) { |
||
| 99 | // No rows returned - invalid username/password |
||
| 100 | Logger::error('sqlauth:' . $this->authId . |
||
| 101 | ': No rows in result set. Probably wrong username/password.'); |
||
| 102 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Sanity check, passwordhash must be in each resulting tuple and must have |
||
| 109 | * the same value in every tuple. |
||
| 110 | * |
||
| 111 | * Note that $pwhash will contain the passwordhash value after this loop. |
||
| 112 | */ |
||
| 113 | $pwhash = null; |
||
| 114 | foreach ($data as $row) { |
||
| 115 | if ( |
||
| 116 | !array_key_exists($this->passwordhashcolumn, $row) |
||
| 117 | || is_null($row[$this->passwordhashcolumn]) |
||
| 118 | ) { |
||
| 119 | Logger::error('sqlauth:' . $this->authId . |
||
| 120 | ': column ' . $this->passwordhashcolumn . ' must be in every result tuple.'); |
||
| 121 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 122 | } |
||
| 123 | if ($pwhash) { |
||
| 124 | if ($pwhash != $row[$this->passwordhashcolumn]) { |
||
| 125 | Logger::error('sqlauth:' . $this->authId |
||
| 126 | . ': column ' . $this->passwordhashcolumn |
||
| 127 | . ' must be THE SAME in every result tuple.'); |
||
| 128 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $pwhash = $row[$this->passwordhashcolumn]; |
||
| 132 | } |
||
| 133 | /** |
||
| 134 | * This should never happen as the count(data) test above would have already thrown. |
||
| 135 | * But checking twice doesn't hurt. |
||
| 136 | */ |
||
| 137 | if (is_null($pwhash)) { |
||
| 138 | if ($pwhash != $row[$this->passwordhashcolumn]) { |
||
|
|
|||
| 139 | Logger::error('sqlauth:' . $this->authId . |
||
| 140 | ': column ' . $this->passwordhashcolumn . ' does not contain a password hash.'); |
||
| 141 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * VERIFICATION! |
||
| 147 | * Now to check if the password the user supplied is actually valid |
||
| 148 | */ |
||
| 149 | if (!password_verify($password, $pwhash)) { |
||
| 150 | Logger::error('sqlauth:' . $this->authId . ': password is incorrect.'); |
||
| 151 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | $this->extractAttributes($attributes, $data, [$this->passwordhashcolumn]); |
||
| 156 | } |
||
| 157 | |||
| 158 | Logger::info('sqlauth:' . $this->authId . ': Attributes: ' . |
||
| 159 | implode(',', array_keys($attributes))); |
||
| 160 | |||
| 161 | return $attributes; |
||
| 162 | } |
||
| 164 |