|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\controllers\web\server\base; |
|
4
|
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
6
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebAction; |
|
7
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebController; |
|
8
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\UrlHelper; |
|
9
|
|
|
use Yii; |
|
10
|
|
|
use yii\base\ErrorException; |
|
11
|
|
|
use yii\base\Exception; |
|
12
|
|
|
use yii\base\UserException; |
|
13
|
|
|
use yii\web\HttpException; |
|
14
|
|
|
use yii\web\Response; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property Oauth2BaseWebController $controller |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Oauth2BaseServerAction extends Oauth2BaseWebAction |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param \Exception $exception |
|
23
|
|
|
* @return Response |
|
24
|
|
|
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-5.2 |
|
25
|
|
|
*/ |
|
26
|
6 |
|
protected function processException($exception, $logCategory) |
|
27
|
|
|
{ |
|
28
|
6 |
|
$module = $this->controller->module; |
|
29
|
|
|
|
|
30
|
6 |
|
if (Yii::$app->has('response')) { |
|
31
|
5 |
|
$response = Yii::$app->getResponse(); |
|
32
|
|
|
// reset parameters of response to avoid interference with partially created response data |
|
33
|
|
|
// in case the error occurred while sending the response. |
|
34
|
5 |
|
$response->isSent = false; |
|
35
|
5 |
|
$response->stream = null; |
|
36
|
5 |
|
$response->data = null; |
|
|
|
|
|
|
37
|
5 |
|
$response->content = null; |
|
38
|
|
|
} else { |
|
39
|
1 |
|
$response = new Response(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
if ($exception instanceof OAuthServerException) { |
|
43
|
2 |
|
if ($exception->hasRedirect()) { |
|
44
|
1 |
|
return $this->controller->redirect(UrlHelper::addQueryParams($exception->getRedirectUri(), [ |
|
45
|
1 |
|
'error' => $exception->getErrorType() |
|
46
|
1 |
|
])); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
$response->setStatusCode($exception->getHttpStatusCode()); |
|
50
|
|
|
|
|
51
|
1 |
|
$error = $exception->getErrorType(); |
|
52
|
1 |
|
$hint = $exception->getHint(); |
|
53
|
1 |
|
$description = $exception->getMessage() . ($hint ? ' ' . $hint : ''); |
|
54
|
|
|
} else { |
|
55
|
4 |
|
$response->setStatusCodeByException($exception); |
|
56
|
|
|
|
|
57
|
4 |
|
$error = ($exception instanceof Exception || $exception instanceof ErrorException) |
|
58
|
4 |
|
? $exception->getName() |
|
59
|
4 |
|
: 'Exception'; |
|
60
|
|
|
|
|
61
|
4 |
|
$displayNonHttpExceptionMessages = $module->displayConfidentialExceptionMessages !== null |
|
62
|
2 |
|
? $module->displayConfidentialExceptionMessages |
|
63
|
4 |
|
: YII_DEBUG; |
|
64
|
|
|
|
|
65
|
|
|
if ( |
|
66
|
4 |
|
!$displayNonHttpExceptionMessages |
|
67
|
4 |
|
&& !$exception instanceof UserException |
|
68
|
4 |
|
&& !$exception instanceof HttpException |
|
69
|
|
|
) { |
|
70
|
1 |
|
$description = Yii::t('yii', 'An internal server error occurred.'); |
|
71
|
|
|
} else { |
|
72
|
3 |
|
$description = (string)$exception; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
5 |
|
$response->data = [ |
|
77
|
5 |
|
'error' => $error, |
|
78
|
5 |
|
'error_description' => $description, |
|
79
|
5 |
|
]; |
|
80
|
|
|
|
|
81
|
5 |
|
if ($response->getIsClientError()) { |
|
82
|
1 |
|
if ($module->httpClientErrorsLogLevel) { |
|
|
|
|
|
|
83
|
1 |
|
Yii::getLogger()->log((string)$exception, $module->httpClientErrorsLogLevel, $logCategory); |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
4 |
|
Yii::error((string)$exception, $logCategory); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
return $response; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|