From 36485830964e3dd80ece804fac694247d4fcb83b Mon Sep 17 00:00:00 2001 From: aemaddin Date: Wed, 19 Jul 2023 00:32:48 +0300 Subject: [PATCH 1/2] - [X] added a new env key to determine which jwt key should be used to return SubjectModel on authenticate method - [X] Added .idea to the gitignore file --- .gitignore | 1 + config/config.php | 11 +++++++++++ src/JWTAuth.php | 2 +- src/JWTGuard.php | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 187e79cda..9729c46b5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock build coverage.xml .phpunit.result.cache +.idea diff --git a/config/config.php b/config/config.php index f83234d16..2f25c059b 100644 --- a/config/config.php +++ b/config/config.php @@ -11,6 +11,17 @@ return [ + /* + |-------------------------------------------------------------------------- + | JWT Authentication Key + |-------------------------------------------------------------------------- + | + | You can define a specific key to return user data from the JWT + | + */ + + 'identifier' => env('JWT_IDENTIFIER', 'sub'), + /* |-------------------------------------------------------------------------- | JWT Authentication Secret diff --git a/src/JWTAuth.php b/src/JWTAuth.php index e8ab72729..bc65b3b53 100644 --- a/src/JWTAuth.php +++ b/src/JWTAuth.php @@ -60,7 +60,7 @@ public function attempt(array $credentials) */ public function authenticate() { - $id = $this->getPayload()->get('sub'); + $id = $this->getPayload()->get(config('jwt.identifier')); if (! $this->auth->byId($id)) { return false; diff --git a/src/JWTGuard.php b/src/JWTGuard.php index 31957072e..94ac8cdc8 100644 --- a/src/JWTGuard.php +++ b/src/JWTGuard.php @@ -78,7 +78,7 @@ public function user() ($payload = $this->jwt->check(true)) && $this->validateSubject() ) { - return $this->user = $this->provider->retrieveById($payload['sub']); + return $this->user = $this->provider->retrieveById($payload[config('jwt.identifier')]); } } From de460760b1fb1a60ef4dcb99d5d2ebd5f053a4f0 Mon Sep 17 00:00:00 2001 From: aemaddin Date: Wed, 19 Jul 2023 01:09:09 +0300 Subject: [PATCH 2/2] - [X] Fixes force merge for sub changed to config('jwt.identifier') --- src/JWT.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index a958071ce..a7074db66 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -250,7 +250,7 @@ protected function getClaimsArray(JWTSubject $subject) protected function getClaimsForSubject(JWTSubject $subject) { return array_merge([ - 'sub' => $subject->getJWTIdentifier(), + config('jwt.identifier') => $subject->getJWTIdentifier(), ], $this->lockSubject ? ['prv' => $this->hashSubjectModel($subject)] : []); }