|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\controllers\web\openidconnect; |
|
4
|
|
|
|
|
5
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
|
6
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebAction; |
|
7
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2OidcController; |
|
8
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\EndSession\Oauth2EndSessionAuthorizationRequestInterface; |
|
9
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\openidconnect\Oauth2OidcEndSessionActionInterface; |
|
10
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\InvalidConfigException; |
|
13
|
|
|
use yii\web\BadRequestHttpException; |
|
14
|
|
|
use yii\web\ForbiddenHttpException; |
|
15
|
|
|
use yii\web\NotFoundHttpException; |
|
16
|
|
|
use yii\web\Response; |
|
17
|
|
|
use yii\web\UnauthorizedHttpException; |
|
|
|
|
|
|
18
|
|
|
// phpcs:enable Generic.Files.LineLength.TooLong |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @property Oauth2OidcController $controller |
|
22
|
|
|
*/ |
|
23
|
|
|
class Oauth2OidcEndSessionAction extends Oauth2BaseWebAction implements Oauth2OidcEndSessionActionInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @see https://openid.net/specs/openid-connect-rpinitiated-1_0.html |
|
27
|
|
|
* @return Response |
|
28
|
|
|
* @throws InvalidConfigException |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function run($endSessionAuthorizationRequestId = null) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$module = $this->controller->module; |
|
33
|
1 |
|
$request = Yii::$app->request; |
|
|
|
|
|
|
34
|
1 |
|
$identity = $module->getUserIdentity(); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
1 |
|
if (!$module->enableOpenIdConnect) { |
|
37
|
|
|
throw new ForbiddenHttpException('OpenID Connect is disabled.'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
if ($identity && !($identity instanceof Oauth2OidcUserInterface)) { |
|
|
|
|
|
|
41
|
|
|
throw new InvalidConfigException('In order to support OpenID Connect ' |
|
42
|
|
|
. get_class($identity) . ' must implement ' . Oauth2OidcUserInterface::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
if (empty($endSessionAuthorizationRequestId)) { |
|
46
|
|
|
/** @var Oauth2EndSessionAuthorizationRequestInterface $endSessionAuthorizationRequest */ |
|
47
|
1 |
|
$endSessionAuthorizationRequest = Yii::createObject([ |
|
48
|
1 |
|
'class' => Oauth2EndSessionAuthorizationRequestInterface::class, |
|
49
|
1 |
|
'module' => $module, |
|
50
|
1 |
|
'idTokenHint' => $this->getRequestParam($request, 'id_token_hint'), |
|
|
|
|
|
|
51
|
1 |
|
'clientIdentifier' => $this->getRequestParam($request, 'client_id'), |
|
52
|
1 |
|
'endSessionUrl' => $request->absoluteUrl, |
|
53
|
1 |
|
'redirectUri' => $this->getRequestParam($request, 'post_logout_redirect_uri'), |
|
54
|
1 |
|
'state' => $this->getRequestParam($request, 'state'), |
|
55
|
1 |
|
]); |
|
56
|
|
|
|
|
57
|
1 |
|
$endSessionAuthorizationRequest->validateRequest(); |
|
58
|
|
|
|
|
59
|
1 |
|
if (!$identity) { |
|
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Specified in https://openid.net/specs/openid-connect-rpinitiated-1_0.html#ValidationAndErrorHandling |
|
62
|
|
|
* "Note that because RP-Initiated Logout Requests are intended to be idempotent, |
|
63
|
|
|
* it is explicitly not an error for an RP to request that a logout be performed when the OP does not |
|
64
|
|
|
* consider that the End-User is logged in with the OP at the requesting RP." |
|
65
|
|
|
*/ |
|
66
|
|
|
return $this->controller->redirect($endSessionAuthorizationRequest->getRequestCompletedRedirectUrl(true)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
if ($endSessionAuthorizationRequest->getEndUserAuthorizationRequired()) { |
|
70
|
1 |
|
if ($endSessionAuthorizationRequest->isAuthorizationAllowed()) { |
|
71
|
1 |
|
return $module->generateEndSessionAuthReqRedirectResponse($endSessionAuthorizationRequest); |
|
72
|
|
|
} else { |
|
73
|
|
|
throw new UnauthorizedHttpException(Yii::t('oauth2', 'You are not allowed to authorize logging out.')); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
$endSessionAuthorizationRequest->autoApproveAndProcess(); |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
1 |
|
$endSessionAuthorizationRequest = $module->getEndSessionAuthReqSession($endSessionAuthorizationRequestId); |
|
80
|
1 |
|
if (empty($endSessionAuthorizationRequest)) { |
|
81
|
|
|
throw new NotFoundHttpException('End Session authorization request not found.'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
if (!$endSessionAuthorizationRequest->isCompleted()) { |
|
86
|
|
|
throw new BadRequestHttpException('End Session authorization is not completed.'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return $this->controller->redirect($endSessionAuthorizationRequest->getRequestCompletedRedirectUrl()); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|