|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace sample\components; |
|
4
|
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\user\Oauth2OidcUserComponentInterface; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\db\Exception; |
|
8
|
|
|
use yii\web\User; |
|
9
|
|
|
|
|
10
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong -- Sample documentation |
|
11
|
|
|
class UserComponent extends User implements |
|
12
|
|
|
Oauth2OidcUserComponentInterface # Optional interface, only required when 'Open ID Connect' is used. |
|
13
|
|
|
{ |
|
14
|
|
|
// phpcs:enable Generic.Files.LineLength.TooLong |
|
15
|
|
|
|
|
16
|
|
|
# region Oauth2OidcUserComponentInterface |
|
17
|
|
|
/** |
|
18
|
|
|
* @inheritDoc |
|
19
|
|
|
*/ |
|
20
|
|
|
public function reauthenticationRequired($clientAuthorizationRequest) |
|
21
|
|
|
{ |
|
22
|
|
|
return Yii::$app->response->redirect([ |
|
23
|
|
|
'user/login', |
|
24
|
|
|
'reauthenticate' => true, |
|
25
|
|
|
'clientAuthorizationRequestId' => $clientAuthorizationRequest->getRequestId(), |
|
26
|
|
|
]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function accountSelectionRequired($clientAuthorizationRequest) |
|
33
|
|
|
{ |
|
34
|
|
|
return Yii::$app->response->redirect([ |
|
35
|
|
|
'user/select-account', |
|
36
|
|
|
'clientAuthorizationRequestId' => $clientAuthorizationRequest->getRequestId(), |
|
37
|
|
|
]); |
|
38
|
|
|
} |
|
39
|
|
|
# endregion Oauth2OidcUserComponentInterface |
|
40
|
|
|
|
|
41
|
|
|
# region Updates User's `latest_authenticated_at` |
|
42
|
|
|
# which is used for Oauth2OidcUserInterface::getLatestAuthenticatedAt() |
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
* @param \sample\models\User $identity |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function afterLogin($identity, $cookieBased, $duration) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::afterLogin($identity, $cookieBased, $duration); |
|
50
|
|
|
|
|
51
|
|
|
$identity->latest_authenticated_at = time(); |
|
52
|
|
|
if (!$identity->save()) { |
|
53
|
|
|
throw new Exception('Could not save user.'); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
# endregion |
|
57
|
|
|
} |
|
58
|
|
|
|