forked from OpenConextApps/simplesamlphp-module-stepupsfo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFO.php
More file actions
201 lines (162 loc) · 5.9 KB
/
SFO.php
File metadata and controls
201 lines (162 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\stepupsfo\Auth\Process;
use Exception;
use SAML2\AuthnRequest;
use SAML2\Binding;
use SAML2\Constants as C;
use SAML2\XML\saml\NameID;
use SimpleSAML\Auth;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Logger;
use SimpleSAML\Metadata\MetaDataStorageHandler;
use SimpleSAML\Module;
use SimpleSAML\Module\saml\Message;
use function array_key_exists;
use function in_array;
use function sprintf;
use function substr;
use function var_export;
/**
* @package SimpleSAMLphp
*/
class SFO extends Auth\ProcessingFilter
{
/** @var \SimpleSAML\Configuration */
private Configuration $metadata;
/** @var \SimpleSAML\Configuration */
private Configuration $idpMetadata;
/** @var array */
private array $config = [];
/** @var string */
private string $subjectidattribute;
/** @var array */
private array $skipentities = [];
/**
* Initialize this filter.
*
* @param array $config Configuration information about this filter.
* @param mixed $reserved For future use.
*/
public function __construct(array $config, $reserved)
{
parent::__construct($config, $reserved);
$this->subjectidattribute = $config['subjectattribute'];
if (isset($config['skipentities'])) {
$this->skipentities = $config['skipentities'];
}
$this->idpMetadata = $this->getIdPMetadata($config['idpEntityid']);
$config['entityid'] = $config['entityID'];
$this->config = $config;
}
/**
* Process an authentication response.
*
* @param array $state The state of the response.
*/
public function process(array &$state): void
{
foreach ($this->skipentities as $skip) {
if ($skip === $state['SPMetadata']['entityid'] || in_array($skip, $state['saml:RequesterID'], true)) {
Logger::info('SFO - skipping SFO for entity ' . var_export($skip, true));
return;
}
}
if (array_key_exists('loa', $state['Attributes'])) {
// LOA is present in User's attributes
$loa = $state['Attributes']['loa'];
} else if (array_key_exists('loa', $this->config)) {
// LOA set in SFO config is default.
$loa = $this->config['loa'];
} else {
throw new Exception("SFO - No LOA set.");
}
if (empty($loa)) {
// LOA is set to an empty string, skip SFO
return;
}
Logger::info('SFO - requested LOA: ' . $loa);
$this->config['AuthnContextClassRef'] = $loa;
$this->metadata = Configuration::loadFromArray($this->config);
$state['sfo:sp:metadata'] = $this->metadata;
$state['sfo:idp:entityid'] = $this->idpMetadata->getString('entityid');
$samlstateid = Auth\State::saveState($state, 'stepupsfo:pre');
if (empty($state['Attributes'][$this->subjectidattribute])) {
throw new Exception("Subjectid " . $this->subjectidattribute . " not found in attributes.");
}
$subjectid = $state['Attributes'][$this->subjectidattribute][0];
if (substr($subjectid, 0, 18) !== 'urn:collab:person:') {
throw new Exception(sprintf(
"Subjectid %s does not start with urn:collab:person:",
var_export($subjectid, true)
));
}
$nameid = new NameID();
$nameid->setValue($subjectid);
// Start the authentication request
$this->startSFO($this->idpMetadata, $nameid, $samlstateid);
}
/**
* Retrieve the metadata of an IdP.
*
* @param string $entityId The entity id of the IdP.
* @return \SimpleSAML\Configuration The metadata of the IdP.
*/
public function getIdPMetadata(string $entityId): Configuration
{
$metadataHandler = MetaDataStorageHandler::getMetadataHandler();
try {
return $metadataHandler->getMetaDataConfig($entityId, 'saml20-idp-remote');
} catch (Exception $e) {
/* Metadata wasn't found. */
Logger::debug('getIdpMetadata: ' . $e->getMessage());
}
/* Not found. */
throw new Error\Exception(sprintf(
'Could not find the metadata of an IdP with entity ID %s',
var_export($entityId, true)
));
}
/**
* Send a SAML2 SSO request to the SFO IdP.
*
* @param \SimpleSAML\Configuration $idpMetadata The metadata of the IdP.
* @param \SAML2\XML\saml\NameID $nameid The unspecified NameID of the principal to perform SFO for.
* @param string $relay RelayState to pass
*/
private function startSFO(Configuration $idpMetadata, NameID $nameid, $relay): void
{
$ar = Message::buildAuthnRequest($this->metadata, $idpMetadata);
$ar->setAssertionConsumerServiceURL(Module::getModuleURL('stepupsfo/acs.php'));
$ar->setNameId($nameid);
$ar->setRelayState($relay);
Logger::debug(sprintf(
'Sending SAML 2 SFO AuthnRequest for %s to %s with id %s',
$nameid->getValue(),
var_export($idpMetadata->getString('entityid'), true),
$ar->getId()
));
$dst = $idpMetadata->getEndpointPrioritizedByBinding(
'SingleSignOnService',
[C::BINDING_HTTP_REDIRECT]
);
$ar->setDestination($dst['Location']);
$b = Binding::getBinding($dst['Binding']);
$this->sendSAML2AuthnRequest($b, $ar);
assert(false);
}
/**
* Function to actually send the authentication request.
*
* This function does not return.
*
* @param \SAML2\Binding $binding The binding.
* @param \SAML2\AuthnRequest $ar The authentication request.
*/
private function sendSAML2AuthnRequest(Binding $binding, AuthnRequest $ar): void
{
$binding->send($ar);
assert(false);
}
}