Completed
Push — master ( e8c6a9...13221c )
by Robbie
13s queued 10s
created

src/Control/SAMLController.php (1 issue)

1
<?php
2
3
namespace SilverStripe\SAML\Control;
4
5
use Exception;
6
use function gmmktime;
7
use OneLogin\Saml2\Auth;
8
use OneLogin\Saml2\Constants;
9
use OneLogin\Saml2\Utils;
10
use OneLogin\Saml2\Error;
11
use Psr\Log\LoggerInterface;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\SAML\Authenticators\SAMLAuthenticator;
15
use SilverStripe\SAML\Authenticators\SAMLLoginForm;
16
use SilverStripe\SAML\Helpers\SAMLHelper;
17
use SilverStripe\Control\Controller;
18
use SilverStripe\Control\Director;
19
use SilverStripe\Control\HTTPResponse;
20
use SilverStripe\Core\Injector\Injector;
21
use SilverStripe\SAML\Model\SAMLResponse;
22
use SilverStripe\SAML\Services\SAMLConfiguration;
23
use SilverStripe\Security\IdentityStore;
24
use SilverStripe\Security\Member;
25
use SilverStripe\Security\Security;
26
use function uniqid;
27
28
/**
29
 * Class SAMLController
30
 *
31
 * This controller handles serving metadata requests for the identity provider (IdP), as well as handling the creation
32
 * of new users and logging them into SilverStripe after being authenticated at the IdP.
33
 */
34
class SAMLController extends Controller
35
{
36
    /**
37
     * @var array
38
     */
39
    private static $allowed_actions = [
40
        'index',
41
        'acs',
42
        'metadata'
43
    ];
44
45
    public function index()
46
    {
47
        return $this->redirect('/');
48
    }
49
50
    /**
51
     * Assertion Consumer Service
52
     *
53
     * The user gets sent back here after authenticating with the IdP, off-site.
54
     * The earlier redirection to the IdP can be found in the SAMLAuthenticator::authenticate.
55
     *
56
     * After this handler completes, we end up with a rudimentary Member record (which will be created on-the-fly
57
     * if not existent), with the user already logged in. Login triggers memberLoggedIn hooks, which allows
58
     * LDAP side of this module to finish off loading Member data.
59
     *
60
     * @throws Error
61
     * @throws \Psr\Container\NotFoundExceptionInterface
62
     */
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(),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
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
        $helper = SAMLHelper::singleton();
127
128
        // If we expect the NameID to be a binary version of the GUID (ADFS), check that it actually is
129
        // If we are configured not to expect a binary NameID, then we assume it is a direct GUID (Azure AD)
130
        if (Config::inst()->get(SAMLConfiguration::class, 'expect_binary_nameid')) {
131
            $decodedNameId = base64_decode($auth->getNameId());
132
            if (ctype_print($decodedNameId)) {
133
                $this->getForm()->sessionMessage('NameID from IdP is not a binary GUID.', ValidationResult::TYPE_ERROR);
134
                $this->getRequest()->getSession()->save($this->getRequest());
135
                return $this->getRedirect();
136
            }
137
138
            // transform the NameId to guid
139
            $guid = $helper->binToStrGuid($decodedNameId);
140
        } else {
141
            $guid = $auth->getNameId();
142
        }
143
144
        if (!$helper->validGuid($guid)) {
145
            $errorMessage = "Not a valid GUID '{$guid}' received from server.";
146
            $this->getLogger()->error($errorMessage);
147
            $this->getForm()->sessionMessage($errorMessage, ValidationResult::TYPE_ERROR);
148
            $this->getRequest()->getSession()->save($this->getRequest());
149
            return $this->getRedirect();
150
        }
151
152
        $this->extend('updateGuid', $guid);
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())
162
            && Config::inst()->get(SAMLConfiguration::class, 'allow_insecure_email_linking')
163
            && isset($fieldToClaimMap['Email'])
164
        ) {
165
            // If there is no member found via GUID and we allow linking via email, search by email
166
            $member = Member::get()->filter('Email', $attributes[$fieldToClaimMap['Email']])->limit(1)->first();
167
168
            if (!($member && $member->exists())) {
169
                $member = new Member();
170
            }
171
172
            $member->GUID = $guid;
173
        } elseif (!($member && $member->exists())) {
174
            // If the member doesn't exist and we don't allow linking via email, then create a new member
175
            $member = new Member();
176
            $member->GUID = $guid;
177
        }
178
179
        foreach ($member->config()->claims_field_mappings as $claim => $field) {
180
            if (!isset($attributes[$claim][0])) {
181
                $this->getLogger()->warning(
182
                    sprintf(
183
                        'Claim rule \'%s\' configured in SAMLMemberExtension.claims_field_mappings, ' .
184
                                'but wasn\'t passed through. Please check IdP claim rules.',
185
                        $claim
186
                    )
187
                );
188
189
                continue;
190
            }
191
192
            $member->$field = $attributes[$claim][0];
193
        }
194
195
        $member->SAMLSessionIndex = $auth->getSessionIndex();
196
197
        // This will trigger LDAP update through LDAPMemberExtension::memberLoggedIn, if the LDAP module is installed.
