Passed
Push — master ( cd94d8...ff9ad6 )
by Tim
02:37
created

AuthnQuery::toUnsignedXML()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooHighException;
10
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooLowException;
11
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
12
use SimpleSAML\SAML2\XML\saml\Issuer;
13
use SimpleSAML\SAML2\XML\saml\Subject;
14
use SimpleSAML\SAML2\XML\samlp\RequestedAuthnContext;
15
use SimpleSAML\XML\Exception\InvalidDOMElementException;
16
use SimpleSAML\XML\Exception\MissingElementException;
17
use SimpleSAML\XML\Exception\TooManyElementsException;
18
use SimpleSAML\XML\Utils as XMLUtils;
19
use SimpleSAML\XMLSecurity\ds\Signature;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\ds\Signature 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...
20
21
use function array_pop;
22
use function in_array;
23
24
/**
25
 * Class for SAML 2 authentication query messages.
26
 *
27
 * The <AuthnQuery> message element is used to make the query What assertions containing
28
 * authentication statements are available for this subject? A successful <Response> will contain one or
29
 * more assertions containing authentication statements.
30
 *
31
 * The <AuthnQuery> message MUST NOT be used as a request for a new authentication using
32
 * credentials provided in the request. <AuthnQuery> is a request for statements about authentication acts
33
 * that have occurred in a previous interaction between the indicated subject and the authentication authority.
34
 *
35
 * @package simplesamlphp/saml2
36
 */
