| Conditions | 8 |
| Paths | 14 |
| Total Lines | 73 |
| Code Lines | 39 |
| 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 |
||
| 98 | public function main(Request $request): RunnableResponse |
||
| 99 | { |
||
| 100 | // load SimpleSAMLphp configuration |
||
| 101 | $ssp_cf = $this->config::getInstance(); |
||
|
|
|||
| 102 | |||
| 103 | // load Auth MemCookie configuration |
||
| 104 | $amc_cf = AuthMemCookie::getInstance(); |
||
| 105 | |||
| 106 | $sourceId = $amc_cf->getAuthSource(); |
||
| 107 | $simple = $this->auth_simple; |
||
| 108 | $s = new $simple($sourceId); |
||
| 109 | |||
| 110 | // check if the user is authorized. We attempt to authenticate the user if not |
||
| 111 | $s->requireAuth(); |
||
| 112 | |||
| 113 | // generate session id and save it in a cookie |
||
| 114 | $sessionID = Utils\Random::generateID(); |
||
| 115 | $cookieName = $amc_cf->getCookieName(); |
||
| 116 | $this->http_utils::setCookie($cookieName, $sessionID); |
||
| 117 | |||
| 118 | // generate the authentication information |
||
| 119 | $attributes = $s->getAttributes(); |
||
| 120 | |||
| 121 | $authData = []; |
||
| 122 | |||
| 123 | // username |
||
| 124 | $usernameAttr = $amc_cf->getUsernameAttr(); |
||
| 125 | if ($usernameAttr === null || !array_key_exists($usernameAttr, $attributes)) { |
||
| 126 | throw new Error\Exception( |
||
| 127 | "The user doesn't have an attribute named '" . $usernameAttr . |
||
| 128 | "'. This attribute is expected to contain the username." |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | $authData['UserName'] = $attributes[$usernameAttr]; |
||
| 132 | |||
| 133 | // groups |
||
| 134 | $groupsAttr = $amc_cf->getGroupsAttr(); |
||
| 135 | if ($groupsAttr !== null) { |
||
| 136 | if (!array_key_exists($groupsAttr, $attributes)) { |
||
| 137 | throw new Error\Exception( |
||
| 138 | "The user doesn't have an attribute named '" . $groupsAttr . |
||
| 139 | "'. This attribute is expected to contain the groups the user is a member of." |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | $authData['Groups'] = $attributes[$groupsAttr]; |
||
| 143 | } else { |
||
| 144 | $authData['Groups'] = []; |
||
| 145 | } |
||
| 146 | |||
| 147 | $authData['RemoteIP'] = $request->server->get('REMOTE_ADDR'); |
||
| 148 | |||
| 149 | foreach ($attributes as $n => $v) { |
||
| 150 | $authData['ATTR_' . $n] = $v; |
||
| 151 | } |
||
| 152 | |||
| 153 | // store the authentication data in the memcache server |
||
| 154 | $data = ''; |
||
| 155 | foreach ($authData as $n => $v) { |
||
| 156 | if (is_array($v)) { |
||
| 157 | $v = implode(':', $v); |
||
| 158 | } |
||
| 159 | $data .= $n . '=' . $v . "\r\n"; |
||
| 160 | } |
||
| 161 | |||
| 162 | $memcache = $amc_cf->getMemcache(); |
||
| 163 | $expirationTime = $s->getAuthData('Expire'); |
||
| 164 | $memcache->set($sessionID, $data, $expirationTime ?? 0); |
||
| 165 | |||
| 166 | // register logout handler |
||
| 167 | $this->session->registerLogoutHandler($sourceId, '\SimpleSAML\Module\memcookie\AuthMemCookie', 'logoutHandler'); |
||
| 168 | |||
| 169 | // redirect the user back to this page to signal that the login is completed |
||
| 170 | return new RunnableResponse([$this->http_utils, 'redirectTrustedURL'], [$this->http_utils::getSelfURL()]); |
||
| 171 | } |
||
| 173 |