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