feat: refactor JWT middleware and update annotations for guards
Some checks failed
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Failing after 1m1s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 1m41s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Has been cancelled
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Has been cancelled
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Has been cancelled
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Has been cancelled

This commit is contained in:
2025-11-07 11:04:29 -05:00
parent d10ad2065c
commit c9d0e337c7
2 changed files with 11 additions and 6 deletions

View File

@@ -6,8 +6,7 @@ namespace Siteworxpro\App\Controllers;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Siteworxpro\App\Annotations\Guards\Jwt; use Siteworxpro\App\Annotations\Guards;
use Siteworxpro\App\Annotations\Guards\Scope;
use Siteworxpro\App\Http\JsonResponseFactory; use Siteworxpro\App\Http\JsonResponseFactory;
/** /**
@@ -22,8 +21,8 @@ class IndexController extends Controller
* *
* @throws \JsonException * @throws \JsonException
*/ */
#[Jwt] #[Guards\Jwt]
#[Scope(['get.index'])] #[Guards\Scope(['get.index'])]
public function get(ServerRequest $request): ResponseInterface public function get(ServerRequest $request): ResponseInterface
{ {
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']); return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);
@@ -32,8 +31,8 @@ class IndexController extends Controller
/** /**
* @throws \JsonException * @throws \JsonException
*/ */
#[Jwt] #[Guards\Jwt]
#[Scope(['post.index'])] #[Guards\Scope(['post.index'])]
public function post(ServerRequest $request): ResponseInterface public function post(ServerRequest $request): ResponseInterface
{ {
return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']); return JsonResponseFactory::createJsonResponse(['status_code' => 200, 'message' => 'Server is running']);

View File

@@ -10,6 +10,7 @@ use Lcobucci\JWT\JwtFacade;
use Lcobucci\JWT\Signer\Hmac\Sha256 as Hmac256; use Lcobucci\JWT\Signer\Hmac\Sha256 as Hmac256;
use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha256; use Lcobucci\JWT\Signer\Rsa\Sha256;
use Lcobucci\JWT\Token\InvalidTokenStructure;
use Lcobucci\JWT\Validation\Constraint\IssuedBy; use Lcobucci\JWT\Validation\Constraint\IssuedBy;
use Lcobucci\JWT\Validation\Constraint\LooseValidAt; use Lcobucci\JWT\Validation\Constraint\LooseValidAt;
use Lcobucci\JWT\Validation\Constraint\PermittedFor; use Lcobucci\JWT\Validation\Constraint\PermittedFor;
@@ -99,6 +100,11 @@ class JwtMiddleware implements MiddlewareInterface
'message' => 'Unauthorized: Invalid token', 'message' => 'Unauthorized: Invalid token',
'errors' => $violations 'errors' => $violations
], CodesEnum::UNAUTHORIZED); ], CodesEnum::UNAUTHORIZED);
} catch (InvalidTokenStructure) {
return JsonResponseFactory::createJsonResponse([
'status_code' => 401,
'message' => 'Unauthorized: Invalid token',
], CodesEnum::UNAUTHORIZED);
} }
foreach ($jwt->claims()->all() as $item => $value) { foreach ($jwt->claims()->all() as $item => $value) {