Completed
Pull Request — master (#995)
by
unknown
02:16
created

RequestValidatorTrait::getCookieParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 *
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server;
11
12
use League\OAuth2\Server\RequestEvent;
13
use League\OAuth2\Server\Entities\ClientEntityInterface;
14
use League\OAuth2\Server\Exception\OAuthServerException;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
trait RequestValidatorTrait
18
{
19
    /**
20
     * Get the Emitter.
21
     *
22
     * @return EmitterInterface
23
     */
24
    abstract public function getEmitter();
25
26
    /**
27
     * @return ClientRepositoryInterface
28
     */
29
    abstract public function getClientRepository();
30
31
    /**
32
     * Return the grant identifier that can be used in matching up requests.
33
     *
34
     * @return string
35
     */
36
    abstract public function getIdentifier();
37
38
    /**
39
     * Validate the client.
40
     *
41
     * @param ServerRequestInterface    $request
42
     *
43
     * @throws OAuthServerException
44
     *
45
     * @return ClientEntityInterface
46
     */
47 54
    public function validateClient(ServerRequestInterface $request)
48
    {
49 54
        list($basicAuthUser, $basicAuthPassword) = $this->getBasicAuthCredentials($request);
50
51 54
        $clientId = $this->getRequestParameter('client_id', $request, $basicAuthUser);
52 54
        if ($clientId === null) {
53 1
            throw OAuthServerException::invalidRequest('client_id');
54
        }
55
56
        // If the client is confidential require the client secret
57 53
        $clientSecret = $this->getRequestParameter('client_secret', $request, $basicAuthPassword);
58
59 53
        $client = $this->getClientRepository()->getClientEntity(
60 53
            $clientId,
61 53
            $this->getIdentifier(),
62 53
            $clientSecret,
63 53
            true
64
        );
65
66 53
        if ($client instanceof ClientEntityInterface === false) {
67 4
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
68 4
            throw OAuthServerException::invalidClient();
69
        }
70
71 49
        $redirectUri = $this->getRequestParameter('redirect_uri', $request, null);
72
73 49
        if ($redirectUri !== null) {
74 20
            $this->validateRedirectUri($redirectUri, $client, $request);
75
        }
76
77 47
        return $client;
78
    }
79
80
    /**
81
     * Validate redirectUri from the request.
82
     * If a redirect URI is provided ensure it matches what is pre-registered
83
     *
84
     * @param string                 $redirectUri
85
     * @param ClientEntityInterface  $client
86
     * @param ServerRequestInterface $request
87
     *
88
     * @throws OAuthServerException
89
     */
90 34
    public function validateRedirectUri(
91
        $redirectUri,
92
        ClientEntityInterface $client,
93
        ServerRequestInterface $request
94
    ) {
95 34
        if (\is_string($client->getRedirectUri())
96 34
            && (strcmp($client->getRedirectUri(), $redirectUri) !== 0)
97
        ) {
98 3
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
99 3
            throw OAuthServerException::invalidClient();
100 31
        } elseif (\is_array($client->getRedirectUri())
101 31
            && \in_array($redirectUri, $client->getRedirectUri(), true) === false
102
        ) {
103 3
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
104 3
            throw OAuthServerException::invalidClient();
105
        }
106 28
    }
107
108
    /**
109
     * Retrieve request parameter.
110
     *
111
     * @param string                 $parameter
112
     * @param ServerRequestInterface $request
113
     * @param mixed                  $default
114
     *
115
     * @return null|string
116
     */
117 54
    protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null)
118
    {
119 54
        $requestParameters = (array) $request->getParsedBody();
120
121 54
        return isset($requestParameters[$parameter]) ? $requestParameters[$parameter] : $default;
122
    }
123
124
    /**
125
     * Retrieve HTTP Basic Auth credentials with the Authorization header
126
     * of a request. First index of the returned array is the username,
127
     * second is the password (so list() will work). If the header does
128
     * not exist, or is otherwise an invalid HTTP Basic header, return
129
     * [null, null].
130
     *
131
     * @param ServerRequestInterface $request
132
     *
133
     * @return string[]|null[]
134
     */
135 59
    public function getBasicAuthCredentials(ServerRequestInterface $request)
136
    {
137 59
        if (!$request->hasHeader('Authorization')) {
138 54
            return [null, null];
139
        }
140
141 5
        $header = $request->getHeader('Authorization')[0];
142 5
        if (strpos($header, 'Basic ') !== 0) {
143 1
            return [null, null];
144
        }
145
146 4
        if (!($decoded = base64_decode(substr($header, 6)))) {
147 1
            return [null, null];
148
        }
149
150 3
        if (strpos($decoded, ':') === false) {
151 1
            return [null, null]; // HTTP Basic header without colon isn't valid
152
        }
153
154 2
        return explode(':', $decoded, 2);
155
    }
156
157
    /**
158
     * Retrieve query string parameter.
159
     *
160
     * @param string                 $parameter
161
     * @param ServerRequestInterface $request
162
     * @param mixed                  $default
163
     *
164
     * @return null|string
165
     */
166 21
    protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null)
167
    {
168 21
        return isset($request->getQueryParams()[$parameter]) ? $request->getQueryParams()[$parameter] : $default;
169
    }
170
171
    /**
172
     * Retrieve cookie parameter.
173
     *
174
     * @param string                 $parameter
175
     * @param ServerRequestInterface $request
176
     * @param mixed                  $default
177
     *
178
     * @return null|string
179
     */
180 1
    protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null)
181
    {
182 1
        return isset($request->getCookieParams()[$parameter]) ? $request->getCookieParams()[$parameter] : $default;
183
    }
184
185
    /**
186
     * Retrieve server parameter.
187
     *
188
     * @param string                 $parameter
189
     * @param ServerRequestInterface $request
190
     * @param mixed                  $default
191
     *
192
     * @return null|string
193
     */
194 20
    protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null)
195
    {
196 20
        return isset($request->getServerParams()[$parameter]) ? $request->getServerParams()[$parameter] : $default;
197
    }
198
}
199