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 33 additions and 6 deletions
Showing only changes of commit 9f77e70b25 - Show all commits

View File

@@ -36,6 +36,11 @@ class JwtMiddleware implements MiddlewareInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface|Dispatcher $handler): ResponseInterface
{
if (!$handler instanceof Dispatcher) {
return $handler->handle($request);
}
/** @var Route | null $lastSegment */
$lastSegment = array_last($handler->getMiddlewareStack());
@@ -43,10 +48,19 @@ class JwtMiddleware implements MiddlewareInterface
return $handler->handle($request);
}
[$class, $method] = $lastSegment->getCallable();
$callable = $lastSegment->getCallable();
$class = null;
$method = null;
if (class_exists($class::class)) {
$reflectionClass = new \ReflectionClass($class::class);
if (is_array($callable) && count($callable) === 2) {
[$class, $method] = $callable;
} elseif (is_string($callable)) {
// Handle the case where the callable is a string (e.g., 'ClassName::methodName')
[$class, $method] = explode('::', $callable);
}
if (class_exists($class)) {
$reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->hasMethod($method)) {
$reflectionMethod = $reflectionClass->getMethod($method);

View File

@@ -21,6 +21,10 @@ class ScopeMiddleware implements MiddlewareInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface | Dispatcher $handler): ResponseInterface
{
if (!$handler instanceof Dispatcher) {
return $handler->handle($request);
}
/** @var Route | null $lastSegment */
$lastSegment = array_last($handler->getMiddlewareStack());
@@ -28,10 +32,19 @@ class ScopeMiddleware implements MiddlewareInterface
return $handler->handle($request);
}
[$class, $method] = $lastSegment->getCallable();
$callable = $lastSegment->getCallable();
$class = null;
$method = null;
if (class_exists($class::class)) {
$reflectionClass = new \ReflectionClass($class::class);
if (is_array($callable) && count($callable) === 2) {
[$class, $method] = $callable;
} elseif (is_string($callable)) {
// Handle the case where the callable is a string (e.g., 'ClassName::methodName')
[$class, $method] = explode('::', $callable);
}
if (class_exists($class)) {
$reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->hasMethod($method)) {
$reflectionMethod = $reflectionClass->getMethod($method);
$attributes = $reflectionMethod->getAttributes(Scope::class);