Skip to content

Commit 68c4614

Browse files
committed
#269 Allow the getSPMetadata() method to always include the encryption KeyDescriptor
1 parent e590ced commit 68c4614

2 files changed

Lines changed: 71 additions & 24 deletions

File tree

src/Saml2/Settings.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,17 @@ public function shouldCompressResponses()
788788
/**
789789
* Gets the SP metadata. The XML representation.
790790
*
791+
* @param bool $alwaysPublishEncryptionCert When 'true', the returned
792+
* metadata will always include an 'encryption' KeyDescriptor. Otherwise,
793+
* the 'encryption' KeyDescriptor will only be included if
794+
* $advancedSettings['security']['wantNameIdEncrypted'] or
795+
* $advancedSettings['security']['wantAssertionsEncrypted'] are enabled.
796+
*
791797
* @return string SP metadata (xml)
792798
* @throws Exception
793799
* @throws Error
794800
*/
795-
public function getSPMetadata()
801+
public function getSPMetadata($alwaysPublishEncryptionCert = false)
796802
{
797803
$metadata = Metadata::builder($this->_sp, $this->_security['authnRequestsSigned'], $this->_security['wantAssertionsSigned'], null, null, $this->getContacts(), $this->getOrganization());
798804

@@ -801,7 +807,7 @@ public function getSPMetadata()
801807
$metadata = Metadata::addX509KeyDescriptors(
802808
$metadata,
803809
$certNew,
804-
$this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
810+
$alwaysPublishEncryptionCert || $this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
805811
);
806812
}
807813

@@ -810,7 +816,7 @@ public function getSPMetadata()
810816
$metadata = Metadata::addX509KeyDescriptors(
811817
$metadata,
812818
$cert,
813-
$this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
819+
$alwaysPublishEncryptionCert || $this->_security['wantNameIdEncrypted'] || $this->_security['wantAssertionsEncrypted']
814820
);
815821
}
816822

tests/src/OneLogin/Saml2/SettingsTest.php

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -404,33 +404,74 @@ public function testGetSPMetadata()
404404
* Case with x509certNew
405405
*
406406
* @covers OneLogin\Saml2\Settings::getSPMetadata
407+
* @dataProvider getSPMetadataWithX509CertNewDataProvider
407408
*/
408-
public function testGetSPMetadataWithX509CertNew()
409+
public function testGetSPMetadataWithX509CertNew($alwaysIncludeEncryption, $wantNameIdEncrypted, $wantAssertionsEncrypted, $expectEncryptionKeyDescriptor)
409410
{
410411
$settingsDir = TEST_ROOT .'/settings/';
411412
include $settingsDir.'settings5.php';
412-
413-
$settingsInfo['security']['wantNameIdEncrypted'] = false;
414-
$settingsInfo['security']['wantAssertionsEncrypted'] = false;
413+
$settingsInfo['security']['wantNameIdEncrypted'] = $wantNameIdEncrypted;
414+
$settingsInfo['security']['wantAssertionsEncrypted'] = $wantAssertionsEncrypted;
415415
$settings = new Settings($settingsInfo);
416-
$metadata = $settings->getSPMetadata();
417-
418-
$this->assertEquals(2, substr_count($metadata, "<md:KeyDescriptor"));
419-
416+
$metadata = $settings->getSPMetadata($alwaysIncludeEncryption);
417+
$this->assertEquals($expectEncryptionKeyDescriptor ? 4 : 2, substr_count($metadata, "<md:KeyDescriptor"));
418+
// signing KeyDescriptor should always be included
420419
$this->assertEquals(2, substr_count($metadata, '<md:KeyDescriptor use="signing"'));
421-
422-
$this->assertEquals(0, substr_count($metadata, '<md:KeyDescriptor use="encryption"'));
423-
424-
$settingsInfo['security']['wantNameIdEncrypted'] = true;
425-
$settingsInfo['security']['wantAssertionsEncrypted'] = true;
426-
$settings2 = new Settings($settingsInfo);
427-
$metadata2 = $settings2->getSPMetadata();
428-
429-
$this->assertEquals(4, substr_count($metadata2, "<md:KeyDescriptor"));
430-
431-
$this->assertEquals(2, substr_count($metadata2, '<md:KeyDescriptor use="signing"'));
432-
433-
$this->assertEquals(2, substr_count($metadata2, '<md:KeyDescriptor use="encryption"'));
420+
$this->assertEquals($expectEncryptionKeyDescriptor ? 2 : 0, substr_count($metadata, '<md:KeyDescriptor use="encryption"'));
421+
}
422+
423+
public function getSPMetadataWithX509CertNewDataProvider()
424+
{
425+
return [
426+
'settings do not require encryption' => [
427+
'alwaysIncludeEncryption' => false,
428+
'wantNameIdEncrypted' => false,
429+
'wantAssertionsEncrypted' => false,
430+
'expectEncryptionKeyDescriptor' => false,
431+
],
432+
'wantNameIdEncrypted setting enabled' => [
433+
'alwaysIncludeEncryption' => false,
434+
'wantNameIdEncrypted' => true,
435+
'wantAssertionsEncrypted' => false,
436+
'expectEncryptionKeyDescriptor' => true,
437+
],
438+
'wantAssertionsEncrypted setting enabled' => [
439+
'alwaysIncludeEncryption' => false,
440+
'wantNameIdEncrypted' => false,
441+
'wantAssertionsEncrypted' => true,
442+
'expectEncryptionKeyDescriptor' => true,
443+
],
444+
'both settings enabled'=> [
445+
'alwaysIncludeEncryption' => false,
446+
'wantNameIdEncrypted' => true,
447+
'wantAssertionsEncrypted' => true,
448+
'expectEncryptionKeyDescriptor' => true,
449+
],
450+
'metadata requested with encryption' => [
451+
'alwaysIncludeEncryption' => true,
452+
'wantNameIdEncrypted' => false,
453+
'wantAssertionsEncrypted' => false,
454+
'expectEncryptionKeyDescriptor' => true,
455+
],
456+
'metadata requested with encryption and wantNameIdEncrypted setting enabled' => [
457+
'alwaysIncludeEncryption' => true,
458+
'wantNameIdEncrypted' => true,
459+
'wantAssertionsEncrypted' => false,
460+
'expectEncryptionKeyDescriptor' => true,
461+
],
462+
'metadata requested with encryption and wantAssertionsEncrypted setting enabled' => [
463+
'alwaysIncludeEncryption' => true,
464+
'wantNameIdEncrypted' => false,
465+
'wantAssertionsEncrypted' => true,
466+
'expectEncryptionKeyDescriptor' => true,
467+
],
468+
'metadata requested with encryption and both settings enabled' => [
469+
'alwaysIncludeEncryption' => true,
470+
'wantNameIdEncrypted' => true,
471+
'wantAssertionsEncrypted' => true,
472+
'expectEncryptionKeyDescriptor' => true,
473+
],
474+
];
434475
}
435476

436477
/**

0 commit comments

Comments
 (0)