Skip to content

Commit a6b7e04

Browse files
committed
Refactor SP template and filed handling
1 parent b432118 commit a6b7e04

6 files changed

Lines changed: 336 additions & 210 deletions

File tree

‎docker/sspwww/sp-fields.inc‎

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4+
// The fields of the login form of the Test SP (sp.php)
5+
//
6+
// Each field is described once here. This description is used to:
7+
// - normalise the request parameters (normaliseParams()), so the rest of the code gets valid values
8+
// - render the field (renderField() in sp-utils.inc)
9+
// What a field does in the AuthnRequest is in sp-login.inc
10+
//
11+
// Field properties:
12+
// - type: 'select', 'text' or 'checkbox'. The value of a checked checkbox is 'true', of an unchecked one ''
13+
// - label: The label in front of the field
14+
// - help: Hover help text of the label (optional)
15+
// - options: select: value => display name
16+
// - default: The value when the parameter is missing. For a select with a default, an unknown value also
17+
// becomes the default. A select without a default keeps any value (e.g. idp: SimpleSAMLphp checks it)
18+
// - datalist: text: suggested values (optional)
19+
// - list: text: use the datalist of this other field (optional)
20+
// - id: HTML id of the field (optional, defaults to the field name)
21+
//
22+
// Requires $gIDPmap from sp-config.inc
23+
24+
/** The signature algorithms for the AuthnRequest: 'signing' value => algorithm identifier ('' is not signed) */
25+
const SP_SIGNING_ALGORITHMS = array(
26+
'none' => '',
27+
'rsa-sha1' => 'http://www.w3.org/2000/09/xmldsig#rsa-sha1',
28+
'rsa-sha256' => 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256',
29+
'rsa-sha384' => 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384',
30+
'rsa-sha512' => 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512',
31+
);
32+
33+
const SP_BINDING_REDIRECT = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect';
34+
const SP_BINDING_POST = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST';
35+
36+
$gFormFields = array(
37+
'idp' => array(
38+
'type' => 'select',
39+
'label' => 'Identity Provider:',
40+
'help' => 'Select the IdP to authenticate to',
41+
'options' => array_map(static fn (array $idp): string => $idp['name'], $gIDPmap),
42+
),
43+
'loa' => array(
44+
'type' => 'select',
45+
'label' => 'Request LOA:',
46+
'help' => 'Select the level of assurance to include in the AuthnContextClassRef in the AuthnRequest. The value depends on the selected IdP and the selected LoA and is only added when the IdP supports it.',
47+
'options' => array('' => 'None', '1' => '1', '1.5' => '1.5', '2' => '2', '3' => '3'),
48+
'default' => '',
49+
),
50+
'sp' => array(
51+
'type' => 'select',
52+
'label' => 'Service Provider EntityID:',
53+
'help' => 'Select the EntityID to use for this SP. This value goes into the Issuer in the AuthnRequest.',
54+
'options' => array('default-sp' => 'default-sp', 'second-sp' => 'second-sp', 'third-sp' => 'third-sp', 'fourth-sp' => 'fourth-sp'),
55+
'default' => 'default-sp',
56+
),
57+
'signing' => array(
58+
'type' => 'select',
59+
'label' => 'Signing:',
60+
'help' => "Select the signing algorithm the SP must use to sign the AuthnRequest, select 'none' to disable signing.",
61+
'options' => array_combine(array_keys(SP_SIGNING_ALGORITHMS), array_keys(SP_SIGNING_ALGORITHMS)),
62+
'default' => 'rsa-sha256',
63+
),
64+
'key' => array(
65+
'type' => 'select',
66+
'label' => 'Signing key:',
67+
'help' => 'Select the key the SP must use for signing the AuthnRequest. The default key is the key that is present in the SP metadata.',
68+
'options' => array('default' => 'default', 'secondary' => 'secondary'),
69+
'default' => 'default',
70+
),
71+
'nameidpolicy' => array(
72+
'type' => 'select',
73+
'label' => 'NameIDPolicy:',
74+
'help' => "Select the NameID policy that the SP should request from the IdP. This value is put in the Format attribute of the NameIDPolicy in the AuthnRequest. 'None' uses the simpleSAMLphp default behaviour based on the SP and IdP metadata.",
75+
'options' => array(
76+
'' => 'None',
77+
'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified' => 'unspecified',
78+
'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent' => 'persistent',
79+
'urn:oasis:names:tc:SAML:2.0:nameid-format:transient' => 'transient',
80+
),
81+
'default' => '',
82+
),
83+
'ssobinding' => array(
84+
'type' => 'select',
85+
'label' => 'SSO Binding:',
86+
'help' => 'Specify the SAML Binding that the SP must use to send the AuthnRequest to the IdP.',
87+
'options' => array(SP_BINDING_REDIRECT => 'HTTP-Redirect', SP_BINDING_POST => 'HTTP-POST'),
88+
'default' => SP_BINDING_REDIRECT,
89+
),
90+
'requesterid' => array(
91+
'type' => 'text',
92+
'label' => 'Scoping (RequesterID) 1:',
93+
'help' => 'Specify up to two SP EntityIDs to put in RequesterID Scoping elements in the AuthnRequest. If left blank no elements are added.',
94+
'datalist' => array('http://localhost/simplesaml/module.php/saml/sp/metadata.php/pieter-local-test-sp'),
95+
),
96+
'requesterid2' => array(
97+
'type' => 'text',
98+
'label' => '2:',
99+
'list' => 'requesterid',
100+
),
101+
'scopingIDP' => array(
102+
'type' => 'text',
103+
'id' => 'scoping_idp',
104+
'label' => 'Scoping (IDPList) 1:',
105+
'help' => 'Specify up to two IdP EntityIDs to put in IDPList Scoping elements in the AuthnRequest. If left blank no elements are added.',
106+
'datalist' => array('https://pieter.aai.surfnet.nl/simplesamlphp/saml2/idp/metadata.php', 'https://idp.surfnet.nl'),
107+
),
108+
'scopingIDP2' => array(
109+
'type' => 'text',
110+
'label' => '2:',
111+
'list' => 'scopingIDP',
112+
),
113+
'subject' => array(
114+
'type' => 'text',
115+
'label' => 'Subject:',
116+
'help' => "Specify the value of a NameID to put in a Subject element in the AuthnRequest. If specified a NameID of type 'urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified' is added to the AuthnRequest.",
117+
'datalist' => array(
118+
'urn:collab:person:dev.openconext.local:admin',
119+
'urn:collab:person:institution-a.example.com:jane-a1',
120+
'urn:collab:person:institution-a.example.com:joe-a1',
121+
'urn:collab:person:institution-a.example.com:',
122+
'urn:collab:person:institution-b.example.com:',
123+
'urn:collab:person:institution-c.example.com:',
124+
'urn:collab:person:institution-d.example.com:',
125+
'urn:collab:person:Institution-D.EXAMPLE.COM:',
126+
'urn:collab:person:institution-e.example.com:',
127+
'urn:collab:person:institution-f.example.com:',
128+
),
129+
),
130+
'forceauthn' => array(
131+
'type' => 'checkbox',
132+
'label' => 'Force authentication:',
133+
'help' => 'If selected ForceAuthn is set to true in the AuthnRequest.',
134+
),
135+
'acsurl' => array(
136+
'type' => 'text',
137+
'label' => 'AssertionConsumerServiceURL:',
138+
// The help text and datalist need the URLs of SimpleSAMLphp: sp.php sets them
139+
'help' => 'Optionally override the AssertionConsumerServiceURL to specify in the AuthnRequest. Leave blank to use the default ACS location that is generated by SimpleSAMLphp.',
140+
'datalist' => array(),
141+
),
142+
'emulateadfs' => array(
143+
'type' => 'checkbox',
144+
'label' => 'Emulate SFO ADFS extension:',
145+
'help' => "If selected two extra POST variables 'AuthMethod' and 'Context' are added to the AuthnRequest and the HTTP-POST binding is used. Note: Request LOA and Subject should be specified as well to make a valid SFO request to the Stepup-Gateway",
146+
),
147+
'email_extension' => array(
148+
'type' => 'text',
149+
'label' => 'Email Userinfo extension:',
150+
'help' => 'Specify an email address. If left blank the email extension is not added.',
151+
),
152+
'sho_extension' => array(
153+
'type' => 'text',
154+
'label' => 'SHO Userinfo extension:',
155+
'help' => 'Specify a domain. If left blank the schacHomeOrganization extension is not added.',
156+
'datalist' => array(
157+
'institution-a.example.com',
158+
'institution-b.example.com',
159+
'institution-c.example.com',
160+
'institution-d.example.com',
161+
'Institution-D.EXAMPLE.COM',
162+
'institution-e.example.com',
163+
'institution-f.example.com',
164+
),
165+
),
166+
'mdui_displayname' => array(
167+
'type' => 'text',
168+
'label' => 'Service name (mdui:DisplayName) extension:',
169+
'help' => 'Specify a service name. If left blank the mdui:UIInfo extension is not added.',
170+
),
171+
);
172+
173+
174+
/**
175+
* The values of the form fields from request parameters: one string value for every field in $fields.
176+
* Missing parameters get the default, unknown select values the default (when the select has one).
177+
* Parameters that are not form fields (e.g. 'action') are not included.
178+
*/
179+
function normaliseParams(array $request, array $fields): array
180+
{
181+
$params = array();
182+
foreach ($fields as $name => $field) {
183+
$value = $request[$name] ?? null;
184+
if (!is_string($value)) {
185+
$value = null; // Missing, or not a single value (e.g. name[]=...)
186+
}
187+
switch ($field['type']) {
188+
case 'checkbox':
189+
$value = $value === 'true' ? 'true' : '';
190+
break;
191+
case 'select':
192+
if (array_key_exists('default', $field) && ($value === null || !array_key_exists($value, $field['options']))) {
193+
$value = $field['default'];
194+
}
195+
break;
196+
}
197+
$params[$name] = $value ?? ($field['default'] ?? '');
198+
}
199+
return $params;
200+
}