198
        // The LDAP update will also write the Member record a second time, but the member *must* be written before
199
        // IdentityStore->logIn() is called, otherwise the identity store throws an exception.
200
        // Both SAML and LDAP identify Members by the same GUID field.
201
        $member->write();
202
203
        /** @var IdentityStore $identityStore */
204
        $identityStore = Injector::inst()->get(IdentityStore::class);
205
        $identityStore->logIn($member, false, $this->getRequest());
206
207
        return $this->getRedirect();
208
    }
209
210
    /**
211
     * Generate this SP's metadata. This is needed for intialising the SP-IdP relationship.
212
     * IdP is instructed to call us back here to establish the relationship. IdP may also be configured
213
     * to hit this endpoint periodically during normal operation, to check the SP availability.
214
     */
215
    public function metadata()
216
    {
217
        try {
218
            /** @var Auth $auth */
219
            $auth = Injector::inst()->get(SAMLHelper::class)->getSAMLAuth();
220
            $settings = $auth->getSettings();
221
            $metadata = $settings->getSPMetadata();
222
            $errors = $settings->validateMetadata($metadata);
223
            if (empty($errors)) {
224
                header('Content-Type: text/xml');
225
                echo $metadata;
226
            } else {
227
                throw new Error(
228
                    'Invalid SP metadata: ' . implode(', ', $errors),
229
                    Error::METADATA_SP_INVALID
230
                );
231
            }
232
        } catch (Exception $e) {
233
            $this->getLogger()->error($e->getMessage());
234
            echo $e->getMessage();
235
        }
236
    }
237
238
    /**
239
     * @return HTTPResponse
240
     */
241
    protected function getRedirect()
242
    {
243
        // Absolute redirection URLs may cause spoofing
244
        if ($this->getRequest()->getSession()->get('BackURL')
245
            && Director::is_site_url($this->getRequest()->getSession()->get('BackURL'))) {
246
            return $this->redirect($this->getRequest()->getSession()->get('BackURL'));
247
        }
248
249
        // Spoofing attack, redirect to homepage instead of spoofing url
250
        if ($this->getRequest()->getSession()->get('BackURL')
251
            && !Director::is_site_url($this->getRequest()->getSession()->get('BackURL'))) {
252
            return $this->redirect(Director::absoluteBaseURL());
253
        }
254
255
        // If a default login dest has been set, redirect to that.
256
        if (Security::config()->default_login_dest) {
257
            return $this->redirect(Director::absoluteBaseURL() . Security::config()->default_login_dest);
258
        }
259
260
        // fallback to redirect back to home page
261
        return $this->redirect(Director::absoluteBaseURL());
262
    }
263
264
    /**
265
     * If processing reaches here, then the user is authenticated but potentially not valid. We first need to confirm
266
     * that they are not an attacker performing a SAML replay attack (capturing the raw traffic from a compromised
267
     * device and then re-submitting the same SAML response).
268
     *
269
     * To combat this, we store SAML response IDs for the amount of time they're valid for (plus a configurable offset
270
     * to account for potential time skew), and if the ID has been seen before we log an error message and return true
271
     * (which indicates that this specific request is a replay attack).
272
     *
273
     * If no replay attack is detected, then the SAML response is logged so that future requests can be blocked.
274
     *
275
     * @param Auth $auth The Auth object that includes the processed response
276
     * @param string $uniqueErrorId The error code to use when logging error messages for this given error
277
     * @return bool true if this response is a replay attack, false if it's the first time we've seen the ID
278
     */
279
    protected function checkForReplayAttack(Auth $auth, $uniqueErrorId = '')
280
    {
281
        $responseId = $auth->getLastMessageId();
282
        $expiry = $auth->getLastAssertionNotOnOrAfter(); // Note: Expiry will always be stored and returned in UTC
283
284
        // Search for any SAMLResponse objects where the response ID is the same and the expiry is within the range
285
        $count = SAMLResponse::get()->filter(['ResponseID' => $responseId])->count();
286
287
        if ($count > 0) {
288
            // Response found, therefore this is a replay attack - log the error and return false so the user is denied
289
            $this->getLogger()->error(sprintf(
290
                '[%s] SAML replay attack detected! Response ID "%s", expires "%s", client IP "%s"',
291
                $uniqueErrorId,
292
                $responseId,
293
                $expiry,
294
                $this->getRequest()->getIP()
295
            ));
296
297
            return true;
298
        } else {
299
            // No attack detected, log the SAML response
300
            $response = new SAMLResponse([
301
                'ResponseID' => $responseId,
302
                'Expiry' => $expiry
303
            ]);
304
305
            $response->write();
306
            return false;
307
        }
308
    }
309
310
    /**
311
     * Get a logger
312
     *
313
     * @return LoggerInterface
314
     */
315
    public function getLogger()
316
    {
317
        return Injector::inst()->get(LoggerInterface::class);
318
    }
319
320
    /**
321
     * Gets the login form
322
     *
323
     * @return SAMLLoginForm
324
     */
325
    public function getForm()
326
    {
327
        return Injector::inst()->get(SAMLLoginForm::class, true, [$this, SAMLAuthenticator::class, 'LoginForm']);
328
    }
329
}
330