-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\SAML11\XML\saml; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\SAML11\XML\saml\AbstractStatementType; | ||
use SimpleSAML\SAML11\XML\saml\Subject; | ||
use SimpleSAML\SAML11\Constants as C; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\MissingElementException; | ||
use SimpleSAML\XML\Exception\TooManyElementsException; | ||
|
||
use function array_pop; | ||
|
||
/** | ||
* @package simplesamlphp\saml11 | ||
*/ | ||
abstract class AbstractSubjectStatementType extends AbstractStatementType | ||
{ | ||
/** | ||
* CustomStatement constructor. | ||
* | ||
* @param \SimpleSAML\SAML11\XML\saml\Subject $subject | ||
*/ | ||
public function __construct( | ||
protected Subject $subject, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* Get the value of the subject-attribute. | ||
* | ||
* @return \SimpleSAML\SAML11\XML\saml\Subject | ||
*/ | ||
public function getSubject(): Subject | ||
{ | ||
return $this->subject; | ||
} | ||
|
||
|
||
/** | ||
* Convert XML into a SubjectStatement | ||
* | ||
* @param \DOMElement $xml The XML element we should load | ||
* @return static | ||
* | ||
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException | ||
* if the qualified name of the supplied element is wrong | ||
*/ | ||
public static function fromXML(DOMElement $xml): static | ||
{ | ||
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); | ||
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class); | ||
|
||
$subject = Subject::getChildrenOfClass($xml); | ||
Assert::minCount($subject, 1, MissingElementException::class); | ||
Assert::maxCount($subject, 1, TooManyElementsException::class); | ||
|
||
return new static( | ||
array_pop($subject), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Convert this SubjectStatement to XML. | ||
* | ||
* @param \DOMElement $parent The element we are converting to XML. | ||
* @return \DOMElement The XML element after adding the data corresponding to this SubjectStatement. | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
|
||
$this->getSubject()->toXML($e); | ||
|
||
return $e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\SAML11\XML\saml; | ||
|
||
/** | ||
* Class representing a saml:SubjectStatement element. | ||
* | ||
* @package simplesamlphp/saml11 | ||
*/ | ||
final class SubjectStatement extends AbstractSubjectStatementType | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<saml:SubjectStatement xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion"> | ||
<saml:Subject> | ||
<saml:NameIdentifier NameQualifier="TheNameQualifier" Format="urn:the:format">TheNameIDValue</saml:NameIdentifier> | ||
<saml:SubjectConfirmation> | ||
<saml:ConfirmationMethod>_Test1</saml:ConfirmationMethod> | ||
<saml:ConfirmationMethod>_Test2</saml:ConfirmationMethod> | ||
<saml:SubjectConfirmationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:integer">2</saml:SubjectConfirmationData> | ||
<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="fed654"> | ||
<ds:KeyName>testkey</ds:KeyName> | ||
<ds:X509Data> | ||
<ds:X509Certificate>MIICxDCCAi2gAwIBAgIUZ9QDx+SBFHednUWDFGm9tyVKrgQwDQYJKoZIhvcNAQELBQAwczElMCMGA1UEAwwcc2VsZnNpZ25lZC5zaW1wbGVzYW1scGhwLm9yZzEZMBcGA1UECgwQU2ltcGxlU0FNTHBocCBIUTERMA8GA1UEBwwISG9ub2x1bHUxDzANBgNVBAgMBkhhd2FpaTELMAkGA1UEBhMCVVMwIBcNMjIxMjAzMTAzNTQwWhgPMjEyMjExMDkxMDM1NDBaMHMxJTAjBgNVBAMMHHNlbGZzaWduZWQuc2ltcGxlc2FtbHBocC5vcmcxGTAXBgNVBAoMEFNpbXBsZVNBTUxwaHAgSFExETAPBgNVBAcMCEhvbm9sdWx1MQ8wDQYDVQQIDAZIYXdhaWkxCzAJBgNVBAYTAlVTMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDessdFRVDTMQQW3Na81B1CjJV1tmY3nopoIhZrkbDxLa+pv7jGDRcYreyu1DoQxEs06V2nHLoyOPhqJXSFivqtUwVYhR6NYgbNI6RRSsIJCweH0YOdlHna7gULPcLX0Bfbi4odStaFwG9yzDySwSEPtsKxm5pENPjNVGh+jJ+H/QIDAQABo1MwUTAdBgNVHQ4EFgQUvV75t8EoQo2fVa0E9otdtIGK5X0wHwYDVR0jBBgwFoAUvV75t8EoQo2fVa0E9otdtIGK5X0wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQANQUeiwPJXkWMXuaDHToEBKcezYGqGEYnGUi9LMjeb+Kln7X8nn5iknlz4k77rWCbSwLPC/WDr0ySYQA+HagaeUaFpoiYFJKS6uFlK1HYWnM3W4PUiGHg1/xeZlMO44wTwybXVo0y9KMhchfB5XNbDdoJcqWYvi6xtmZZNRbxUyw==</ds:X509Certificate> | ||
<ds:X509SubjectName>/CN=selfsigned.simplesamlphp.org/O=SimpleSAMLphp HQ/L=Honolulu/ST=Hawaii/C=US</ds:X509SubjectName> | ||
</ds:X509Data> | ||
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk> | ||
</ds:KeyInfo> | ||
</saml:SubjectConfirmation> | ||
</saml:Subject> | ||
</saml:SubjectStatement> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\SAML11\XML\saml; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\SAML11\XML\saml\ConfirmationMethod; | ||
use SimpleSAML\SAML11\XML\saml\NameIdentifier; | ||
use SimpleSAML\SAML11\XML\saml\Subject; | ||
use SimpleSAML\SAML11\XML\saml\SubjectConfirmation; | ||
use SimpleSAML\SAML11\XML\saml\SubjectConfirmationData; | ||
use SimpleSAML\SAML11\XML\saml\SubjectStatement; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; | ||
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; | ||
use SimpleSAML\XMLSecurity\XML\ds\KeyName; | ||
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate; | ||
use SimpleSAML\XMLSecurity\XML\ds\X509Data; | ||
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Tests for SubjectStatement elements. | ||
* | ||
* @covers \SimpleSAML\SAML11\XML\saml\SubjectStatement | ||
* @covers \SimpleSAML\SAML11\XML\saml\AbstractSubjectStatementType | ||
* @covers \SimpleSAML\SAML11\XML\saml\AbstractStatementType | ||
* @covers \SimpleSAML\SAML11\XML\saml\AbstractSamlElement | ||
* | ||
* @package simplesamlphp/saml11 | ||
*/ | ||
final class SubjectStatementTest extends TestCase | ||
{ | ||
use SerializableElementTestTrait; | ||
|
||
/** @var string */ | ||
private static string $certificate; | ||
|
||
/** @var string[] */ | ||
private static array $certData; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = SubjectStatement::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 5) . '/resources/xml/saml_SubjectStatement.xml', | ||
); | ||
|
||
self::$certificate = str_replace( | ||
[ | ||
'-----BEGIN CERTIFICATE-----', | ||
'-----END CERTIFICATE-----', | ||
'-----BEGIN RSA PUBLIC KEY-----', | ||
'-----END RSA PUBLIC KEY-----', | ||
"\r\n", | ||
"\n", | ||
], | ||
[ | ||
'', | ||
'', | ||
'', | ||
'', | ||
"\n", | ||
'' | ||
], | ||
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), | ||
); | ||
|
||
self::$certData = openssl_x509_parse( | ||
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), | ||
); | ||
} | ||
|
||
|
||
// marshalling | ||
|
||
|
||
/** | ||
* Test creating an Subject from scratch. | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$scd = new SubjectConfirmationData(2); | ||
|
||
$keyInfo = new KeyInfo( | ||
[ | ||
new KeyName('testkey'), | ||
new X509Data( | ||
[ | ||
new X509Certificate(self::$certificate), | ||
new X509SubjectName(self::$certData['name']), | ||
], | ||
), | ||
new Chunk(DOMDocumentFactory::fromString( | ||
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>' | ||
)->documentElement), | ||
], | ||
'fed654', | ||
); | ||
|
||
$sc = new SubjectConfirmation( | ||
[new ConfirmationMethod('_Test1'), new ConfirmationMethod('_Test2')], | ||
$scd, | ||
$keyInfo, | ||
); | ||
|
||
$nameIdentifier = new NameIdentifier( | ||
'TheNameIDValue', | ||
'TheNameQualifier', | ||
'urn:the:format', | ||
); | ||
|
||
$subject = new Subject($sc, $nameIdentifier); | ||
$subjectStatement = new SubjectStatement($subject); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($subjectStatement), | ||
); | ||
} | ||
} |