Completed
Branch rewrite-api-md (eb38e5)
by Tim
03:57
created

AuthnContext::isEmptyElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use DOMElement;
8
use SAML2\Constants;
9
use SAML2\DOMDocumentFactory;
10
use SAML2\Utils;
11
use SAML2\XML\saml\AuthenticatingAuthority;
12
use SAML2\XML\saml\AuthnContextClassRef;
13
use SAML2\XML\saml\AuthnContextDecl;
14
use SAML2\XML\saml\AuthnContextDeclRef;
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * Class representing SAML2 AuthnContext
19
 *
20
 * @author Tim van Dijen, <[email protected]>
21
 * @package SimpleSAMLphp
22
 */
23
final class AuthnContext extends AbstractSamlElement
24
{
25
    /** @var \SAML2\XML\saml\AuthnContextClassRef|null */
26
    protected $authnContextClassRef = null;
27
28
    /** @var \SAML2\XML\saml\AuthnContextDeclRef|null */
29
    protected $authnContextDeclRef = null;
30
31
    /** @var \SAML2\XML\saml\AuthnContextDecl|null */
32
    protected $authnContextDecl = null;
33
34
    /** @var \SAML2\XML\saml\AuthenticatingAuthority[]|null */
35
    protected $authenticatingAuthorities = null;
36
37
38
    /**
39
     * Initialize an AuthnContext.
40
     *
41
     * @param \SAML2\XML\saml\AuthnContextClassRef|null $authnContextClassRef
42
     * @param \SAML2\XML\saml\AuthnContextDecl|null $authnContextDecl
43
     * @param \SAML2\XML\saml\AuthnContextDeclRef|null $authnContextDeclRef
44
     * @param \SAML2\XML\saml\AuthenticatingAuthority[]|null $authenticatingAuthorities
45
     */
46
    public function __construct(
47
        ?AuthnContextClassRef $authnContextClassRef,
48
        ?AuthnContextDecl $authnContextDecl,
49
        ?AuthnContextDeclRef $authnContextDeclRef,
50
        ?array $authenticatingAuthorities
51
    ) {
52
        if (!is_null($authnContextClassRef)) {
53
            Assert::oneOf(
54
                null,
55
                [$authnContextDecl, $authnContextDeclRef],
56
                'Can only have one of AuthnContextDecl/AuthnContextDeclRef'
57
            );
58
        } else {
59
            Assert::false(
60
                is_null($authnContextDecl) && is_null($authnContextDeclRef),
61
                'You need either an AuthnContextDecl or an AuthnContextDeclRef'
62
            );
63
        }
64
65
        $this->setAuthnContextClassRef($authnContextClassRef);
66
        $this->setAuthnContextDecl($authnContextDecl);
67
        $this->setAuthnContextDeclRef($authnContextDeclRef);
68
        $this->setAuthenticatingAuthorities($authenticatingAuthorities);
69
    }
70
71
72
    /**
73
     * Collect the value of the authnContextClassRef-property
74
     *
75
     * @return \SAML2\XML\saml\AuthnContextClassRef|null
76
     */
77
    public function getAuthnContextClassRef(): ?AuthnContextClassRef
78
    {
79
        return $this->authnContextClassRef;
80
    }
81
82
83
    /**
84
     * Set the value of the authnContextClassRef-property
85
     *
86
     * @param \SAML2\XML\saml\AuthnContextClassRef|null $authnContextClassRef
87
     * @return void
88
     */
89
    private function setAuthnContextClassRef(?AuthnContextClassRef $authnContextClassRef): void
90
    {
91
        $this->authnContextClassRef = $authnContextClassRef;
92
    }
93
94
95
    /**
96
     * Collect the value of the authnContextDeclRef-property
97
     *
98
     * @return \SAML2\XML\saml\AuthnContextDeclRef|null
99
     */
100
    public function getAuthnContextDeclRef(): ?AuthnContextDeclRef
101
    {
102
        return $this->authnContextDeclRef;
103
    }
104
105
106
    /**
107
     * Set the value of the authnContextDeclRef-property
108
     *
109
     * @param \SAML2\XML\saml\AuthnContextDeclRef|null $authnContextDeclRef
110
     * @return void
111
     */
112
    private function setAuthnContextDeclRef(?AuthnContextDeclRef $authnContextDeclRef): void
113
    {
114
        $this->authnContextDeclRef = $authnContextDeclRef;
115
    }
116
117
118
    /**
119
     * Collect the value of the authnContextDecl-property
120
     *
121
     * @return \SAML2\XML\saml\AuthnContextDecl|null
122
     */
123
    public function getAuthnContextDecl(): ?AuthnContextDecl
124
    {
125
        return $this->authnContextDecl;
126
    }
127
128
129
    /**
130
     * Set the value of the authnContextDecl-property
131
     *
132
     * @param \SAML2\XML\saml\AuthnContextDecl|null $authnContextDecl
133
     * @return void
134
     */
135
    private function setAuthnContextDecl(?AuthnContextDecl $authnContextDecl): void
136
    {
137
        $this->authnContextDecl = $authnContextDecl;
138
    }
139
140
141
    /**
142
     * Collect the value of the authenticatingAuthorities-property
143
     *
144
     * @return \SAML2\XML\saml\AuthenticatingAuthority[]|null
145
     */
146
    public function getAuthticatingAuthorities(): ?array
147
    {
148
        return $this->authenticatingAuthorities;
149
    }
150
151
152
    /**
153
     * Set the value of the authenticatingAuthorities-property
154
     *
155
     * @param \SAML2\XML\saml\AuthenticatingAuthority[]|null $authenticatingAuthorities
156
     * @return void
157
     */
158
    private function setAuthenticatingAuthorities(?array $authenticatingAuthorities): void
159
    {
160
        if (!is_null($authenticatingAuthorities)) {
0 ignored issues
show
introduced by
The condition is_null($authenticatingAuthorities) is always false.
Loading history...
161
            Assert::allIsInstanceof($authenticatingAuthorities, AuthenticatingAuthority::class);
162
        }
163
164
        $this->authenticatingAuthorities = $authenticatingAuthorities;
165
    }
166
167
168
    /**
169
     * Test if an object, at the state it's in, would produce an empty XML-element
170
     *
171
     * @return bool
172
     */
173
    public function isEmptyElement(): bool
174
    {
175
        return (
176
            empty($this->authnContextClassRef)
177
            && empty($this->authnContextDecl)
178
            && empty($this->authnContextDeclRef)
179
            && empty($this->authenticatingAuthorities)
180
        );
181
    }
182
183
184
    /**
185
     * Convert XML into a AuthnContext
186
     *
187
     * @param \DOMElement $xml The XML element we should load
188
     * @return \SAML2\XML\saml\AuthnContext
189
     */
190
    public static function fromXML(DOMElement $xml): object
191
    {
192
        Assert::same($xml->localName, 'AuthnContext');
193
        Assert::same($xml->namespaceURI, Constants::NS_SAML);
194
195
        /** @var \DOMElement[] $authnContextClassRef */
196
        $authnContextClassRef = Utils::xpQuery($xml, './saml_assertion:AuthnContextClassRef');
197
        Assert::maxCount($authnContextClassRef, 1);
198
199
        /** @var \DOMElement[] $authnContextDeclRef */
200
        $authnContextDeclRef = Utils::xpQuery($xml, './saml_assertion:AuthnContextDeclRef');
201
        Assert::maxCount($authnContextDeclRef, 1);
202
203
        /** @var \DOMElement[] $authnContextDecl */
204
        $authnContextDecl = Utils::xpQuery($xml, './saml_assertion:AuthnContextDecl');
205
        Assert::maxCount($authnContextDecl, 1);
206
207
        /** @var \DOMElement[] $authenticatingAuthorities */
208
        $authenticatingAuthorities = Utils::xpQuery($xml, './saml_assertion:AuthenticatingAuthority');
209
210
        return new self(
211
            empty($authnContextClassRef) ? null : AuthnContextClassRef::fromXML($authnContextClassRef[0]),
212
            empty($authnContextDecl) ? null : AuthnContextDecl::fromXML($authnContextDecl[0]),
213
            empty($authnContextDeclRef) ? null : AuthnContextDeclRef::fromXML($authnContextDeclRef[0]),
214
            array_map([AuthenticatingAuthority::class, 'fromXML'], $authenticatingAuthorities) ?: null
215
        );
216
    }
217
218
219
    /**
220
     * Convert this AuthContextDeclRef to XML.
221
     *
222
     * @param \DOMElement|null $parent The element we should append this AuthnContextDeclRef to.
223
     * @return \DOMElement
224
     */
225
    public function toXML(DOMElement $parent = null): DOMElement
226
    {
227
        $e = $this->instantiateParentElement($parent);
228
229
        if (!empty($this->authnContextClassRef)) {
230
            $this->authnContextClassRef->toXML($e);
231
        }
232
233
        if (!empty($this->authnContextDecl)) {
234
            $this->authnContextDecl->toXML($e);
235
        }
236
237
        if (!empty($this->authnContextDeclRef)) {
238
            $this->authnContextDeclRef->toXML($e);
239
        }
240
241
        if (!empty($this->authenticatingAuthorities)) {
242
            foreach ($this->authenticatingAuthorities as $authority) {
243
                $authority->toXML($e);
244
            }
245
        }
246
247
        return $e;
248
    }
249
}
250