Passed
Push — master ( b3f66f...fee7a6 )
by Tim
14:24
created

Binding::setDestination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Exception\Protocol\UnsupportedBindingException;
11
use SimpleSAML\SAML2\XML\samlp\AbstractMessage;
12
13
use function array_key_exists;
14
use function array_keys;
15
use function array_map;
16
use function explode;
17
use function implode;
18
use function var_export;
19
20
/**
21
 * Base class for SAML 2 bindings.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
abstract class Binding
26
{
27
    /**
28
     * The RelayState associated with the message.
29
     *
30
     * @var string|null
31
     */
32
    protected ?string $relayState = null;
33
34
    /**
35
     * The destination of messages.
36
     *
37
     * This can be null, in which case the destination in the message is used.
38
     * @var string|null
39
     */
40
    protected ?string $destination = null;
41
42
43
    /**
44
     * Retrieve a binding with the given URN.
45
     *
46
     * Will throw an exception if it is unable to locate the binding.
47
     *
48
     * @param string $urn The URN of the binding.
49
     * @throws \SimpleSAML\SAML2\Exception\Protocol\UnsupportedBindingException
50
     * @return \SimpleSAML\SAML2\Binding The binding.
51
     */
52
    public static function getBinding(string $urn): Binding
53
    {
54
        switch ($urn) {
55
            case C::BINDING_HTTP_POST:
56
            case C::BINDING_HOK_SSO:
57
                return new HTTPPost();
58
            case C::BINDING_HTTP_REDIRECT:
59
                return new HTTPRedirect();
60
            case C::BINDING_HTTP_ARTIFACT:
61
                return new HTTPArtifact();
62
            // ECP ACS is defined with the PAOS binding, but as the IdP, we
63
            // talk to the ECP using SOAP -- if support for ECP as an SP is
64
            // implemented, this logic may need to change
65
            case C::BINDING_PAOS:
66
                return new SOAP();
67
            default:
68
                throw new UnsupportedBindingException('Unsupported binding: ' . var_export($urn, true));
69
        }
70
    }
71
72
73
    /**
74
     * Guess the current binding.
75
     *
76
     * This function guesses the current binding and creates an instance
77
     * of \SimpleSAML\SAML2\Binding matching that binding.
78
     *
79
     * An exception will be thrown if it is unable to guess the binding.
80
     *
81
     * @param \Psr\Http\Message\ServerRequestInterface $request
82
     * @throws \SimpleSAML\SAML2\Exception\Protocol\UnsupportedBindingException
83
     * @return \SimpleSAML\SAML2\Binding The binding.
84
     */
85
    public static function getCurrentBinding(ServerRequestInterface $request): Binding
86
    {
87
        $method = $request->getMethod();
88
89
        switch ($method) {
90
            case 'GET':
91
                $query = $request->getQueryParams();
92
                if (array_key_exists('SAMLRequest', $query) || array_key_exists('SAMLResponse', $query)) {
93
                    return new HTTPRedirect();
94
                } elseif (array_key_exists('SAMLart', $query)) {
95
                    return new HTTPArtifact();
96
                }
97
                break;
98
99
            case 'POST':
100
                $contentType = null;
101
                if ($request->hasHeader('Content-Type')) {
102
                    $contentType = $request->getHeader('Content-Type')[0];
103
                    $contentType = explode(';', $contentType);
104
                    $contentType = $contentType[0]; /* Remove charset. */
105
                }
106
107
                $query = $request->getParsedBody();
108
                if (array_key_exists('SAMLRequest', $query) || array_key_exists('SAMLResponse', $query)) {
109
                    return new HTTPPost();
110
                } elseif (array_key_exists('SAMLart', $query)) {
111
                    return new HTTPArtifact();
112
                } else {
113
                    /**
114
                     * The registration information for text/xml is in all respects the same
115
                     * as that given for application/xml (RFC 7303 - Section 9.1)
116
                     */
117
                    if (
118
                        ($contentType === 'text/xml' || $contentType === 'application/xml')
119
                        // See paragraph 3.2.3 of Binding for SAML2 (OASIS)
120
                        || ($request->hasHeader('SOAPAction')
121
                            && $request->getHeader('SOAPAction')[0] === 'http://www.oasis-open.org/committees/security')
122
                    ) {
123
                        return new SOAP();
124
                    }
125
                }
126
                break;
127
        }
128
129
        $logger = Utils::getContainer()->getLogger();
130
        $logger->warning('Unable to find the SAML 2 binding used for this request.');
131
        $logger->warning('Request method: ' . var_export($method, true));
132
133
        if (!empty($query)) {
134
            $logger->warning(
135
                $method . " parameters: '" . implode("', '", array_map('addslashes', array_keys($query))) . "'"
136
            );
137
        }
138
139
        if ($request->hasHeader('Content-Type')) {
140
            $logger->warning('Content-Type: ' . var_export($request->getHeader('Content-Type')[0], true));
141
        }
142
143
        throw new UnsupportedBindingException('Unable to find the SAML 2 binding used for this request.');
144
    }
145
146
147
    /**
148
     * Retrieve the destination of a message.
149
     *
150
     * @return string|null $destination The destination the message will be delivered to.
151
     */
152
    public function getDestination(): ?string
153
    {
154
        return $this->destination;
155
    }
156
157
158
    /**
159
     * Set the RelayState associated with he message.
160
     *
161
     * @param string|null $relayState The RelayState.
162
     */
163
    public function setRelayState(string $relayState = null): void
164
    {
165
        $this->relayState = $relayState;
166
    }
167
168
169
    /**
170
     * Get the RelayState associated with the message.
171
     *
172
     * @return string|null The RelayState.
173
     */
174
    public function getRelayState(): ?string
175
    {
176
        return $this->relayState;
177
    }
178
179
180
    /**
181
     * Override the destination of a message.
182
     *
183
     * Set to null to use the destination set in the message.
184
     *
185
     * @param string|null $destination The destination the message should be delivered to.
186
     */
187
    public function setDestination(string $destination = null): void
188
    {
189
        $this->destination = $destination;
190
    }
191
192
193
    /**
194
     * Send a SAML 2 message.
195
     *
196
     * This function will send a message using the specified binding.
197
     * The message will be delivered to the destination set in the message.
198
     *
199
     * @param \SimpleSAML\SAML2\XML\samlp\AbstractMessage $message The message which should be sent.
200
     * @return \Psr\Http\Message\ResponseInterface
201
     */
202
    abstract public function send(AbstractMessage $message): ResponseInterface;
203
204
205
    /**
206
     * Receive a SAML 2 message.
207
     *
208
     * This function will extract the message from the current request.
209
     * An exception will be thrown if we are unable to process the message.
210
     *
211
     * @param \Psr\Http\Message\ServerRequestInterface $request
212
     * @return \SimpleSAML\SAML2\XML\samlp\AbstractMessage The received message.
213
     */
214
    abstract public function receive(ServerRequestInterface $request): AbstractMessage;
215
}
216