feat: implement JWT authentication and scope validation middleware #11

Merged
rrise merged 7 commits from feat/jwt-middlewares into master 2025-11-07 17:14:24 +00:00
2 changed files with 11 additions and 6 deletions
Showing only changes of commit c9d0e337c7 - Show all commits

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) {