| Conditions | 19 |
| Paths | 72 |
| Total Lines | 139 |
| Code Lines | 72 |
| 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 |
||
| 63 | public function acs() |
||
| 64 | { |
||
| 65 | /** @var Auth $auth */ |
||
| 66 | $auth = Injector::inst()->get(SAMLHelper::class)->getSAMLAuth(); |
||
| 67 | $caughtException = null; |
||
| 68 | |||
| 69 | // Log both errors (reported by php-saml and thrown as exception) with a common ID for later tracking |
||
| 70 | $uniqueErrorId = uniqid('SAML-'); |
||
| 71 | |||
| 72 | // Force php-saml module to use the current absolute base URL (e.g. https://www.example.com/saml). This avoids |
||
| 73 | // errors that we otherwise get when having a multi-directory ACS URL like /saml/acs). |
||
| 74 | // See https://github.com/onelogin/php-saml/issues/249 |
||
| 75 | Utils::setBaseURL(Controller::join_links($auth->getSettings()->getSPData()['entityId'], 'saml')); |
||
| 76 | |||
| 77 | // Attempt to process the SAML response. If there are errors during this, log them and redirect to the generic |
||
| 78 | // error page. Note: This does not necessarily include all SAML errors (e.g. we still need to confirm if the |
||
| 79 | // user is authenticated after this block |
||
| 80 | try { |
||
| 81 | $auth->processResponse(); |
||
| 82 | $error = $auth->getLastErrorReason(); |
||
| 83 | } catch (Exception $e) { |
||
| 84 | $caughtException = $e; |
||
| 85 | } |
||
| 86 | |||
| 87 | // If there was an issue with the SAML response, if it was missing or if the SAML response indicates that they |
||
| 88 | // aren't authorised, then log the issue and provide a traceable error back to the user via the login form |
||
| 89 | if ( |
||
| 90 | $caughtException || |
||
| 91 | !empty($error) || |
||
| 92 | !$auth->isAuthenticated() || |
||
| 93 | $this->checkForReplayAttack($auth, $uniqueErrorId) |
||
| 94 | ) { |
||
| 95 | if ($caughtException instanceof Exception) { |
||
| 96 | $this->getLogger()->error(sprintf( |
||
| 97 | '[%s] [code: %s] %s (%s:%s)', |
||
| 98 | $uniqueErrorId, |
||
| 99 | $e->getCode(), |
||
|
|
|||
| 100 | $e->getMessage(), |
||
| 101 | $e->getFile(), |
||
| 102 | $e->getLine() |
||
| 103 | )); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!empty($error)) { |
||
| 107 | $this->getLogger()->error(sprintf('[%s] %s', $uniqueErrorId, $error)); |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->getForm()->sessionMessage( |
||
| 111 | _t( |
||
| 112 | 'SilverStripe\\SAML\\Control\\SAMLController.ERR_SAML_ACS_FAILURE', |
||
| 113 | 'Unfortunately we couldn\'t log you in. If this continues, please contact your I.T. department' |
||
| 114 | . ' with the following reference: {ref}', |
||
| 115 | ['ref' => $uniqueErrorId] |
||
| 116 | ), |
||
| 117 | ValidationResult::TYPE_ERROR |
||
| 118 | ); |
||
| 119 | |||
| 120 | // Redirect the user back to the login form to display the generic error message and reference |
||
| 121 | $this->getRequest()->getSession()->save($this->getRequest()); |
||
| 122 | return $this->redirect('Security/login'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * If processing reaches here, then the user is authenticated - the rest of this method is just processing their |
||
| 127 | * legitimate information and configuring their account. |
||
| 128 | */ |
||
| 129 | |||
| 130 | // If we expect the NameID to be a binary version of the GUID (ADFS), check that it actually is |
||
| 131 | // If we are configured not to expect a binary NameID, then we assume it is a direct GUID (Azure AD) |
||
| 132 | if (Config::inst()->get(SAMLConfiguration::class, 'expect_binary_nameid')) { |
||
| 133 | $decodedNameId = base64_decode($auth->getNameId()); |
||
| 134 | if (ctype_print($decodedNameId)) { |
||
| 135 | $this->getForm()->sessionMessage('NameID from IdP is not a binary GUID.', ValidationResult::TYPE_ERROR); |
||
| 136 | $this->getRequest()->getSession()->save($this->getRequest()); |
||
| 137 | return $this->getRedirect(); |
||
| 138 | } |
||
| 139 | |||
| 140 | // transform the NameId to guid |
||
| 141 | $helper = SAMLHelper::singleton(); |
||
| 142 | $guid = $helper->binToStrGuid($decodedNameId); |
||
| 143 | if (!$helper->validGuid($guid)) { |
||
| 144 | $errorMessage = "Not a valid GUID '{$guid}' recieved from server."; |
||
| 145 | $this->getLogger()->error($errorMessage); |
||
| 146 | $this->getForm()->sessionMessage($errorMessage, ValidationResult::TYPE_ERROR); |
||
| 147 | $this->getRequest()->getSession()->save($this->getRequest()); |
||
| 148 | return $this->getRedirect(); |
||
| 149 | } |
||
| 150 | } else { |
||
| 151 | $guid = $auth->getNameId(); |
||
| 152 | } |
||
| 153 | |||
| 154 | $attributes = $auth->getAttributes(); |
||
| 155 | |||
| 156 | $fieldToClaimMap = array_flip(Member::config()->claims_field_mappings); |
||
| 157 | |||
| 158 | // Write a rudimentary member with basic fields on every login, so that we at least have something |
||
| 159 | // if there is no further sync (e.g. via LDAP) |
||
| 160 | $member = Member::get()->filter('GUID', $guid)->limit(1)->first(); |
||
| 161 | if (!($member && $member->exists()) && Config::inst()->get(SAMLConfiguration::class, 'allow_insecure_email_linking') && isset($fieldToClaimMap['Email'])) { |
||
| 162 | // If there is no member found via GUID and we allow linking via email, search by email |
||
| 163 | $member = Member::get()->filter('Email', $attributes[$fieldToClaimMap['Email']])->limit(1)->first(); |
||
| 164 | |||
| 165 | if (!($member && $member->exists())) { |
||
| 166 | $member = new Member(); |
||
| 167 | } |
||
| 168 | |||
| 169 | $member->GUID = $guid; |
||
| 170 | } |
||
| 171 | |||
| 172 | foreach ($member->config()->claims_field_mappings as $claim => $field) { |
||
| 173 | if (!isset($attributes[$claim][0])) { |
||
| 174 | $this->getLogger()->warning( |
||
| 175 | sprintf( |
||
| 176 | 'Claim rule \'%s\' configured in SAMLMemberExtension.claims_field_mappings, ' . |
||
| 177 | 'but wasn\'t passed through. Please check IdP claim rules.', |
||
| 178 | $claim |
||
| 179 | ) |
||
| 180 | ); |
||
| 181 | |||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | $member->$field = $attributes[$claim][0]; |
||
| 186 | } |
||
| 187 | |||
| 188 | $member->SAMLSessionIndex = $auth->getSessionIndex(); |
||
| 189 | |||
| 190 | // This will trigger LDAP update through LDAPMemberExtension::memberLoggedIn, if the LDAP module is installed. |
||
| 191 | // The LDAP update will also write the Member record a second time, but the member *must* be written before |
||
| 192 | // IdentityStore->logIn() is called, otherwise the identity store throws an exception. |
||
| 193 | // Both SAML and LDAP identify Members by the same GUID field. |
||
| 194 | $member->write(); |
||
| 195 | |||
| 196 | /** @var IdentityStore $identityStore */ |
||
| 197 | $identityStore = Injector::inst()->get(IdentityStore::class); |
||
| 198 | $persistent = Security::config()->get('autologin_enabled'); |
||
| 199 | $identityStore->logIn($member, $persistent, $this->getRequest()); |
||
| 200 | |||
| 201 | return $this->getRedirect(); |
||
| 202 | } |
||
| 324 |