| Conditions | 12 |
| Paths | 36 |
| Total Lines | 83 |
| Code Lines | 40 |
| 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, 'password' => $password]; |
||
| 96 | $attributes = []; |
||
| 97 | |||
| 98 | $numQueries = count($this->query); |
||
| 99 | for ($x = 0; $x < $numQueries; $x++) { |
||
| 100 | |||
| 101 | $data = $this->executeQuery($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 | /* Only the first query should be passed the password, as that is the only |
||
| 114 | * one used for authentication. Subsequent queries are only used for |
||
| 115 | * getting attribute lists, so only need the username. */ |
||
| 116 | unset($params['password']); |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * Sanity check, passwordhash must be in each resulting tuple and must have |
||
| 122 | * the same value in every tuple. |
||
| 123 | * |
||
| 124 | * Note that $pwhash will contain the passwordhash value after this loop. |
||
| 125 | */ |
||
| 126 | $pwhash = null; |
||
| 127 | foreach ($data as $row) { |
||
| 128 | if (!array_key_exists($this->passwordhashcolumn, $row) |
||
| 129 | || is_null($row[$this->passwordhashcolumn])) |
||
| 130 | { |
||
| 131 | Logger::error('sqlauth:'.$this->authId. |
||
| 132 | ': column ' . $this->passwordhashcolumn . ' must be in every result tuple.'); |
||
| 133 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 134 | } |
||
| 135 | if( $pwhash ) { |
||
| 136 | if( $pwhash != $row[$this->passwordhashcolumn] ) { |
||
| 137 | Logger::error('sqlauth:'.$this->authId. |
||
| 138 | ': column ' . $this->passwordhashcolumn . ' must be THE SAME in every result tuple.'); |
||
| 139 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | $pwhash = $row[$this->passwordhashcolumn]; |
||
| 143 | } |
||
| 144 | /** |
||
| 145 | * This should never happen as the count(data) test above would have already thrown. |
||
| 146 | * But checking twice doesn't hurt. |
||
| 147 | */ |
||
| 148 | if( is_null($pwhash)) { |
||
| 149 | if( $pwhash != $row[$this->passwordhashcolumn] ) { |
||
| 150 | Logger::error('sqlauth:'.$this->authId. |
||
| 151 | ': column ' . $this->passwordhashcolumn . ' does not contain a password hash.'); |
||
| 152 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * VERIFICATION! |
||
| 158 | * Now to check if the password the user supplied is actually valid |
||
| 159 | */ |
||
| 160 | if( !password_verify( $password, $pwhash )) { |
||
| 161 | Logger::error('sqlauth:'.$this->authId. ': password is incorrect.'); |
||
| 162 | throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | $this->extractAttributes( $attributes, $data, array($this->passwordhashcolumn) ); |
||
| 167 | } |
||
| 168 | |||
| 169 | Logger::info('sqlauth:'.$this->authId.': Attributes: '. |
||
| 170 | implode(',', array_keys($attributes))); |
||
| 171 | |||
| 172 | return $attributes; |
||
| 173 | } |
||
| 175 |