Skip to content

Commit eacbeb8

Browse files
committed
Fixing bug in last-auth-time update for deactivated users.
1 parent 076f312 commit eacbeb8

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

app/V1Module/presenters/LoginPresenter.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use App\Security\Roles;
2323
use App\Security\TokenScope;
2424
use Nette\Security\AuthenticationException;
25+
use Nette\Http\IResponse;
2526

2627
/**
2728
* Endpoints used to log a user in
@@ -201,6 +202,14 @@ public function actionRefresh()
201202
$token = $this->getAccessToken();
202203

203204
$user = $this->getCurrentUser();
205+
if (!$user->isAllowed()) {
206+
throw new ForbiddenRequestException(
207+
"Forbidden Request - User account was disabled",
208+
IResponse::S403_Forbidden,
209+
FrontendErrorMappings::E403_002__USER_NOT_ALLOWED
210+
);
211+
}
212+
204213
$user->updateLastAuthenticationAt();
205214
$this->users->flush();
206215

@@ -247,6 +256,14 @@ public function actionIssueRestrictedToken()
247256
$this->validateEffectiveRole($effectiveRole);
248257

249258
$user = $this->getCurrentUser();
259+
if (!$user->isAllowed()) {
260+
throw new ForbiddenRequestException(
261+
"Forbidden Request - User account was disabled",
262+
IResponse::S403_Forbidden,
263+
FrontendErrorMappings::E403_002__USER_NOT_ALLOWED
264+
);
265+
}
266+
250267
$user->updateLastAuthenticationAt();
251268
$this->users->flush();
252269

@@ -265,7 +282,7 @@ private function validateScopeRoles(?array $scopes, $expiration)
265282
{
266283
$forbiddenScopes = [
267284
TokenScope::CHANGE_PASSWORD =>
268-
"Password change tokens can only be issued through the password reset endpoint",
285+
"Password change tokens can only be issued through the password reset endpoint",
269286
TokenScope::EMAIL_VERIFICATION => "E-mail verification tokens must be received via e-mail",
270287
];
271288

app/V1Module/security/AccessManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ public function getUser(AccessToken $token): User
112112
if (!$user) {
113113
throw new ForbiddenRequestException(
114114
"Forbidden Request - User does not exist",
115-
IResponse::S403_FORBIDDEN,
115+
IResponse::S403_Forbidden,
116116
FrontendErrorMappings::E403_001__USER_NOT_EXIST
117117
);
118118
}
119119

120120
if (!$user->isAllowed()) {
121121
throw new ForbiddenRequestException(
122122
"Forbidden Request - User account was disabled",
123-
IResponse::S403_FORBIDDEN,
123+
IResponse::S403_Forbidden,
124124
FrontendErrorMappings::E403_002__USER_NOT_ALLOWED
125125
);
126126
}
@@ -148,7 +148,7 @@ public function issueToken(
148148
if (!$user->isAllowed()) {
149149
throw new ForbiddenRequestException(
150150
"Forbidden Request - User account was disabled",
151-
IResponse::S403_FORBIDDEN,
151+
IResponse::S403_Forbidden,
152152
FrontendErrorMappings::E403_002__USER_NOT_ALLOWED
153153
);
154154
}

app/V1Module/security/CredentialsAuthenticator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use App\Exceptions\FrontendErrorMappings;
66
use App\Exceptions\WrongCredentialsException;
7+
use App\Exceptions\ForbiddenRequestException;
78
use App\Model\Entity\User;
89
use App\Model\Repository\Logins;
910
use Nette;
1011
use Nette\Security\Passwords;
12+
use Nette\Http\IResponse;
1113

1214
class CredentialsAuthenticator
1315
{
@@ -40,8 +42,15 @@ public function authenticate(string $username, string $password)
4042
"The username or password is incorrect.",
4143
FrontendErrorMappings::E400_101__WRONG_CREDENTIALS_LOCAL
4244
);
45+
} elseif (!$user->isAllowed()) {
46+
throw new ForbiddenRequestException(
47+
"Forbidden Request - User account was disabled",
48+
IResponse::S403_Forbidden,
49+
FrontendErrorMappings::E403_002__USER_NOT_ALLOWED
50+
);
4351
}
4452

53+
4554
return $user;
4655
}
4756
}

app/helpers/ExternalLogin/ExternalServiceAuthenticator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Exceptions\WrongCredentialsException;
88
use App\Exceptions\InvalidExternalTokenException;
99
use App\Exceptions\InvalidArgumentException;
10+
use App\Exceptions\ForbiddenRequestException;
1011
use App\Model\Entity\Instance;
1112
use App\Model\Entity\User;
1213
use App\Model\Repository\ExternalLogins;
@@ -15,6 +16,7 @@
1516
use App\Model\Repository\Instances;
1617
use App\Helpers\EmailVerificationHelper;
1718
use Nette\Utils\Arrays;
19+
use Nette\Http\IResponse;
1820
use Firebase\JWT\JWT;
1921
use Firebase\JWT\Key;
2022
use DomainException;
@@ -148,8 +150,15 @@ public function authenticate(string $authName, string $token, string $instanceId
148150
FrontendErrorMappings::E400_104__EXTERNAL_AUTH_FAILED_USER_NOT_FOUND,
149151
["service" => $authName]
150152
);
153+
} elseif (!$user->isAllowed()) {
154+
throw new ForbiddenRequestException(
155+
"Forbidden Request - User account was disabled",
156+
IResponse::S403_Forbidden,
157+
FrontendErrorMappings::E403_002__USER_NOT_ALLOWED
158+
);
151159
}
152160

161+
153162
return $user;
154163
}
155164

0 commit comments

Comments
 (0)