Skip to content

Commit

Permalink
Merge pull request #12 from clearbooks/migrate-silex-to-symfony
Browse files Browse the repository at this point in the history
Require latest Dilex
  • Loading branch information
peter-horvath authored Nov 1, 2020
2 parents 39a3c90 + 504a204 commit bdbdd30
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 13 additions & 2 deletions src/JwtGuard.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace Clearbooks\Dilex;

use Clearbooks\Dilex\JwtGuard\NoJwtRequired;
use Clearbooks\Dilex\JwtGuard\RequestAuthoriser;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -21,14 +22,24 @@ public function __construct( RequestAuthoriser $authoriser )
$this->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
* @return JsonResponse|null
*/
public function execute( Request $request )
{
$controllerClass = $request->attributes->get('_controller');
$controllerClass = $this->getControllerClass( $request );
if( !($this->isJwtRequired($controllerClass))) {
return null;
}
Expand All @@ -54,4 +65,4 @@ private function isJwtRequired($controllerClass)
return $jwtRequired;
}
}
}
}
File renamed without changes.
8 changes: 2 additions & 6 deletions test/JwtGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ) ) );

}

/**
Expand All @@ -67,5 +64,4 @@ public function givenInvalidRequest_AndControllerDoesNotRequireJwt_whenExecuting
$this->request->attributes->set('_controller', MockNoJwtRequiredController::class );
$this->assertNull($guard->execute($this->request));
}

}

0 comments on commit bdbdd30

Please sign in to comment.