HTTPRedirect   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 77
dl 0
loc 170
rs 10
c 1
b 0
f 0
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getRedirectURL() 0 51 7
C receive() 0 78 13
A send() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Binding;
6
7
use Exception;
8
use Nyholm\Psr7\Response;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use SimpleSAML\SAML2\Assert\Assert;
12
use SimpleSAML\SAML2\Binding;
13
use SimpleSAML\SAML2\Binding\RelayStateTrait;
14
use SimpleSAML\SAML2\Compat\ContainerSingleton;
15
use SimpleSAML\SAML2\Constants as C;
16
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
17
use SimpleSAML\SAML2\Utils;
18
use SimpleSAML\SAML2\XML\samlp\AbstractMessage;
19
use SimpleSAML\SAML2\XML\samlp\AbstractRequest;
20
use SimpleSAML\SAML2\XML\samlp\MessageFactory;
21
use SimpleSAML\XML\DOMDocumentFactory;
22
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
23
use SimpleSAML\XMLSecurity\Exception\SignatureVerificationFailedException;
24
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
25
26
use function array_key_exists;
27
use function base64_decode;
28
use function base64_encode;
29
use function gzdeflate;
30
use function gzinflate;
31
use function sprintf;
32
use function str_contains;
33
use function strlen;
34
use function urlencode;
35
36
/**
37
 * Class which implements the HTTP-Redirect binding.
38
 *
39
 * @package simplesamlphp/saml2
40
 */