37
class AuthnQuery extends AbstractSubjectQuery
38
{
39
    /**
40
     * The requested authentication context.
41
     *
42
     * @var \SimpleSAML\SAML2\XML\samlp\RequestedAuthnContext|null
43
     */
44
    protected ?RequestedAuthnContext $requestedAuthnContext;
45
46
    /**
47
     * The session index
48
     *
49
     * @var string|null $sessionIndex
50
     */
51
    protected ?string $sessionIndex;
52
53
54
    /**
55
     * Constructor for SAML 2 AttributeQuery.
56
     *
57
     * @param \SimpleSAML\SAML2\XML\saml\Subject $subject
58
     * @param \SimpleSAML\SAML2\XML\samlp\RequestedAuthnContext|null $requestedAuthnContext
59
     * @param string|null $sessionIndex
60
     * @param \SimpleSAML\SAML2\XML\saml\Issuer $issuer
61
     * @param string $id
62
     * @param int $issueInstant
63
     * @param string|null $destination
64
     * @param string|null $consent
65
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
66
     */
67
    public function __construct(
68
        Subject $subject,
69
        ?Issuer $issuer = null,
70
        ?RequestedAuthnContext $requestedAuthnContext = null,
71
        ?string $sessionIndex = null,
72
        ?string $id = null,
73
        ?int $issueInstant = null,
74
        ?string $destination = null,
75
        ?string $consent = null,
76
        ?Extensions $extensions = null
77
    ) {
78
        parent::__construct($subject, $issuer, $id, $issueInstant, $destination, $consent, $extensions);
79
80
        $this->setRequestedAuthnContext($requestedAuthnContext);
81
        $this->setSessionIndex($sesseionIndex);
0 ignored issues
show
Bug introduced by
The method setSessionIndex() does not exist on SimpleSAML\SAML2\XML\samlp\AuthnQuery. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        $this->/** @scrutinizer ignore-call */ 
82
               setSessionIndex($sesseionIndex);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Comprehensibility Best Practice introduced by
The variable $sesseionIndex does not exist. Did you maybe mean $sessionIndex?
Loading history...
82
    }
83
84
85
    /**
86
     * @return \SimpleSAML\SAML2\XML\samlp\RequestedAuthnContext|null
87
     */
88
    public function getRequestedAuthnContext(): ?RequestedAuthnContext
89
    {
90
        return $this->requestedAuthnContext;
91
    }
92
93
94
    /**
95
     * @param \SimpleSAML\SAML2\XML\samlp\RequestedAuthnContext|null $requestedAuthnContext
96
     */
97
    protected function setRequestedAuthnContext(?RequestedAuthnContext $requestedAuthnContext): void
98
    {
99
        $this->requestedAuthnContext = $requestedAuthnContext;
100
    }
101
102
103
    /**
104
     * Create a class from XML
105
     *
106
     * @param \DOMElement $xml
107
     * @return self
108
     *
109
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
110
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
111
     * @throws \SimpleSAML\XML\Exception\MissingElementException if one of the mandatory child-elements is missing
112
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
113
     */
114
    public static function fromXML(DOMElement $xml): object
115
    {
116
        Assert::same($xml->localName, 'AuthnQueryQuery', InvalidDOMElementException::class);
117
        Assert::same($xml->namespaceURI, AuthnQuery::NS, InvalidDOMElementException::class);
118
119
        Assert::true(version_compare('2.0', self::getAttribute($xml, 'Version'), '<='), RequestVersionTooLowException::class);
120
        Assert::true(version_compare('2.0', self::getAttribute($xml, 'Version'), '>='), RequestVersionTooHighException::class);
121
122
        $id = self::getAttribute($xml, 'ID');
123
        $sessionIndex = self::getAttribute($xml, 'SessionIndex', null);
124
        $destination = self::getAttribute($xml, 'Destination', null);
125
        $consent = self::getAttribute($xml, 'Consent', null);
126
127
        $issueInstant = self::getAttribute($xml, 'IssueInstant');
128
        Assert::validDateTimeZulu($issueInstant, ProtocolViolationException::class);
129
        $issueInstant = XMLUtils::xsDateTimeToTimestamp($issueInstant);
130
131
        $requestedAuthnContext = RequestedAuthnContext::getChildrenOfClass($xml);
132
        Assert::countBetween($issuer, 0, 1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $issuer seems to be never defined.
Loading history...
133
134
        $issuer = Issuer::getChildrenOfClass($xml);
135
        Assert::countBetween($issuer, 0, 1);
136
137
        $extensions = Extensions::getChildrenOfClass($xml);
138
        Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.', TooManyElementsException::class);
139
140
        $subject = Subject::getChildrenOfClass($xml);
141
        Assert::notEmpty($subject, 'Missing subject in subject query.', MissingElementException::class);
142
        Assert::maxCount($subject, 1, 'More than one <saml:Subject> in AttributeQuery', TooManyElementsException::class);
143
144
        $signature = Signature::getChildrenOfClass($xml);
145
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
146
147
        $request = new self(
148
            array_pop($subject),
149
            array_pop($requestedAuthnContext),
150
            $sessionIndex,
0 ignored issues
show
Bug introduced by
It seems like $sessionIndex can also be of type string; however, parameter $requestedAuthnContext of SimpleSAML\SAML2\XML\sam...thnQuery::__construct() does only seem to accept SimpleSAML\SAML2\XML\sam...uestedAuthnContext|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
            /** @scrutinizer ignore-type */ $sessionIndex,
Loading history...
151
            array_pop($issuer),
152
            $id,
153
            $issueInstant,
154
            $destination,
155
            $consent,
156
            array_pop($extensions)
157
        );
158
159
        if (!empty($signature)) {
160
            $request->setSignature($signature[0]);
161
            $request->setXML($xml);
162
        }
163
164
        return $request;
165
    }
166
167
168
    /**
169
     * Convert this message to an unsigned XML document.
170
     * This method does not sign the resulting XML document.
171
     *
172
     * @return \DOMElement The root element of the DOM tree
173
     */
174
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
175
    {
176
        $e = parent::toUnsignedXML($parent);
177
178
        if ($this->requestedAuthnContext !== null) {
179
            $this->requestedAuthnContext->toXML($e);
180
        }
181
182
        if ($this->sessionIndex !== null) {
183
            $e->setAttribute('SessionIndex', $this->sessionIndex);
184
        }
185
186
        return $e;
187
    }
188
}
189