Skip to content

Commit 257bf6d

Browse files
committed
Handle SAML error response while having a SP session
1 parent a6b7e04 commit 257bf6d

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎docker/sspwww/sp-template.php‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
<body>
3232
<h1>simpleSAMLphp Test SP</h1>
3333

34+
<?php if ($view['samlResponseIsError']): ?>
35+
<?php /* The IdP returned a SAML error response to the last login. Shown first: it is the result of what the user did last */ ?>
36+
<h3 class="login-failed">Last login failed</h3>
37+
<p>The Identity Provider returned a SAML error response. The XML of the response is shown at the end of the page.</p>
38+
<h4>SAML Error Status</h4>
39+
<?php SAMLResponseFieldsToTable($samlResponse, array('ID', 'InResponseTo', 'IssueInstant', 'Destination', 'Issuer', 'StatusCode', 'SubStatusCode', 'StatusMessage')) ?>
40+
<?php if ($auth !== null): ?>
41+
<p id="earlier_login">You are still logged in to SP <strong><?= e($view['pageSP']) ?></strong> from an earlier login, at <?= e($auth['authnInstant']) ?>. The information below is from that login.</p>
42+
<?php endif ?>
43+
<?php endif ?>
44+
3445
<?php if ($auth !== null): ?>
3546
<label>You are logged in to SP:</label><code><?= e($view['pageSP']) ?></code><br />
3647
<label>IdP EntityID:</label><code><?= e($auth['idpEntityID']) ?></code><br />
@@ -39,7 +50,7 @@
3950
<p><button class="logout" type="submit" name="action" value="logout" title="Log out of <?= e($view['pageSP']) ?>. Note that this does not log you out of any Identity Provider.">Logout</button></p>
4051
</form>
4152

42-
<h3>Session</h3>
53+
<h3>SP Session (<?= e($view['pageSP']) ?>)<?= $view['samlResponseIsError'] ? ' from an earlier login, still active' : '' ?></h3>
4354
<p>
4455
<?php if ($auth['sessionIndex'] !== ''): ?>
4556
<label>SessionIndex:</label><code style="background-color: <?= HTMLColorFingerprint($auth['sessionIndex']) ?>"><?= e($auth['sessionIndex']) ?></code><br />
@@ -87,12 +98,6 @@
8798
<button id="login_2" class="login" type="submit" name="action" value="login" title="Log in to the selected Service Provider with the selected Identity Provider (again)">Login</button>&nbsp;&nbsp;<button type="submit" name="action" value="reset">Reset</button><br />
8899
</p>
89100

90-
<?php if ($view['samlResponseIsError']): ?>
91-
<h3>SAML Error Status</h3>
92-
<?php SAMLResponseFieldsToTable($samlResponse, array('ID', 'InResponseTo', 'IssueInstant', 'Destination', 'Issuer', 'StatusCode', 'SubStatusCode', 'StatusMessage')) ?>
93-
<?php /* The XML of the response is shown at the end of the page */ ?>
94-
<?php endif ?>
95-
96101
<h3>Advanced options</h3>
97102
<?php if ($view['directLoginLink'] !== null): ?>
98103
<p>

‎docker/sspwww/sp.css‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ p,div {margin: 10px; clear: both}
1111
button { border-radius: 4px; font-weight: bold; font-size: 14px; background-color: lightgrey; color: black; border: 1px solid black; padding: 5px 10px}
1212
button.login {background-color: darkseagreen}
1313
button.logout {background-color: indianred}
14+
h3.login-failed {background-color: lightcoral}
1415
select {min-width: 150px; font-size: 14px;}
1516
table.saml-fields td code, table.saml-fields td small {float: none; vertical-align: baseline; margin-top: 0}
1617
table.saml-fields td code {max-width: none}

‎tests/functional/EntraMockIdpTest.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ public function testCancelReturnsSamlErrorToSp(): void
6262
self::assertCount(0, $page->filter('#saml-xml-verbatim'));
6363
}
6464

65+
/** A failed login does not end the SP session of an earlier login: the page shows the error first, then the earlier login */
66+
public function testCancelWhileLoggedIn(): void
67+
{
68+
$this->loginViaSp(['idp' => self::LOCAL_IDP], 'jane-a1');
69+
$page = $this->loginViaSp(['idp' => self::ENTRA_IDP], 'jane-a2', cancel: true);
70+
71+
// The error comes first on the page
72+
self::assertSame('Last login failed', trim($page->filter('h3')->first()->text()), $this->browser->trail());
73+
self::assertSame('AuthnFailed', self::lastPart(self::tableValue($page, 'SAML Error Status', 'SubStatusCode')));
74+
75+
// Still logged in, from the login at the local IdP
76+
self::assertStringContainsString('You are still logged in to SP default-sp from an earlier login', $page->filter('#earlier_login')->text());
77+
self::assertSame('default-sp', self::labelValue($page, 'You are logged in to SP:'));
78+
self::assertSame(self::LOCAL_IDP, self::labelValue($page, 'IdP EntityID:'));
79+
self::assertCount(1, $page->filterXPath("//h3[normalize-space(.)='SP Session (default-sp) from an earlier login, still active']"));
80+
81+
// Once the error is shown, the page is back to normal
82+
$page = $this->browser->get(self::SP_PATH);
83+
self::assertCount(0, $page->filter('#earlier_login'));
84+
self::assertCount(1, $page->filterXPath("//h3[normalize-space(.)='SP Session (default-sp)']"));
85+
}
86+
6587
public function testCancelAtLocalIdp(): void
6688
{
6789
$page = $this->loginViaSp(['idp' => self::LOCAL_IDP], 'jane-a1', cancel: true);

0 commit comments

Comments
 (0)