Skip to content

Commit 75e28c5

Browse files
committed
Migrate samlp:NameIDPolicy to new interface
1 parent 7534f52 commit 75e28c5

4 files changed

Lines changed: 13 additions & 76 deletions

File tree

modules/saml/src/IdP/SAML2.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,8 @@ public static function receiveAuthnRequest(Request $request, IdP $idp): Response
434434
$authnContext = $request->getRequestedAuthnContext();
435435

436436
$nameIdPolicy = $request->getNameIdPolicy();
437-
if (isset($nameIdPolicy['Format'])) {
438-
$nameIDFormat = $nameIdPolicy['Format'];
439-
} else {
440-
$nameIDFormat = null;
441-
}
442-
if (isset($nameIdPolicy['AllowCreate'])) {
443-
$allowCreate = $nameIdPolicy['AllowCreate'];
444-
} else {
445-
$allowCreate = false;
446-
}
437+
$nameIDFormat = $nameIdPolicy->getFormat();
438+
$allowCreate = $nameIdPolicy->getAllowCreate() ?? false;
447439

448440
$idpInit = false;
449441

modules/saml/src/Message.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,7 @@ public static function buildAuthnRequest(
485485
}
486486

487487
$policy = Utils\Config\Metadata::parseNameIdPolicy($nameIdPolicy);
488-
// empty array signals not to set any NameIdPolicy element
489-
if ($policy !== []) {
490-
$ar->setNameIdPolicy($policy);
491-
}
488+
$ar->setNameIdPolicy($policy);
492489

493490
$ar->setForceAuthn($spMetadata->getOptionalBoolean('ForceAuthn', false));
494491
$ar->setIsPassive($spMetadata->getOptionalBoolean('IsPassive', false));

src/SimpleSAML/Utils/Config/Metadata.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use SimpleSAML\{Configuration, Logger};
88
use SimpleSAML\SAML2\Constants as C;
99
use SimpleSAML\SAML2\XML\md\ContactPerson;
10+
use SimpleSAML\SAML2\XML\samlp\NameIDPolicy;
1011

1112
use function in_array;
1213

@@ -129,16 +130,16 @@ public static function isHiddenFromDiscovery(array $metadata): bool
129130
/**
130131
* This method parses the different possible values of the NameIDPolicy metadata configuration.
131132
*/
132-
public static function parseNameIdPolicy(array $nameIdPolicy = null): array
133+
public static function parseNameIdPolicy(array $nameIdPolicy = null): ?NameIDPolicy
133134
{
134135
if ($nameIdPolicy === null) {
135136
// when NameIDPolicy is unset or set to null, default to transient
136-
return ['Format' => C::NAMEID_TRANSIENT, 'AllowCreate' => true];
137+
return NameIDPolicy::fromArray(['Format' => C::NAMEID_TRANSIENT, 'AllowCreate' => true]);
137138
}
138139

139140
if ($nameIdPolicy === []) {
140141
// empty array means not to send any NameIDPolicy element
141-
return [];
142+
return null;
142143
}
143144

144145
// handle configurations specifying an array in the NameIDPolicy config option
@@ -152,6 +153,6 @@ public static function parseNameIdPolicy(array $nameIdPolicy = null): array
152153
$policy['SPNameQualifier'] = $spNameQualifier;
153154
}
154155

155-
return $policy;
156+
return NameIDPolicy::fromArray($policy);
156157
}
157158
}

tests/src/SimpleSAML/Utils/Config/MetadataTest.php

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\TestCase;
1010
use SimpleSAML\SAML2\Constants as C;
1111
use SimpleSAML\SAML2\XML\md\ContactPerson;
12+
use SimpleSAML\SAML2\XML\samlp\NameIDPolicy;
1213
use SimpleSAML\Utils\Config\Metadata;
1314
use TypeError;
1415

@@ -60,72 +61,18 @@ public function testIsHiddenFromDiscovery(): void
6061

6162

6263
/**
63-
* Test \SimpleSAML\Utils\Config\Metadata::parseNameIdPolicy().
64-
* Set to specific arrays.
64+
* @covers \SimpleSAML\Utils\Config\Metadata::parseNameIdPolicy
6565
*/
6666
public function testParseNameIdPolicy(): void
6767
{
68-
$nameIdPolicy = [
69-
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:persistent',
70-
'AllowCreate' => false
71-
];
72-
$this->assertEquals([
73-
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:persistent',
74-
'AllowCreate' => false
75-
], Metadata::parseNameIdPolicy($nameIdPolicy));
68+
$this->assertNull(Metadata::parseNameIdPolicy([]));
69+
$this->assertInstanceOf(NameIDPolicy::class, Metadata::parseNameIdPolicy());
7670

7771
$nameIdPolicy = [
7872
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:persistent',
7973
'AllowCreate' => false,
8074
'SPNameQualifier' => 'TEST'
8175
];
82-
$this->assertEquals([
83-
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:persistent',
84-
'AllowCreate' => false,
85-
'SPNameQualifier' => 'TEST'
86-
], Metadata::parseNameIdPolicy($nameIdPolicy));
87-
}
88-
89-
/**
90-
* Test \SimpleSAML\Utils\Config\Metadata::parseNameIdPolicy().
91-
* Test with settings that produce the fallback defaults.
92-
*/
93-
public function testParseNameIdPolicyDefaults(): void
94-
{
95-
// Test null or unset
96-
$nameIdPolicy = null;
97-
$this->assertEquals([
98-
'Format' => C::NAMEID_TRANSIENT,
99-
'AllowCreate' => true
100-
], Metadata::parseNameIdPolicy($nameIdPolicy));
101-
102-
$nameIdPolicy = [
103-
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:persistent',
104-
];
105-
$this->assertEquals([
106-
'Format' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:persistent',
107-
'AllowCreate' => true
108-
], Metadata::parseNameIdPolicy($nameIdPolicy));
109-
110-
$nameIdPolicy = [
111-
'AllowCreate' => false,
112-
];
113-
$this->assertEquals([
114-
'Format' => C::NAMEID_TRANSIENT,
115-
'AllowCreate' => false
116-
], Metadata::parseNameIdPolicy($nameIdPolicy));
117-
}
118-
119-
/**
120-
* Test \SimpleSAML\Utils\Config\Metadata::parseNameIdPolicy().
121-
* Test with setting to empty array (meaning to not send any NameIdPolicy).
122-
*/
123-
public function testParseNameIdPolicyEmpty(): void
124-
{
125-
$nameIdPolicy = [];
126-
$this->assertEquals(
127-
[],
128-
Metadata::parseNameIdPolicy($nameIdPolicy)
129-
);
76+
$this->assertInstanceOf(NameIDPolicy::class, Metadata::parseNameIdPolicy($nameIdPolicy));
13077
}
13178
}

0 commit comments

Comments
 (0)