Skip to content

Commit 635d64d

Browse files
author
Tim Trinidad
committed
allow the getSPMetadata() method to always include the encryption KeyDescriptor
1 parent 539a3eb commit 635d64d

2 files changed

Lines changed: 68 additions & 19 deletions

File tree

lib/Saml2/Settings.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,11 +800,15 @@ public function shouldCompressResponses()
800800
/**
801801
* Gets the SP metadata. The XML representation.
802802
*
803+
* @param bool $alwaysPublishEncryptionCert When 'true', the returned metadata
804+
* will always include an 'encryption' KeyDescriptor. Otherwise, the 'encryption'
805+
* KeyDescriptor will only be included if $advancedSettings['security']['wantNameIdEncrypted']
806+
* or $advancedSettings['security']['wantAssertionsEncrypted'] are enabled.
803807
* @return string SP metadata (xml)
804808
* @throws Exception
805809
* @throws OneLogin_Saml2_Error
806810
*/
807-
public function getSPMetadata()
811+
public function getSPMetadata($alwaysPublishEncryptionCert = false)
808812
{
809813
$metadata = OneLogin_Saml2_Metadata::builder($this->_sp, $this->_security['authnRequestsSigned'], $this->_security['wantAssertionsSigned'], null, null, $this->getContacts(), $this->getOrganization());
810814

@@ -813,7 +817,7 @@ public function getSPMetadata()
813817
$metadata = OneLogin_Saml2_Metadata::addX509KeyDescriptors(
814818
$metadata,
815819
$certNew,
816-
$this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
820+
$alwaysPublishEncryptionCert || $this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
817821
);
818822
}
819823

@@ -822,7 +826,7 @@ public function getSPMetadata()
822826
$metadata = OneLogin_Saml2_Metadata::addX509KeyDescriptors(
823827
$metadata,
824828
$cert,
825-
$this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
829+
$alwaysPublishEncryptionCert || $this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
826830
);
827831
}
828832

tests/src/OneLogin/Saml2/SettingsTest.php

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -422,33 +422,78 @@ public function testGetSPMetadata()
422422
* Case with x509certNew
423423
*
424424
* @covers OneLogin_Saml2_Settings::getSPMetadata
425+
* @dataProvider testGetSPMetadataWithX509CertNewDataProvider
425426
*/
426-
public function testGetSPMetadataWithX509CertNew()
427+
public function testGetSPMetadataWithX509CertNew($alwaysIncludeEncryption, $wantNameIdEncrypted, $wantAssertionsEncrypted, $expectEncryptionKeyDescriptor)
427428
{
428429
$settingsDir = TEST_ROOT .'/settings/';
429430
include $settingsDir.'settings5.php';
430431

431-
$settingsInfo['security']['wantNameIdEncrypted'] = false;
432-
$settingsInfo['security']['wantAssertionsEncrypted'] = false;
432+
$settingsInfo['security']['wantNameIdEncrypted'] = $wantNameIdEncrypted;
433+
$settingsInfo['security']['wantAssertionsEncrypted'] = $wantAssertionsEncrypted;
433434
$settings = new OneLogin_Saml2_Settings($settingsInfo);
434-
$metadata = $settings->getSPMetadata();
435+
$metadata = $settings->getSPMetadata($alwaysIncludeEncryption);
435436

436-
$this->assertEquals(2, substr_count($metadata, "<md:KeyDescriptor"));
437+
$this->assertEquals($expectEncryptionKeyDescriptor ? 4 : 2, substr_count($metadata, "<md:KeyDescriptor"));
437438

439+
// signing KeyDescriptor should always be included
438440
$this->assertEquals(2, substr_count($metadata, '<md:KeyDescriptor use="signing"'));
439441

440-
$this->assertEquals(0, substr_count($metadata, '<md:KeyDescriptor use="encryption"'));
441-
442-
$settingsInfo['security']['wantNameIdEncrypted'] = true;
443-
$settingsInfo['security']['wantAssertionsEncrypted'] = true;
444-
$settings2 = new OneLogin_Saml2_Settings($settingsInfo);
445-
$metadata2 = $settings2->getSPMetadata();
446-
447-
$this->assertEquals(4, substr_count($metadata2, "<md:KeyDescriptor"));
448-
449-
$this->assertEquals(2, substr_count($metadata2, '<md:KeyDescriptor use="signing"'));
442+
$this->assertEquals($expectEncryptionKeyDescriptor ? 2 : 0, substr_count($metadata, '<md:KeyDescriptor use="encryption"'));
443+
}
450444

451-
$this->assertEquals(2, substr_count($metadata2, '<md:KeyDescriptor use="encryption"'));
445+
public function testGetSPMetadataWithX509CertNewDataProvider()
446+
{
447+
return [
448+
'settings do not require encryption' => [
449+
'alwaysIncludeEncryption' => false,
450+
'wantNameIdEncrypted' => false,
451+
'wantAssertionsEncrypted' => false,
452+
'expectEncryptionKeyDescriptor' => false,
453+
],
454+
'wantNameIdEncrypted setting enabled' => [
455+
'alwaysIncludeEncryption' => false,
456+
'wantNameIdEncrypted' => true,
457+
'wantAssertionsEncrypted' => false,
458+
'expectEncryptionKeyDescriptor' => true,
459+
],
460+
'wantAssertionsEncrypted setting enabled' => [
461+
'alwaysIncludeEncryption' => false,
462+
'wantNameIdEncrypted' => false,
463+
'wantAssertionsEncrypted' => true,
464+
'expectEncryptionKeyDescriptor' => true,
465+
],
466+
'both settings enabled'=> [
467+
'alwaysIncludeEncryption' => false,
468+
'wantNameIdEncrypted' => true,
469+
'wantAssertionsEncrypted' => true,
470+
'expectEncryptionKeyDescriptor' => true,
471+
],
472+
'metadata requested with encryption' => [
473+
'alwaysIncludeEncryption' => true,
474+
'wantNameIdEncrypted' => false,
475+
'wantAssertionsEncrypted' => false,
476+
'expectEncryptionKeyDescriptor' => true,
477+
],
478+
'metadata requested with encryption and wantNameIdEncrypted setting enabled' => [
479+
'alwaysIncludeEncryption' => true,
480+
'wantNameIdEncrypted' => true,
481+
'wantAssertionsEncrypted' => false,
482+
'expectEncryptionKeyDescriptor' => true,
483+
],
484+
'metadata requested with encryption and wantAssertionsEncrypted setting enabled' => [
485+
'alwaysIncludeEncryption' => true,
486+
'wantNameIdEncrypted' => false,
487+
'wantAssertionsEncrypted' => true,
488+
'expectEncryptionKeyDescriptor' => true,
489+
],
490+
'metadata requested with encryption and both settings enabled' => [
491+
'alwaysIncludeEncryption' => true,
492+
'wantNameIdEncrypted' => true,
493+
'wantAssertionsEncrypted' => true,
494+
'expectEncryptionKeyDescriptor' => true,
495+
],
496+
];
452497
}
453498

454499
/**

0 commit comments

Comments
 (0)