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