Skip to content

Commit 8c7d785

Browse files
committed
Ensure a created_at value is set on recovery tokens; closes pterodactyl#3163
1 parent 983a337 commit 8c7d785

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

app/Exceptions/Service/User/TwoFactorAuthenticationTokenInvalid.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@
66

77
class TwoFactorAuthenticationTokenInvalid extends DisplayException
88
{
9+
/**
10+
* TwoFactorAuthenticationTokenInvalid constructor.
11+
*/
12+
public function __construct()
13+
{
14+
parent::__construct('The provided two-factor authentication token was not valid.');
15+
}
916
}

app/Http/Controllers/Api/Client/TwoFactorController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ public function index(Request $request)
7272
*
7373
* @return \Illuminate\Http\JsonResponse
7474
*
75+
* @throws \Throwable
7576
* @throws \Illuminate\Validation\ValidationException
7677
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
7778
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
7879
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
79-
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
80-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
8180
* @throws \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid
8281
*/
8382
public function store(Request $request)

app/Services/Users/ToggleTwoFactorService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function handle(User $user, string $token, bool $toggleState = null): arr
7474
$isValidToken = $this->google2FA->verifyKey($secret, $token, config()->get('pterodactyl.auth.2fa.window'));
7575

7676
if (!$isValidToken) {
77-
throw new TwoFactorAuthenticationTokenInvalid('The token provided is not valid.');
77+
throw new TwoFactorAuthenticationTokenInvalid();
7878
}
7979

8080
return $this->connection->transaction(function () use ($user, $toggleState) {
@@ -94,6 +94,9 @@ public function handle(User $user, string $token, bool $toggleState = null): arr
9494
$inserts[] = [
9595
'user_id' => $user->id,
9696
'token' => password_hash($token, PASSWORD_DEFAULT),
97+
// insert() won't actually set the time on the models, so make sure we do this
98+
// manually here.
99+
'created_at' => Carbon::now(),
97100
];
98101

99102
$tokens[] = $token;

tests/Integration/Api/Client/TwoFactorControllerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public function testTwoFactorCanBeEnabledOnAccount()
101101
$tokens = RecoveryToken::query()->where('user_id', $user->id)->get();
102102
$this->assertCount(10, $tokens);
103103
$this->assertStringStartsWith('$2y$10$', $tokens[0]->token);
104+
// Ensure the recovery tokens that were created include a "created_at" timestamp
105+
// value on them.
106+
//
107+
// @see https://github.com/pterodactyl/panel/issues/3163
108+
$this->assertNotNull($tokens[0]->created_at);
104109

105110
$tokens = $tokens->pluck('token')->toArray();
106111

0 commit comments

Comments
 (0)