Passed
Pull Request — master (#363)
by Tim
02:37
created

Binding::getBinding()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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