Skip to content

Commit cfbc090

Browse files
committed
Fix SF-Request to PSR-7 conversion
1 parent 8f082de commit cfbc090

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@
3636
"require": {
3737
"php": "^8.1",
3838

39+
"nyholm/psr7": "~1.8.2",
3940
"simplesamlphp/saml2": "~5.0.2",
4041
"simplesamlphp/simplesamlphp": "~2.4.0",
41-
"symfony/http-foundation": "~6.4.0"
42+
"symfony/http-foundation": "~6.4.0",
43+
"symfony/psr-http-message-bridge": "~6.4.0"
4244
},
4345
"require-dev": {
46+
"beste/clock": "~3.0.0",
4447
"simplesamlphp/simplesamlphp-test-framework": "~1.9.3",
4548
"simplesamlphp/xml-security": "~1.13.0"
4649
},

src/Controller/AttributeServer.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\Module\exampleattributeserver\Controller;
66

77
use DateInterval;
8+
use Nyholm\Psr7\Factory\Psr17Factory;
89
use SimpleSAML\{Configuration, Error, Logger};
910
use SimpleSAML\HTTP\RunnableResponse;
1011
use SimpleSAML\Metadata\MetaDataStorageHandler;
@@ -29,6 +30,7 @@
2930
};
3031
use SimpleSAML\SAML2\XML\samlp\{AttributeQuery, Response};
3132
use SimpleSAML\XML\Utils\Random;
33+
use Symfony\Bridge\PsrHttpMessage\Factory\{HttpFoundationFactory, PsrHttpFactory};
3234
use Symfony\Component\HttpFoundation\Request;
3335

