Completed
Push — master ( 1de13c...bf55ce )
by Alex
33:38
created

OAuthServerException::generateHttpResponse()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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