EntityAttributes   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 9 2
A getChildren() 0 3 1
A addChild() 0 3 1
A fromXML() 0 22 6
A __construct() 0 62 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\mdattr;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\XML\saml\Assertion;
12
use SimpleSAML\SAML2\XML\saml\Attribute;
13
use SimpleSAML\SAML2\XML\saml\AttributeStatement;
14
use SimpleSAML\SAML2\XML\saml\NameID;
15
use SimpleSAML\XML\SchemaValidatableElementInterface;
16
use SimpleSAML\XML\SchemaValidatableElementTrait;
17
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
18
19
use function array_filter;
20
use function array_merge;
21
use function sprintf;
22
23
/**
24
 * Class for handling the EntityAttributes metadata extension.
25
 *
26
 * @link: http://docs.oasis-open.org/security/saml/Post2.0/sstc-metadata-attr-cs-01.pdf
27
 *
28
 * @package simplesamlphp/saml2
29
 */
30
final class EntityAttributes extends AbstractMdattrElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\mdattr\AbstractMdattrElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
{
32
    use SchemaValidatableElementTrait;
33
34
35
    /**
36
     * Create a EntityAttributes element.
37
     *
38
     * @param (\SimpleSAML\SAML2\XML\saml\Assertion|\SimpleSAML\SAML2\XML\saml\Attribute)[] $children
39
     */
40
    public function __construct(
41
        protected array $children,
42
    ) {
43
        Assert::maxCount($children, C::UNBOUNDED_LIMIT);
44
        Assert::allIsInstanceOfAny($children, [Assertion::class, Attribute::class]);
45
46
        $assertions = array_filter($children, function ($child) {
47
            return $child instanceof Assertion;
48
        });
49
50
        foreach ($assertions as $assertion) {
51
            $statements = array_merge(
52
                $assertion->getAttributeStatements(),
53
                $assertion->getAuthnStatements(),
54
                $assertion->getStatements(),
55
            );
56
57
            Assert::allIsInstanceOf(
58
                $statements,
59
                AttributeStatement::class,
60
                '<saml:Asssertion> elements in an <mdattr:EntityAttributes> may only contain AttributeStatements',
61
                ProtocolViolationException::class,
62
            );
63
            Assert::count(
64
                $statements,
65
                1,
66
                'One (and only one) <saml:AttributeStatement> MUST be included '
67
                . 'in a <saml:Assertion> inside a <mdattr:EntityAttribute>',
68
                ProtocolViolationException::class,
69
            );
70
            Assert::notNull(
71
                Assertion::fromXML($assertion->toXML())->getSignature(),
72
                'Every <saml:Assertion> inside a <mdattr:EntityAttributes> must be individually signed',
73
                ProtocolViolationException::class,
74
            );
75
76
            $subject = $assertion->getSubject();
77
            Assert::notNull(
78
                $subject,
79
                'Every <saml:Assertion> inside a <mdattr:EntityAttributes> must contain a Subject',
80
                ProtocolViolationException::class,
81
            );
82
83
            Assert::isEmpty(
84
                $subject->getSubjectConfirmation(),
85
                'Every <saml:Assertion> inside a <mdattr:EntityAttributes> must NOT contain any SubjectConfirmation',
86
                ProtocolViolationException::class,
87
            );
88
89
            /** @var \SimpleSAML\SAML2\XML\saml\NameID|null $nameId */
90
            $nameId = $subject?->getIdentifier();
91
            Assert::isInstanceOf(
92
                $nameId,
93
                NameID::class,
94
                'Every <saml:Assertion> inside a <mdattr:EntityAttributes> must contain a NameID',
95
                ProtocolViolationException::class,
96
            );
97
            Assert::same(
98
                $nameId?->getFormat()->getValue(),
99
                C::NAMEID_ENTITY,
100
                sprintf('The NameID format must be %s', C::NAMEID_ENTITY),
101
                ProtocolViolationException::class,
102
            );
103
        }
104
    }
105
106
107
    /**
108
     * Collect the value of the children-property
109
     *
110
     * @return (\SimpleSAML\SAML2\XML\saml\Assertion|\SimpleSAML\SAML2\XML\saml\Attribute)[]
111
     */
112
    public function getChildren(): array
113
    {
114
        return $this->children;
115
    }
116
117
118
    /**
119
     * Add the value to the children-property
120
     *
121
     * @param \SimpleSAML\SAML2\XML\saml\Assertion|\SimpleSAML\SAML2\XML\saml\Attribute $child
122
     *
123
     * @throws \SimpleSAML\Assert\AssertionFailedException
124
     */
125
    public function addChild($child): void
126
    {
127
        $this->children = array_merge($this->children, [$child]);
128
    }
129
130
131
    /**
132
     * Convert XML into a EntityAttributes
133
     *
134
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
135
     *   if the qualified name of the supplied element is wrong
136
     */
137
    public static function fromXML(DOMElement $xml): static
138
    {
139
        Assert::same($xml->localName, 'EntityAttributes', InvalidDOMElementException::class);
140
        Assert::same($xml->namespaceURI, EntityAttributes::NS, InvalidDOMElementException::class);
141
142
        $children = [];
143
        foreach ($xml->childNodes as $node) {
144
            if ($node instanceof DOMElement && $node->namespaceURI === C::NS_SAML) {
145
                switch ($node->localName) {
146
                    case 'Assertion':
147
                        $children[] = Assertion::fromXML($node);
148
                        break;
149
                    case 'Attribute':
150
                        $children[] = Attribute::fromXML($node);
151
                        break;
152
                    default:
153
                        continue 2;
154
                }
155
            }
156
        }
157
158
        return new static($children);
159
    }
160
161
162
    /**
163
     * Convert this EntityAttributes to XML.
164
     */
165
    public function toXML(?DOMElement $parent = null): DOMElement
166
    {
167
        $e = $this->instantiateParentElement($parent);
168
169
        foreach ($this->getChildren() as $child) {
170
            $child->toXML($e);
171
        }
172
173
        return $e;
174
    }
175
}
176