| Conditions | 15 |
| Paths | 117 |
| Total Lines | 81 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 140 | protected function login( |
||
| 141 | string $username, |
||
| 142 | #[\SensitiveParameter] |
||
| 143 | string $password, |
||
| 144 | ): array { |
||
| 145 | $radius = new RadiusClient(); |
||
| 146 | $response = false; |
||
| 147 | |||
| 148 | // Try to add all radius servers, trigger a failure if no one works |
||
| 149 | foreach ($this->servers as $server) { |
||
| 150 | $radius->setServer($server['hostname']); |
||
| 151 | $radius->setAuthenticationPort($server['port']); |
||
| 152 | $radius->setSecret($server['secret']); |
||
| 153 | $radius->setDebug($this->debug); |
||
| 154 | $radius->setTimeout($this->timeout); |
||
| 155 | $radius->setIncludeMessageAuthenticator(); |
||
| 156 | |||
| 157 | $httpUtils = new Utils\HTTP(); |
||
| 158 | $radius->setNasIpAddress($_SERVER['SERVER_ADDR'] ?: $httpUtils->getSelfHost()); |
||
| 159 | |||
| 160 | if ($this->nasIdentifier !== null) { |
||
| 161 | $radius->setAttribute(self::RADIUS_NAS_IDENTIFIER, $this->nasIdentifier); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($this->realm !== null) { |
||
| 165 | $radius->setRadiusSuffix('@' . $this->realm); |
||
| 166 | } |
||
| 167 | $response = $radius->accessRequest($username, $password); |
||
| 168 | |||
| 169 | if ($response !== false) { |
||
| 170 | break; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($response === false) { |
||
| 175 | $errorCode = $radius->getErrorCode(); |
||
| 176 | switch ($errorCode) { |
||
| 177 | case $radius::TYPE_ACCESS_REJECT: |
||
| 178 | Logger::warning('ldapRadius: Radius authentication failed.'); |
||
| 179 | throw new Error\Error('WRONGUSERPASS'); |
||
| 180 | case $radius::TYPE_ACCESS_CHALLENGE: |
||
| 181 | throw new Exception('Radius authentication error: Challenge requested, but not supported.'); |
||
| 182 | default: |
||
| 183 | throw new Exception(sprintf( |
||
| 184 | 'Error during radius authentication; %s (%d)', |
||
| 185 | $radius->getErrorMessage(), |
||
| 186 | $errorCode, |
||
| 187 | )); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | Logger::info('ldapRadius: Radius authentication succeeded.'); |
||
| 192 | |||
| 193 | // If we get this far, we have a valid login |
||
| 194 | $attributes = []; |
||
| 195 | if ($this->usernameAttribute !== null) { |
||
| 196 | $attributes[$this->usernameAttribute] = [$username]; |
||
| 197 | } |
||
| 198 | |||
| 199 | if ($this->vendor === null) { |
||
| 200 | /* |
||
| 201 | * We aren't interested in any vendor-specific attributes. We are |
||
| 202 | * therefore done now. |
||
| 203 | */ |
||
| 204 | return $attributes; |
||
| 205 | } else { |
||
| 206 | foreach ($radius->getReceivedAttributes() as $content) { |
||
| 207 | if ($content[0] == 26) { // is a Vendor-Specific attribute |
||
| 208 | $vsa = $radius->decodeVendorSpecificContent($content[1]); |
||
| 209 | |||
| 210 | // matches configured Vendor and Type |
||
| 211 | if ($vsa[0][0] === $this->vendor && $vsa[0][1] === $this->vendorType) { |
||
| 212 | // SAML attributes expected in a URN=value, so split at first = |
||
| 213 | $decomposed = explode("=", $vsa[0][2], 2); |
||
| 214 | $attributes[$decomposed[0]][] = $decomposed[1]; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | return array_merge($attributes, $this->getAttributes($radius)); |
||
| 221 | } |
||
| 275 |