| Conditions | 21 |
| Paths | 180 |
| Total Lines | 138 |
| Code Lines | 71 |
| 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 | $hasError = $caughtException || !empty($error); |
||
| 90 | if ($hasError || !$auth->isAuthenticated() || $this->checkForReplayAttack($auth, $uniqueErrorId)) { |
||
| 91 | if ($caughtException instanceof Exception) { |
||
| 92 | $this->getLogger()->error(sprintf( |
||
| 93 | '[%s] [code: %s] %s (%s:%s)', |
||
| 94 | $uniqueErrorId, |
||
| 95 | $e->getCode(), |
||
|
|
|||
| 96 | $e->getMessage(), |
||
| 97 | $e->getFile(), |
||
| 98 | $e->getLine() |
||
| 99 | )); |
||
| 100 | } |
||
| 101 | |||
| 102 | if (!empty($error)) { |
||
| 103 | $this->getLogger()->error(sprintf('[%s] %s', $uniqueErrorId, $error)); |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->getForm()->sessionMessage( |
||
| 107 | _t( |
||
| 108 | 'SilverStripe\\SAML\\Control\\SAMLController.ERR_SAML_ACS_FAILURE', |
||
| 109 | 'Unfortunately we couldn\'t log you in. If this continues, please contact your I.T. department' |
||
| 110 | . ' with the following reference: {ref}', |
||
| 111 | ['ref' => $uniqueErrorId] |
||
| 112 | ), |
||
| 113 | ValidationResult::TYPE_ERROR |
||
| 114 | ); |
||
| 115 | |||
| 116 | // Redirect the user back to the login form to display the generic error message and reference |
||
| 117 | $this->getRequest()->getSession()->save($this->getRequest()); |
||
| 118 | return $this->redirect('Security/login'); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * If processing reaches here, then the user is authenticated - the rest of this method is just processing their |
||
| 123 | * legitimate information and configuring their account. |
||
| 124 | */ |
||
| 125 | |||
| 126 | // If we expect the NameID to be a binary version of the GUID (ADFS), check that it actually is |
||
| 127 | // If we are configured not to expect a binary NameID, then we assume it is a direct GUID (Azure AD) |
||
| 128 | if (Config::inst()->get(SAMLConfiguration::class, 'expect_binary_nameid')) { |
||
| 129 | $decodedNameId = base64_decode($auth->getNameId()); |
||
| 130 | if (ctype_print($decodedNameId)) { |
||
| 131 | $this->getForm()->sessionMessage('NameID from IdP is not a binary GUID.', ValidationResult::TYPE_ERROR); |
||
| 132 | $this->getRequest()->getSession()->save($this->getRequest()); |
||
| 133 | return $this->getRedirect(); |
||
| 134 | } |
||
| 135 | |||
| 136 | // transform the NameId to guid |
||
| 137 | $helper = SAMLHelper::singleton(); |
||
| 138 | $guid = $helper->binToStrGuid($decodedNameId); |
||
| 139 | if (!$helper->validGuid($guid)) { |
||
| 140 | $errorMessage = "Not a valid GUID '{$guid}' recieved from server."; |
||
| 141 | $this->getLogger()->error($errorMessage); |
||
| 142 | $this->getForm()->sessionMessage($errorMessage, ValidationResult::TYPE_ERROR); |
||
| 143 | $this->getRequest()->getSession()->save($this->getRequest()); |
||
| 144 | return $this->getRedirect(); |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | $guid = $auth->getNameId(); |
||
| 148 | } |
||
| 149 | |||
| 150 | $attributes = $auth->getAttributes(); |
||
| 151 | |||
| 152 | $fieldToClaimMap = array_flip(Member::config()->claims_field_mappings); |
||
| 153 | |||
| 154 | // Write a rudimentary member with basic fields on every login, so that we at least have something |
||
| 155 | // if there is no further sync (e.g. via LDAP) |
||
| 156 | $member = Member::get()->filter('GUID', $guid)->limit(1)->first(); |
||
| 157 | if (!($member && $member->exists()) && Config::inst()->get(SAMLConfiguration::class, 'allow_insecure_email_linking') && isset($fieldToClaimMap['Email'])) { |
||
| 158 | // If there is no member found via GUID and we allow linking via email, search by email |
||
| 159 | $member = Member::get()->filter('Email', $attributes[$fieldToClaimMap['Email']])->limit(1)->first(); |
||
| 160 | |||
| 161 | if (!($member && $member->exists())) { |
||
| 162 | $member = new Member(); |
||
| 163 | } |
||
| 164 | |||
| 165 | $member->GUID = $guid; |
||
| 166 | } elseif (!($member && $member->exists())) { |
||
| 167 | // If the member doesn't exist and we don't allow linking via email, then create a new member |
||
| 168 | $member = new Member(); |
||
| 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 | $identityStore->logIn($member, false, $this->getRequest()); |
||
| 199 | |||
| 200 | return $this->getRedirect(); |
||
| 201 | } |
||
| 323 |