Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require latest Dilex #12

Merged
merged 5 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}
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));
}

}