41
class HTTPRedirect extends Binding implements AsynchronousBindingInterface, RelayStateInterface
42
{
43
    use RelayStateTrait;
44
45
46
    /**
47
     * Create the redirect URL for a message.
48
     *
49
     * @param \SimpleSAML\SAML2\XML\samlp\AbstractMessage $message The message.
50
     * @return string The URL the user should be redirected to in order to send a message.
51
     */
52
    public function getRedirectURL(AbstractMessage $message): string
53
    {
54
        if ($this->destination === null) {
55
            $destination = $message->getDestination();
56
            if ($destination === null) {
57
                throw new Exception('Cannot build a redirect URL, no destination set.');
58
            } else {
59
                $destination = $destination->getValue();
60
            }
61
        } else {
62
            $destination = $this->destination;
63
        }
64
65
        $relayState = $this->getRelayState();
66
        $msgStr = $message->toXML();
67
68
        Utils::getContainer()->debugMessage($msgStr, 'out');
69
        $msgStr = $msgStr->ownerDocument?->saveXML($msgStr);
70
71
        $msgStr = gzdeflate($msgStr);
72
        $msgStr = base64_encode($msgStr);
73
74
        /* Build the query string. */
75
76
        if ($message instanceof AbstractRequest) {
77
            $msg = 'SAMLRequest=';
78
        } else {
79
            $msg = 'SAMLResponse=';
80
        }
81
        $msg .= urlencode($msgStr);
82
83
        if ($relayState !== null) {
84
            $msg .= '&RelayState=' . urlencode($relayState);
85
        }
86
87
        $signature = $message->getSignature();
88
        if ($signature !== null) { // add the signature
89
            $signatureMethod = $signature->getSignedInfo()->getSignatureMethod();
90
            $signatureValue = $signature->getSignatureValue();
91
92
            $msg .= '&SigAlg=' . urlencode($signatureMethod->getAlgorithm()->getValue());
93
            $msg .= '&Signature=' . urlencode($signatureValue->getValue()->getValue());
94
        }
95
96
        if (str_contains($destination, '?')) {
97
            $destination .= '&' . $msg;
98
        } else {
99
            $destination .= '?' . $msg;
100
        }
101
102
        return $destination;
103
    }
104
105
106
    /**
107
     * Send a SAML 2 message using the HTTP-Redirect binding.
108
     *
109
     * @param \SimpleSAML\SAML2\XML\samlp\AbstractMessage $message
110
     * @return \Psr\Http\Message\ResponseInterface
111
     */
112
    public function send(AbstractMessage $message): ResponseInterface
113
    {
114
        $destination = $this->getRedirectURL($message);
115
        Utils::getContainer()->getLogger()->debug(
116
            'Redirect to ' . strlen($destination) . ' byte URL: ' . $destination,
117
        );
118
        return new Response(303, ['Location' => $destination]);
119
    }
120
121
122
    /**
123
     * Receive a SAML 2 message sent using the HTTP-Redirect binding.
124
     *
125
     * Throws an exception if it is unable receive the message.
126
     *
127
     * @param \Psr\Http\Message\ServerRequestInterface $request
128
     * @return \SimpleSAML\SAML2\XML\samlp\AbstractMessage The received message.
129
     * @throws \Exception
130
     *
131
     * NPath is currently too high but solving that just moves code around.
132
     */
133
    public function receive(ServerRequestInterface $request): AbstractMessage
134
    {
135
        $query = $request->getQueryParams();
136
137
        /**
138
         * Put the SAMLRequest/SAMLResponse from the actual query string into $message,
139
         * and assert that the result from parseQuery() in $query and the parsing of the SignedQuery in $res agree
140
         */
141
        if (array_key_exists('SAMLRequest', $query)) {
142
            $message = $query['SAMLRequest'];
143
            $signedQuery = 'SAMLRequest=' . urlencode($query['SAMLRequest']);
144
        } elseif (array_key_exists('SAMLResponse', $query)) {
145
            $message = $query['SAMLResponse'];
146
            $signedQuery = 'SAMLResponse=' . urlencode($query['SAMLResponse']);
147
        } else {
148
            throw new Exception('Missing SAMLRequest or SAMLResponse parameter.');
149
        }
150
151
        if (array_key_exists('SAMLRequest', $query) && array_key_exists('SAMLResponse', $query)) {
152
            throw new Exception('Both SAMLRequest and SAMLResponse provided.');
153
        }
154
155
        if (isset($query['SAMLEncoding']) && $query['SAMLEncoding'] !== C::BINDING_HTTP_REDIRECT_DEFLATE) {
156
            throw new Exception(sprintf('Unknown SAMLEncoding: %s', $query['SAMLEncoding']));
157
        }
158
159
        $message = base64_decode($message, true);
160
        if ($message === false) {
161
            throw new Exception('Error while base64 decoding SAML message.');
162
        }
163
164
        $message = gzinflate($message);
165
        if ($message === false) {
166
            throw new Exception('Error while inflating SAML message.');
167
        }
168
169
        $document = DOMDocumentFactory::fromString($message);
170
        Utils::getContainer()->debugMessage($document->documentElement, 'in');
171
        $message = MessageFactory::fromXML($document->documentElement);
172
173
        if (array_key_exists('RelayState', $query)) {
174
            $this->setRelayState($query['RelayState']);
175
            $signedQuery .= '&RelayState=' . urlencode($query['RelayState']);
176
        }
177
178
        if (!array_key_exists('Signature', $query)) {
179
            return $message;
180
        }
181
182
        /**
183
         * 3.4.5.2 - SAML Bindings
184
         *
185
         * If the message is signed, the Destination XML attribute in the root SAML element of the protocol
186
         * message MUST contain the URL to which the sender has instructed the user agent to deliver the
187
         * message.
188
         */
189
        Assert::notNull($message->getDestination(), ProtocolViolationException::class);
190
        // Validation of the Destination must be done upstream
191
192
        if (!array_key_exists('SigAlg', $query)) {
193
            throw new Exception('Missing signature algorithm.');
194
        } else {
195
            $signedQuery .= '&SigAlg=' . urlencode($query['SigAlg']);
196
        }
197
198
        $container = ContainerSingleton::getInstance();
199
        $blacklist = $container->getBlacklistedEncryptionAlgorithms();
200
        $verifier = (new SignatureAlgorithmFactory($blacklist))->getAlgorithm(
0 ignored issues
show
Bug introduced by
It seems like $blacklist can also be of type null; however, parameter $blacklist of SimpleSAML\XMLSecurity\A...mFactory::__construct() does only seem to accept array, 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

200
        $verifier = (new SignatureAlgorithmFactory(/** @scrutinizer ignore-type */ $blacklist))->getAlgorithm(
Loading history...
201
            $query['SigAlg'],
202
            // TODO:  Need to use the key from the metadata
203
            PEMCertificatesMock::getPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY),
204
        );
205
206
        if ($verifier->verify($signedQuery, base64_decode($query['Signature'])) === false) {
207
            throw new SignatureVerificationFailedException('Failed to verify signature.');
208
        }
209
210
        return $message;
211
    }
212
}
213