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