3436
/**
@@ -77,19 +79,23 @@ public function setMetadataStorageHandler(MetaDataStorageHandler $handler): void
7779
*/
7880
public function main(/** @scrutinizer ignore-unused */ Request $request): RunnableResponse
7981
{
80-
$binding = Binding::getCurrentBinding();
81-
$query = $binding->receive();
82-
if (!($query instanceof AttributeQuery)) {
82+
$psr17Factory = new Psr17Factory();
83+
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
84+
$psrRequest = $psrHttpFactory->createRequest($request);
85+
86+
$binding = Binding::getCurrentBinding($psrRequest);
87+
$message = $binding->receive($psrRequest);
88+
if (!($message instanceof AttributeQuery)) {
8389
throw new Error\BadRequest('Invalid message received to AttributeQuery endpoint.');
8490
}
8591

8692
$idpEntityId = $this->metadataHandler->getMetaDataCurrentEntityID('saml20-idp-hosted');
8793

88-
$issuer = $query->getIssuer();
94+
$issuer = $message->getIssuer();
8995
if ($issuer === null) {
9096
throw new Error\BadRequest('Missing <saml:Issuer> in <samlp:AttributeQuery>.');
9197
} else {
92-
$spEntityId = $issuer->getValue();
98+
$spEntityId = $issuer->getContent();
9399
if ($spEntityId === '') {
94100
throw new Error\BadRequest('Empty <saml:Issuer> in <samlp:AttributeQuery>.');
95101
}
@@ -130,7 +136,7 @@ public function main(/** @scrutinizer ignore-unused */ Request $request): Runnab
130136
Logger::debug('No attributes requested - return all attributes.');
131137
$returnAttributes = $attributes;
132138
} else {
133-
foreach ($query->getAttributes() as $reqAttr) {
139+
foreach ($message->getAttributes() as $reqAttr) {
134140
foreach ($attributes as $attr) {
135141
if ($attr->getName() === $reqAttr->getName() && $attr->getNameFormat() === $reqAttr->getNameFormat()) {
136142
// The requested attribute is available
@@ -158,15 +164,15 @@ public function main(/** @scrutinizer ignore-unused */ Request $request): Runnab
158164
issuer: new Issuer($idpEntityId),
159165
issueInstant: $clock->now(),
160166
id: (new Random())->generateID(),
161-
subject: Subject(
162-
identifier: $query->getNameID(),
167+
subject: new Subject(
168+
identifier: $message->getSubject()->getIdentifier(),
163169
subjectConfirmation: [
164170
new SubjectConfirmation(
165171
method: C::CM_BEARER,
166172
subjectConfirmationData: new SubjectConfirmationData(
167173
notOnOrAfter: $clock->now()->add(new DateInterval('PT300S')),
168174
recipient: $endpoint,
169-
inResponseTo: $query->getId(),
175+
inResponseTo: $message->getId(),
170176
),
171177
),
172178
],
@@ -196,7 +202,7 @@ public function main(/** @scrutinizer ignore-unused */ Request $request): Runnab
196202
issuer: new Issuer($issuer),
197203
id: (new Random())->generateID(),
198204
version: '2.0',
199-
inResponseTo: $query->getId(),
205+
inResponseTo: $message->getId(),
200206
destination: $endpoint,
201207
assertions: [$assertion],
202208
);

tests/bootstrap.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
declare(strict_types=1);
44

5+
use Beste\Clock\LocalizedClock;
6+
use SimpleSAML\SAML2\Compat\ContainerSingleton;
7+
use SimpleSAML\SAML2\Compat\MockContainer;
8+
59
$projectRoot = dirname(__DIR__);
610
require_once($projectRoot . '/vendor/autoload.php');
711

@@ -11,3 +15,11 @@
1115
echo "Linking '$linkPath' to '$projectRoot'\n";
1216
symlink($projectRoot, $linkPath);
1317
}
18+
19+
// Load the system clock
20+
$systemClock = LocalizedClock::in(new DateTimeZone('Z'));
21+
22+
// And set the Mock container as the Container to use.
23+
$container = new MockContainer();
24+
$container->setClock($systemClock);
25+
ContainerSingleton::setContainer($container);

tests/src/Controller/AttributeServerTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function setUpBeforeClass(): void
3434

3535
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
3636
$_SERVER['REQUEST_METHOD'] = 'GET';
37-
$_GET = ['SAMLRequest' => 'rZNRa8IwFIXfB%2FsPIe9Nm7SaNWhBEEFwg%2BHYw95ie%2Bsy2rQkKei%2FX9o6kQ1EhoQQOLnn3I8bMrOyrlqxcM6oXefgtQNzRIe60lYMV3PcGS0aaZUVWtZghcvFdvG8EYxEojWNa%2FKmwheW6w5pLRinGo3RejnHUpYspuk0oJzHAaM0DhKeyKAESpOEMrnjDKN3MNZb5tgneJ%2B1Hay1dVI7L0WUB1EaRNM3SkWSCsY%2FcPb4gNCspxFDtUGrxtTSXWfrFVUE5VAqQDvljjj7dK61IgzhIOu2AtKYfTgLL7Ivmm273RfkblB%2BtBeful7%2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gLL7Ivmm273RfkblB%2BtBeful7%2BA6DTtoVclQoKnPW2E4GwY5cTxJg%2FQoR%2FKEaG8%2FuivvoEoQpBSUymhJLE70nKYn%2F0i%2BOh7hZk6ZN79QxtFEYro0AX1XFsNgyygtqfFoc3UDEy8UTJXSGsvrE1nZKnJOrHQGMeR6SfSZzelaU1UIIxUGyk3ndyDwPa%2BHi%2F%2F2L2DQ%3D%3D&RelayState=something';
4040

@@ -53,13 +53,11 @@ public static function setUpBeforeClass(): void
5353
public function testMain(): void
5454
{
5555
$_SERVER['REQUEST_URI'] = '/module.php/exampleattributeserver/attributeserver';
56-
$request = Request::create(
57-
'/module.php/exampleattributeserver/attributeserver',
58-
'GET',
59-
);
56+
$_SERVER['HTTP_HOST'] = 'example.org';
57+
$request = Request::createFromGlobals();
6058

6159
$mdh = $this->createMock(MetaDataStorageHandler::class);
62-
$mdh->method('getMetaDataCurrentEntityID')->willReturn('entityID');
60+
$mdh->method('getMetaDataCurrentEntityID')->willReturn('https://example.org/');
6361
$mdh->method('getMetaDataConfig')->willReturn(Configuration::loadFromArray([
6462
'EntityID' => 'auth_source_id',
6563
'testAttributeEndpoint' => 'test',

0 commit comments

Comments
 (0)