Skip to content

Commit 140435c

Browse files
committed
feat: add token purpose for fragmented security
1 parent e9696ef commit 140435c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Auth.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public function parseToken()
763763
* @param string $token The token to verify
764764
* @return User|null
765765
*/
766-
public function verifyToken(string $token)
766+
public function verifyToken(string $token, ?string $purpose = null)
767767
{
768768
try {
769769
$decodedToken = (array) JWT::decode(
@@ -788,6 +788,11 @@ public function verifyToken(string $token)
788788
return null;
789789
}
790790

791+
if ($purpose && (!isset($decodedToken['token.purpose']) || $decodedToken['token.purpose'] !== $purpose)) {
792+
$this->errorsArray['token'] = 'Invalid token';
793+
return null;
794+
}
795+
791796
return $user;
792797
} catch (\Throwable $th) {
793798
$this->errorsArray['token'] = $th->getMessage();

src/Auth/User.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,10 @@ public function generateToken($tokenLifetime): string
154154
/**
155155
* Generate a verification token for the user
156156
* @param mixed $expiresIn Token expiration time
157+
* @param string|null $purpose Purpose of the token
157158
* @return string
158159
*/
159-
public function generateVerificationToken($expiresIn = null): string
160+
public function generateVerificationToken($expiresIn = null, ?string $purpose = null): string
160161
{
161162
$userIdKey = Config::get('id.key');
162163
$secretPhrase = Config::get('token.secret') . '-verification';
@@ -169,6 +170,10 @@ public function generateVerificationToken($expiresIn = null): string
169170
'iss' => $_SERVER['HTTP_HOST'] ?? 'localhost',
170171
];
171172

173+
if ($purpose) {
174+
$payload['token.purpose'] = $purpose;
175+
}
176+
172177
return JWT::encode($payload, $secretPhrase, 'HS256');
173178
}
174179

0 commit comments

Comments
 (0)