-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAttributeServer.php
More file actions
165 lines (139 loc) · 5.71 KB
/
AttributeServer.php
File metadata and controls
165 lines (139 loc) · 5.71 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
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\exampleattributeserver\Controller;
use SAML2\Assertion;
use SAML2\AttributeQuery;
use SAML2\Binding;
use SAML2\Constants;
use SAML2\HTTPPost;
use SAML2\Response;
use SAML2\XML\saml\Issuer;
use SAML2\XML\saml\SubjectConfirmation;
use SAML2\XML\saml\SubjectConfirmationData;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Logger;
use SimpleSAML\Metadata\MetaDataStorageHandler;
use SimpleSAML\Module\saml\Message;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller class for the exampleattributeserver module.
*
* This class serves the attribute server available in the module.
*
* @package SimpleSAML\Module\exampleattributeserver
*/
class AttributeServer
{
/** @var \SimpleSAML\Configuration */
protected Configuration $config;
/** @var \SimpleSAML\Metadata\MetaDataStorageHandler|null */
protected ?MetaDataStorageHandler $metadataHandler = null;
/**
* ConfigController constructor.
*
* @param \SimpleSAML\Configuration $config The configuration to use.
*/
public function __construct(Configuration $config)
{
$this->config = $config;
}
/**
* Inject the \SimpleSAML\Metadata\MetaDataStorageHandler dependency.
*
* @param \SimpleSAML\Metadata\MetaDataStorageHandler $handler
*/
public function setMetadataStorageHandler(MetaDataStorageHandler $handler): void
{
$this->metadataHandler = $handler;
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
*
* @return \SimpleSAML\HTTP\RunnableResponse
* @throws \SimpleSAML\Error\BadRequest
*/
public function main(/** @scrutinizer ignore-unused */ Request $request): RunnableResponse
{
$binding = Binding::getCurrentBinding();
$query = $binding->receive();
if (!($query instanceof AttributeQuery)) {
throw new Error\BadRequest('Invalid message received to AttributeQuery endpoint.');
}
$idpEntityId = $this->metadataHandler->getMetaDataCurrentEntityID('saml20-idp-hosted');
$issuer = $query->getIssuer();
if ($issuer === null) {
throw new Error\BadRequest('Missing <saml:Issuer> in <samlp:AttributeQuery>.');
} else {
$spEntityId = $issuer->getValue();
if ($spEntityId === '') {
throw new Error\BadRequest('Empty <saml:Issuer> in <samlp:AttributeQuery>.');
}
}
$idpMetadata = $this->metadataHandler->getMetaDataConfig($idpEntityId, 'saml20-idp-hosted');
$spMetadata = $this->metadataHandler->getMetaDataConfig($spEntityId, 'saml20-sp-remote');
// The endpoint we should deliver the message to
$endpoint = $spMetadata->getString('testAttributeEndpoint');
// The attributes we will return
$attributes = [
'name' => ['value1', 'value2', 'value3'],
'test' => ['test'],
];
// The name format of the attributes
$attributeNameFormat = Constants::NAMEFORMAT_UNSPECIFIED;
// Determine which attributes we will return
$returnAttributes = array_keys($query->getAttributes());
if (count($returnAttributes) === 0) {
Logger::debug('No attributes requested - return all attributes.');
$returnAttributes = $attributes;
} elseif ($query->getAttributeNameFormat() !== $attributeNameFormat) {
Logger::debug('Requested attributes with wrong NameFormat - no attributes returned.');
$returnAttributes = [];
} else {
/** @var array<mixed>$values */
foreach ($returnAttributes as $name => $values) {
if (!array_key_exists($name, $attributes)) {
// We don't have this attribute
unset($returnAttributes[$name]);
continue;
}
if (count($values) === 0) {
// Return all attributes
$returnAttributes[$name] = $attributes[$name];
continue;
}
// Filter which attribute values we should return
$returnAttributes[$name] = array_intersect($values, $attributes[$name]);
}
}
// $returnAttributes contains the attributes we should return. Send them
$issuer = new Issuer();
$issuer->setValue($idpEntityId);
$assertion = new Assertion();
$assertion->setIssuer($issuer);
$assertion->setNameId($query->getNameId());
$assertion->setNotBefore(time());
$assertion->setNotOnOrAfter(time() + 300); // 60*5 = 5min
$assertion->setValidAudiences([$spEntityId]);
$assertion->setAttributes($returnAttributes);
$assertion->setAttributeNameFormat($attributeNameFormat);
$sc = new SubjectConfirmation();
$sc->setMethod(Constants::CM_BEARER);
$scd = new SubjectConfirmationData();
$scd->setNotOnOrAfter(time() + 300); // 60*5 = 5min
$scd->setRecipient($endpoint);
$scd->setInResponseTo($query->getId());
$sc->setSubjectConfirmationData($scd);
$assertion->setSubjectConfirmation([$sc]);
Message::addSign($idpMetadata, $spMetadata, $assertion);
$response = new Response();
$response->setRelayState($query->getRelayState());
$response->setDestination($endpoint);
$response->setIssuer($issuer);
$response->setInResponseTo($query->getId());
$response->setAssertions([$assertion]);
Message::addSign($idpMetadata, $spMetadata, $response);
return new RunnableResponse([new HTTPPost(), 'send'], [$response]);
}
}