Passed
Push — master ( 293268...0b4558 )
by Tim
02:38 queued 14s
created

SignedElementTrait::setSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 22
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XMLSecurity\Exception\ReferenceValidationFailedException;
9
use SimpleSAML\XMLSecurity\XML\ds\Signature;
10
use SimpleSAML\XMLSecurity\XML\SignedElementTrait as BaseSignedElementTrait;
11
12
/**
13
 * Helper trait for processing signed elements.
14
 *
15
 * @package simplesamlphp/saml2
16
 */
17
trait SignedElementTrait
18
{
19
    use BaseSignedElementTrait;
20
21
22
    /**
23
     * Initialize a signed element from XML.
24
     *
25
     * @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature The ds:Signature object
26
     */
27
    protected function setSignature(Signature $signature): void
28
    {
29
        /**
30
         * Signatures MUST contain a single <ds:Reference> containing a same-document reference to the ID
31
         * attribute value of the root element of the assertion or protocol message being signed. For example, if the
32
         * ID attribute value is "foo", then the URI attribute in the <ds:Reference> element MUST be "#foo".
33
         */
34
35
        $references = $signature->getSignedInfo()->getReferences();
36
        Assert::count($references, 1, "A signature needs to have exactly one Reference, %d found.");
37
38
        $reference = array_pop($references);
39
        Assert::notNull($reference->getURI(), "URI attribute not found.", ReferenceValidationFailedException::class);
40
41
        Assert::same(
42
            $reference->getURI(),
43
            '#' . $this->getID(),
44
            "Reference does not point to given element.",
45
            ReferenceValidationFailedException::class,
46
        );
47
48
        $this->signature = $signature;
49
    }
50
}
51