Passed
Push — master ( 6d44e6...724433 )
by Tim
02:38
created

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