-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignPresenterTrait.php
More file actions
209 lines (172 loc) · 6.39 KB
/
Copy pathSignPresenterTrait.php
File metadata and controls
209 lines (172 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace ADT\FancyAdmin\UI\Presenters\Sign;
use ADT\FancyAdmin\DI\Injects\AuthenticatorInject;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\DI\Injects\FancyAdminInject;
use ADT\FancyAdmin\DI\Injects\SecurityUserInject;
use ADT\FancyAdmin\Model\Entities\Identity;
use ADT\FancyAdmin\Model\Security\Keycloak\KeycloakSessionSection;
use ADT\FancyAdmin\UI\Components\Forms\LostPassword\LostPasswordForm;
use ADT\FancyAdmin\UI\Components\Forms\LostPassword\LostPasswordFormFactory;
use ADT\FancyAdmin\UI\Components\Forms\NewPassword\NewPasswordForm;
use ADT\FancyAdmin\UI\Components\Forms\NewPassword\NewPasswordFormFactory;
use ADT\FancyAdmin\UI\Components\Forms\SignIn\SignInForm;
use ADT\FancyAdmin\UI\Components\Forms\SignIn\SignInFormFactory;
use ADT\FancyAdmin\UI\Presenters\PresenterTrait;
use ADT\FancyAdmin\UI\RedirectAfterLoginTrait;
use Nette\Application\Attributes\Persistent;
use Nette\Security\AuthenticationException;
use Nette\Utils\Validators;
trait SignPresenterTrait
{
use PresenterTrait;
use RedirectAfterLoginTrait;
use EntityManagerInject;
use FancyAdminInject;
use SecurityUserInject;
use AuthenticatorInject;
#[Persistent]
public ?string $token = null;
protected Identity $identity;
public function actionIn(?string $errorMsg): void
{
if ($this->getUser()->isLoggedIn()) {
$this->redirectAfterLogin();
}
// Automatický pokus o SSO přihlášení (silent check, prompt=none).
// Pokud má uživatel aktivní session v Keycloaku a v aplikaci existuje
// odpovídající identita, přihlásí se rovnou bez zadávání údajů.
$this->attemptSilentSso();
if ($errorMsg) {
$this->flashMessageError($errorMsg);
}
if ($this->getParameter('fraudDetected')) {
$this->flashMessageError('_fcadmin.modules.web.presenters.sign.flashFraud');
}
}
/**
* Zkusí nepřihlášeného uživatele automaticky přihlásit přes existující Keycloak SSO session.
*
* Pro každou SSO instanci provede jeden silent check (prompt=none). Keycloak buď vrátí
* authorization code (existuje aktivní SSO session) a uživatel se přihlásí v callbacku,
* nebo vrátí chybu (login_required) a uživatel se vrátí sem na formulář.
*
* Každá instance se v rámci jedné session zkusí maximálně jednou (kvůli ochraně před smyčkou).
*/
private function attemptSilentSso(): void
{
if (!$this->_fancyAdmin->isKeycloakEnabled()) {
return;
}
$manager = $this->_fancyAdmin->getKeycloakManager();
if ($manager === null || !$manager->hasInstances()) {
return;
}
$keycloakSession = $this->getSession(KeycloakSessionSection::SECTION_NAME);
// Po explicitním odhlášení přeskočíme jeden silent SSO pokus,
// jinak by uživatele mohlo hned znovu přihlásit a nešlo by se odhlásit.
if ($keycloakSession->get(KeycloakSessionSection::SSO_SUPPRESS_SILENT)) {
$keycloakSession->remove(KeycloakSessionSection::SSO_SUPPRESS_SILENT);
return;
}
$tried = $keycloakSession->get(KeycloakSessionSection::SSO_SILENT_TRIED) ?? [];
foreach ($manager->getInstanceNames() as $name) {
if (in_array($name, $tried, true)) {
continue;
}
$keycloak = $manager->getInstance($name);
if ($keycloak === null) {
continue;
}
// Pojistka proti chybné konfiguraci — bez validní absolutní hostUrl
// by se vygenerovala rozbitá relativní redirect URL.
if (!Validators::isUrl($keycloak->getHostUrl())) {
continue;
}
$tried[] = $name;
$keycloakSession->set(KeycloakSessionSection::SSO_SILENT_TRIED, $tried);
// Po úspěšném (i neúspěšném) silent checku se vrátíme na tuto přihlašovací stránku
// (včetně případného backlinku), odkud se buď pokračuje na další instanci, nebo se zobrazí formulář.
$backRedirect = $this->getHttpRequest()->getUrl()->getAbsoluteUrl();
$this->redirectUrl($keycloak->getSilentLoginUrl($backRedirect));
}
}
/**
* Potlačí jeden následující automatický silent SSO pokus na přihlašovací stránce.
* Voláme při explicitním odhlášení, aby uživatele po logoutu hned znovu nepřihlásilo.
*/
private function suppressSilentSso(): void
{
if (!$this->_fancyAdmin->isKeycloakEnabled()) {
return;
}
$this->getSession(KeycloakSessionSection::SECTION_NAME)
->set(KeycloakSessionSection::SSO_SUPPRESS_SILENT, true);
}
public function actionOut(): never
{
if ($this->getUser()->isLoggedIn()) {
$this->suppressSilentSso();
// Pokud je Keycloak zapnutý a uživatel se přihlásil přes Keycloak, přesměrujeme na Keycloak logout
if ($this->_fancyAdmin->isKeycloakEnabled()) {
$keycloak = $this->_fancyAdmin->getKeycloakManager()?->getInstanceFromSession();
if ($keycloak === null) {
// Fallback — zkusíme najít instanci dle identity
$keycloak = $this->_fancyAdmin->getKeycloakManager()?->getInstanceForIdentity($this->getUser()->getIdentity());
}
$redirectUrl = $this->getPresenter()->link(':Portal:Sign:in');
$logoutUrl = $keycloak?->getLogoutUrl($redirectUrl);
if ($logoutUrl !== null) {
$this->getUser()->logout(true);
$this->redirectUrl($logoutUrl);
}
}
$this->getUser()->logout(true);
}
$this->redirect('in');
}
public function actionOutAll(): never
{
if ($this->getUser()->isLoggedIn()) {
$this->suppressSilentSso();
$this->_authenticator->clearIdentity(
$this->getUser()->getIdentity()->getAuthObjectId()
);
$this->getUser()->logout(true);
}
$this->redirect('in');
}
public function actionNewPassword(string $token): void
{
$this->user->logout(clearIdentity: true);
try {
$this->identity = $this->_authenticator->authenticate($token);
} catch(AuthenticationException) {
$this->flashMessageError('fcadmin.presenters.sign.errors.expiredLink');
$this->redirect(':Portal:Sign:lostPassword');
}
}
public function actionPasswordSet(): void
{
$this->template->canContinue = $this->getUser()->isLoggedIn();
}
public function handleContinue(): void
{
$this->redirectAfterLogin();
}
public function actionLostPassword(): void
{
}
public function createComponentSignInForm(SignInFormFactory $factory): SignInForm
{
return $factory->create();
}
public function createComponentLostPasswordForm(LostPasswordFormFactory $factory): LostPasswordForm
{
return $factory->create();
}
public function createComponentNewPasswordForm(NewPasswordFormFactory $factory): NewPasswordForm
{
return $factory->create()->setEntity($this->identity);
}
}