diff --git a/composer.json b/composer.json index b0d6c8f..978511d 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,12 @@ { "name": "clearbooks/dilex-jwt", "description": "JWT authentication middleware for Dilex", - "type": "project", + "type": "library", "require": { "php": ">=7.2", - "clearbooks/dilex": "^2.0", - "emarref/jwt": "^1.0.3" + "clearbooks/dilex": "^3.0.0-alpha.1", + "emarref/jwt": "^1.0.3", + "ext-json": "*" }, "require-dev": { diff --git a/src/JwtGuard.php b/src/JwtGuard.php index 8cb5605..f730b9e 100644 --- a/src/JwtGuard.php +++ b/src/JwtGuard.php @@ -1,5 +1,6 @@ authoriser = $authoriser; } + private function getControllerClass( Request $request ) + { + $controllerAttribute = $request->attributes->get( "_controller" ); + if ( is_array( $controllerAttribute ) ) { + return $controllerAttribute[0]; + } + + return $controllerAttribute; + } + /** * Authorise this request * @param Request $request @@ -28,7 +39,7 @@ public function __construct( RequestAuthoriser $authoriser ) */ public function execute( Request $request ) { - $controllerClass = $request->attributes->get('_controller'); + $controllerClass = $this->getControllerClass( $request ); if( !($this->isJwtRequired($controllerClass))) { return null; } @@ -54,4 +65,4 @@ private function isJwtRequired($controllerClass) return $jwtRequired; } } -} \ No newline at end of file +} diff --git a/test/JwtTokenAuthenticatorTest.php b/test/JwtGuard/JwtTokenAuthenticatorTest.php similarity index 100% rename from test/JwtTokenAuthenticatorTest.php rename to test/JwtGuard/JwtTokenAuthenticatorTest.php diff --git a/test/JwtGuardTest.php b/test/JwtGuardTest.php index 986d5ad..763f535 100644 --- a/test/JwtGuardTest.php +++ b/test/JwtGuardTest.php @@ -6,17 +6,15 @@ * Time: 11:44 */ -namespace Clearbooks\Dilex\JwtGuard; +namespace Clearbooks\Dilex; - -use Clearbooks\Dilex\JwtGuard; +use Clearbooks\Dilex\JwtGuard\MockNoJwtRequiredController; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; class JwtGuardTest extends TestCase { - private $request; public function setUp(): void @@ -46,7 +44,6 @@ public function givenUnauthorisedRequest_returnError() { $guard = new JwtGuard( new JwtGuard\RequestAuthoriserStub( false ) ); $this->assertContains( 'error', array_keys( json_decode( $guard->execute( new Request )->getContent(), true ) ) ); - } /** @@ -67,5 +64,4 @@ public function givenInvalidRequest_AndControllerDoesNotRequireJwt_whenExecuting $this->request->attributes->set('_controller', MockNoJwtRequiredController::class ); $this->assertNull($guard->execute($this->request)); } - }