‎docker/sspwww/sp-login.inc‎

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,30 @@
1717
* this SP requests the IdP to use for sending back the SAML Response to us)
1818
* * $GLOBALS['gSP_SSOBinding'] -- The binding to use for sending the authentication request to the IdP
1919
*
20-
* Unknown values fall back to the defaults: signing with rsa-sha256, the default key and the HTTP-Redirect binding.
20+
* @param array $params The parameters of the login form, normalised by normaliseParams()
2121
*/
2222
function applySpGlobals(array $params): void
2323
{
24-
$algorithms = array(
25-
'none' => '',
26-
'rsa-sha1' => 'http://www.w3.org/2000/09/xmldsig#rsa-sha1',
27-
'rsa-sha256' => 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256',
28-
'rsa-sha384' => 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384',
29-
'rsa-sha512' => 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512',
30-
);
31-
$signing = $params['signing'] ?? 'rsa-sha256';
32-
if (!isset($algorithms[$signing])) {
33-
$signing = 'rsa-sha256';
34-
}
35-
$GLOBALS['gSP_redirect_sign'] = $signing !== 'none';
36-
$GLOBALS['gSP_signature_algorithm'] = $algorithms[$signing];
37-
38-
$GLOBALS['gSP_secondary_key'] = ($params['key'] ?? '') === 'secondary';
39-
40-
$GLOBALS['gSP_ProtocolBinding'] = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST';
41-
42-
$bindings = array(
43-
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
44-
'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
45-
);
46-
$ssoBinding = $params['ssobinding'] ?? '';
24+
$GLOBALS['gSP_signature_algorithm'] = SP_SIGNING_ALGORITHMS[$params['signing']];
25+
$GLOBALS['gSP_redirect_sign'] = $GLOBALS['gSP_signature_algorithm'] !== '';
26+
$GLOBALS['gSP_secondary_key'] = $params['key'] === 'secondary';
27+
$GLOBALS['gSP_ProtocolBinding'] = SP_BINDING_POST;
4728
// The SURF MFA extension for ADFS posts its request (see buildLoginContext())
48-
if (($params['emulateadfs'] ?? '') === 'true') {
49-
$ssoBinding = 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST';
50-
}
51-
$GLOBALS['gSP_SSOBinding'] = in_array($ssoBinding, $bindings, true)
52-
? $ssoBinding
53-
: 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect';
29+
$GLOBALS['gSP_SSOBinding'] = $params['emulateadfs'] === 'true' ? SP_BINDING_POST : $params['ssobinding'];
5430
}
5531

