Completed
Pull Request — master (#719)
by
unknown
35:20
created

OAuthServerException::generateHttpResponse()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 13
nc 6
nop 3
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\Exception;
11
12
use Psr\Http\Message\ResponseInterface;
13
14
class OAuthServerException extends \Exception
15
{
16
    /**
17
     * @var int
18
     */
19
    private $httpStatusCode;
20
21
    /**
22
     * @var string
23
     */
24
    private $errorType;
25
26
    /**
27
     * @var null|string
28
     */
29
    private $hint;
30
31
    /**
32
     * @var null|string
33
     */
34
    private $redirectUri;
35
36
    /**
37
     * @var  array
38
     */
39
    private $payload;
40
41
    /**
42
     * Throw a new exception.
43
     *
44
     * @param string      $message        Error message
45
     * @param int         $code           Error code
46
     * @param string      $errorType      Error type
47
     * @param int         $httpStatusCode HTTP status code to send (default = 400)
48
     * @param null|string $hint           A helper hint
49
     * @param null|string $redirectUri    A HTTP URI to redirect the user back to
50
     */
51
    public function __construct($message, $code, $errorType, $httpStatusCode = 400, $hint = null, $redirectUri = null)
52
    {
53
        parent::__construct($message, $code);
54
        $this->httpStatusCode = $httpStatusCode;
55
        $this->errorType = $errorType;
56
        $this->hint = $hint;
57
        $this->redirectUri = $redirectUri;
58
        $this->payload = [
59
            'error'   => $errorType,
60
            'message' => $message,
61
        ];
62
        if ($hint !== null) {
63
            $this->payload['hint'] = $hint;
64
        }
65
    }
66
67
    /**
68
     * Returns the current payload.
69
     *
70
     * @return array
71
     */
72
    public function getPayload()
73
    {
74
        return $this->payload;
75
    }
76
77
    /**
78
     * Updates the current payload.
79
     *
80
     * @param array $payload
81
     */
82
    public function setPayload(array $payload)
83
    {
84
        $this->payload = $payload;
85
    }
86
87
    /**
88
     * Unsupported grant type error.
89
     *
90
     * @return static
91
     */
92
    public static function unsupportedGrantType()
93
    {
94
        $errorMessage = 'The authorization grant type is not supported by the authorization server.';
95
        $hint = 'Check the `grant_type` parameter';
96
97
        return new static($errorMessage, 2, 'unsupported_grant_type', 400, $hint);
98
    }
99
100
    /**
101
     * Invalid request error.
102
     *
103
     * @param string      $parameter The invalid parameter
104
     * @param null|string $hint
105
     *
106
     * @return static
107
     */
108
    public static function invalidRequest($parameter, $hint = null)
109
    {
110
        $errorMessage = 'The request is missing a required parameter, includes an invalid parameter value, ' .
111
            'includes a parameter more than once, or is otherwise malformed.';
112
        $hint = ($hint === null) ? sprintf('Check the `%s` parameter', $parameter) : $hint;
113
114
        return new static($errorMessage, 3, 'invalid_request', 400, $hint);
115
    }
116
117
    /**
118
     * Invalid client error.
119
     *
120
     * @return static
121
     */
122
    public static function invalidClient()
123
    {
124
        $errorMessage = 'Client authentication failed';
125
126
        return new static($errorMessage, 4, 'invalid_client', 401);
127
    }
128
129
    /**
130
     * Invalid scope error.
131
     *
132
     * @param string      $scope       The bad scope
133
     * @param null|string $redirectUri A HTTP URI to redirect the user back to
134
     *
135
     * @return static
136
     */
137
    public static function invalidScope($scope, $redirectUri = null)
138
    {
139
        $errorMessage = 'The requested scope is invalid, unknown, or malformed';
140
        $hint = sprintf('Check the `%s` scope', $scope);
141
142
        return new static($errorMessage, 5, 'invalid_scope', 400, $hint, $redirectUri);
143
    }
144
145
    /**
146
     * Invalid credentials error.
147
     *
148
     * @return static
149
     */
150
    public static function invalidCredentials()
151
    {
152
        return new static('The user credentials were incorrect.', 6, 'invalid_credentials', 401);
153
    }
154
155
    /**
156
     * Server error.
157
     *
158
     * @param $hint
159
     *
160
     * @return static
161
     *
162
     * @codeCoverageIgnore
163
     */
164
    public static function serverError($hint)
165
    {
166
        return new static(
167
            'The authorization server encountered an unexpected condition which prevented it from fulfilling'
168
            . ' the request: ' . $hint,
169
            7,
170
            'server_error',
171
            500
172
        );
173
    }
174
175
    /**
176
     * Invalid refresh token.
177
     *
178
     * @param null|string $hint
179
     *
180
     * @return static
181
     */
182
    public static function invalidRefreshToken($hint = null)
183
    {
184
        return new static('The refresh token is invalid.', 8, 'invalid_request', 400, $hint);
185
    }
186
187
    /**
188
     * Access denied.
189
     *
190
     * @param null|string $hint
191
     * @param null|string $redirectUri
192
     *
193
     * @return static
194
     */
195
    public static function accessDenied($hint = null, $redirectUri = null)
196
    {
197
        return new static(
198
            'The resource owner or authorization server denied the request.',
199
            9,
200
            'access_denied',
201
            401,
202
            $hint,
203
            $redirectUri
204
        );
205
    }
206
207
    /**
208
     * Invalid grant.
209
     *
210
     * @param string $hint
211
     *
212
     * @return static
213
     */
214
    public static function invalidGrant($hint = '')
215
    {
216
        return new static(
217
            'The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token '
218
                . 'is invalid, expired, revoked, does not match the redirection URI used in the authorization request, '
219
                . 'or was issued to another client.',
220
            10,
221
            'invalid_grant',
222
            400,
223
            $hint
224
        );
225
    }
226
227
    /**
228
     * @return string
229
     */
230
    public function getErrorType()
231
    {
232
        return $this->errorType;
233
    }
234
235
    /**
236
     * Generate a HTTP response.
237
     *
238
     * @param ResponseInterface $response
239
     * @param bool              $useFragment True if errors should be in the URI fragment instead of query string
240
     * @param int                $jsonOptions options passed to json_encode
241
     *
242
     * @return ResponseInterface
243
     */
244
    public function generateHttpResponse(ResponseInterface $response, $useFragment = false, $jsonOptions = 0)
245
    {
246
        $headers = $this->getHttpHeaders();
247
248
        $payload = $this->getPayload();
249
250
        if ($this->redirectUri !== null) {
251
            if ($useFragment === true) {
252
                $this->redirectUri .= (strstr($this->redirectUri, '#') === false) ? '#' : '&';
253
            } else {
254
                $this->redirectUri .= (strstr($this->redirectUri, '?') === false) ? '?' : '&';
255
            }
256
257
            return $response->withStatus(302)->withHeader('Location', $this->redirectUri . http_build_query($payload));
258
        }
259
260
        foreach ($headers as $header => $content) {
261
            $response = $response->withHeader($header, $content);
262
        }
263
264
        $response->getBody()->write(json_encode($payload, $jsonOptions));
265
266
        return $response->withStatus($this->getHttpStatusCode());
267
    }
268
269
    /**
270
     * Get all headers that have to be send with the error response.
271
     *
272
     * @return array Array with header values
273
     */
274
    public function getHttpHeaders()
0 ignored issues
show
Coding Style introduced by
getHttpHeaders uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
275
    {
276
        $headers = [
277
            'Content-type' => 'application/json',
278
        ];
279
280
        // Add "WWW-Authenticate" header
281
        //
282
        // RFC 6749, section 5.2.:
283
        // "If the client attempted to authenticate via the 'Authorization'
284
        // request header field, the authorization server MUST
285
        // respond with an HTTP 401 (Unauthorized) status code and
286
        // include the "WWW-Authenticate" response header field
287
        // matching the authentication scheme used by the client.
288
        // @codeCoverageIgnoreStart
289
        if ($this->errorType === 'invalid_client') {
290
            $authScheme = 'Basic';
291
            if (array_key_exists('HTTP_AUTHORIZATION', $_SERVER) !== false
292
                && strpos($_SERVER['HTTP_AUTHORIZATION'], 'Bearer') === 0
293
            ) {
294
                $authScheme = 'Bearer';
295
            }
296
            $headers['WWW-Authenticate'] = $authScheme . ' realm="OAuth"';
297
        }
298
        // @codeCoverageIgnoreEnd
299
        return $headers;
300
    }
301
302
    /**
303
     * Returns the HTTP status code to send when the exceptions is output.
304
     *
305
     * @return int
306
     */
307
    public function getHttpStatusCode()
308
    {
309
        return $this->httpStatusCode;
310
    }
311
312
    /**
313
     * @return null|string
314
     */
315
    public function getHint()
316
    {
317
        return $this->hint;
318
    }
319
}
320