5632

57-
/** Whether a (string) request parameter is set and not empty */
33+
/** Whether a form field has a value */
5834
function paramIsSet(array $params, string $name): bool
5935
{
60-
return isset($params[$name]) && strlen($params[$name]) > 0;
36+
return ($params[$name] ?? '') !== '';
6137
}
6238

6339

6440
/**
6541
* Build the state array for SimpleSAML\Auth\Simple::login() from the parameters of the login form
6642
*
67-
* @param array $params The parameters of the login form
43+
* @param array $params The parameters of the login form, normalised by normaliseParams()
6844
* @param array $idpMap $gIDPmap from sp-config.inc
6945
* @param string $returnURL Where SimpleSAMLphp sends the browser after the login
7046
* @param string $sp The authsource (i.e. the SP) that is used for the login
@@ -74,18 +50,18 @@ function buildLoginContext(array $params, array $idpMap, string $returnURL, stri
7450
{
7551
$context = array(
7652
'ReturnTo' => $returnURL,
77-
'ForceAuthn' => ($params['forceauthn'] ?? '') === 'true',
53+
'ForceAuthn' => $params['forceauthn'] === 'true',
7854
'saml:NameIDPolicy' => null,
7955
);
8056

8157
// IdP
82-
if (isset($params['idp'])) {
58+
if (paramIsSet($params, 'idp')) {
8359
$context['saml:idp'] = $params['idp'];
8460
}
8561

8662
// LOA
8763
// An IdP can map a LOA to '' (e.g. the local IdP): then no AuthnContextClassRef is requested
88-
$loa = $idpMap[$params['idp'] ?? '']['loa'][$params['loa'] ?? ''] ?? '';
64+
$loa = $idpMap[$params['idp']]['loa'][$params['loa']] ?? '';
8965
if (strlen($loa) > 0) {
9066
$context['saml:AuthnContextClassRef'] = $loa;
9167
}
@@ -140,7 +116,7 @@ function buildLoginContext(array $params, array $idpMap, string $returnURL, stri
140116
// must be used to validate the response as that will rename the "_SAMLResponse" POST variable back to "SAMLResponse" so that
141117
// SimpleSAMLphp can then process it normally.
142118
// For more information See: https://github.com/OpenConext/Stepup-Gateway/blob/main/docs/SFO.md#adfs-mfa-extension
143-
if (($params['emulateadfs'] ?? '') === 'true') {
119+
if ($params['emulateadfs'] === 'true') {
144120
// Set AuthMethod and mock Context like the SURF MFA extension for ADFS would do
145121
$context['debugsp:extraPOSTvars'] = array(
146122
'AuthMethod' => 'ADFS.SCSA',
@@ -229,16 +205,13 @@ function buildExtensionChunks(array $params): array
229205

230206

231207
/**
232-
* Link that logs in directly with the given form parameters: the non-empty parameters, sorted, with action=login
208+
* Link that logs in directly with the given form parameters: action=login and the non-empty parameters, sorted
233209
*/
234210
function buildDirectLoginLink(array $params, string $pageURL): string
235211
{
236-
$query = array();
212+
$query = array('action=login');
237213
foreach ($params as $name => $value) {
238-
if (is_string($value) && strlen($value) > 0) {
239-
if ($name === 'action') {
240-
$value = 'login';
241-
}
214+
if ($value !== '') {
242215
$query[] = urlencode($name) . '=' . urlencode($value);
243216
}
244217
}

0 commit comments

Comments
 (0)