From f67ac28514fda808880c64371281b98e6d1b4d6a Mon Sep 17 00:00:00 2001 From: James Rickard Date: Fri, 29 Jan 2016 08:10:27 -0500 Subject: [PATCH 1/3] Updated tests to work with the Symfony 3.0 changes. --- Tests/Security/JWTAuthenticatorTest.php | 19 ++++++++++++++----- src/Security/JWTAuthenticator.php | 7 ++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Tests/Security/JWTAuthenticatorTest.php b/Tests/Security/JWTAuthenticatorTest.php index 55ebe44..9d02881 100644 --- a/Tests/Security/JWTAuthenticatorTest.php +++ b/Tests/Security/JWTAuthenticatorTest.php @@ -4,21 +4,29 @@ use Auth0\JWTAuthBundle\Security\Auth0Service; use Auth0\JWTAuthBundle\Security\JWTAuthenticator; -use Symfony\Component\HttpFoundation\Request; class JWTAuthenticatorTest extends \PHPUnit_Framework_TestCase { public function testTokenCreation() { - $authenticator = new JWTAuthenticator(new Auth0Service('client_id', null, null)); + $mockAuth0 = $this->getMockBuilder('Auth0\JWTAuthBundle\Security\Auth0Service') + ->disableOriginalConstructor() + ->getMock(); + + $mockAuth0->expects($this->once()) + ->method('decodeJWT') + ->will($this->returnValue(new \stdClass())); + + $authenticator = new JWTAuthenticator($mockAuth0); $providerKey = 'providerKey'; //generated with http://jwt.io/ $JWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2RvbWFpbi5jb20vIiwic3ViIjoiYXV0aDB8MDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImF1ZCI6ImNsaWVudF9pZCIsImV4cCI6MTQyMjQ0MDI3MSwiaWF0IjoxNDIyNDA0MjcxfQ.xSuCAetwfHpCWhE_5NqTrwHq0eQ7CVffQwgSqTHwwrY'; - $request = $this->getMock('Request'); - $request->headers = $this->getMock('ParameterBag'); - $request->request + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); + $request->headers = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag'); + + $request->headers ->expects($this->atLeastOnce()) ->method('get') ->with('Authorization') @@ -30,4 +38,5 @@ public function testTokenCreation() $this->assertEquals($providerKey,$token->getProviderKey()); $this->assertEquals($providerKey,$token->getProviderKey()); } + } diff --git a/src/Security/JWTAuthenticator.php b/src/Security/JWTAuthenticator.php index b359f9f..572e414 100644 --- a/src/Security/JWTAuthenticator.php +++ b/src/Security/JWTAuthenticator.php @@ -6,19 +6,20 @@ use Doctrine\Common\Proxy\Exception\InvalidArgumentException; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\DependencyInjection\ContainerAware; +use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\Exception\BadCredentialsException; -use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface; +use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; use Auth0\JWTAuthBundle\Security\Core\JWTUserProviderInterface; -class JWTAuthenticator extends ContainerAware implements SimplePreAuthenticatorInterface,AuthenticationFailureHandlerInterface +class JWTAuthenticator implements SimplePreAuthenticatorInterface,AuthenticationFailureHandlerInterface { + use ContainerAwareTrait; protected $auth0Service; public function __construct(Auth0Service $auth0Service) From 80512a5e98eaed85c10d47daba845310b3e5717f Mon Sep 17 00:00:00 2001 From: James Rickard Date: Fri, 29 Jan 2016 08:15:29 -0500 Subject: [PATCH 2/3] Bootstrap the test process. Updated composer to require Symfony, which has the PHP specification in it (>=5.5.9). --- Tests/bootstrap.php | 14 ++++++++++++++ composer.json | 1 + phpunit.xml.dist | 15 +++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 Tests/bootstrap.php create mode 100644 phpunit.xml.dist diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php new file mode 100644 index 0000000..bf2c05c --- /dev/null +++ b/Tests/bootstrap.php @@ -0,0 +1,14 @@ +add('Auth0\\JWTAuthBundle\\', __DIR__); diff --git a/composer.json b/composer.json index ad03bc1..2fd0b1c 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "require": { "php": ">=5.3.3", + "symfony/symfony": "~3.0", "auth0/auth0-php": "~3.0" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..bdb1848 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,15 @@ + + + + + + tests/ + + + + + + src/ + + + From 96aa61afa89a1f4d26cdb2ccf4a09e4aca0a979b Mon Sep 17 00:00:00 2001 From: James Rickard Date: Fri, 29 Jan 2016 08:30:32 -0500 Subject: [PATCH 3/3] Updated test to include test for no authorization. --- Tests/Security/JWTAuthenticatorTest.php | 34 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Tests/Security/JWTAuthenticatorTest.php b/Tests/Security/JWTAuthenticatorTest.php index 9d02881..a210279 100644 --- a/Tests/Security/JWTAuthenticatorTest.php +++ b/Tests/Security/JWTAuthenticatorTest.php @@ -10,8 +10,8 @@ class JWTAuthenticatorTest extends \PHPUnit_Framework_TestCase public function testTokenCreation() { $mockAuth0 = $this->getMockBuilder('Auth0\JWTAuthBundle\Security\Auth0Service') - ->disableOriginalConstructor() - ->getMock(); + ->disableOriginalConstructor() + ->getMock(); $mockAuth0->expects($this->once()) ->method('decodeJWT') @@ -35,8 +35,34 @@ public function testTokenCreation() $token = $authenticator->createToken($request, $providerKey); $this->assertEquals('anon.',$token->GetUser()); - $this->assertEquals($providerKey,$token->getProviderKey()); - $this->assertEquals($providerKey,$token->getProviderKey()); + $this->assertEquals($providerKey, $token->getProviderKey()); + $this->assertEquals($providerKey, $token->getProviderKey()); + } + + public function testNoAuthorization() + { + $mockAuth0 = $this->getMockBuilder('Auth0\JWTAuthBundle\Security\Auth0Service') + ->disableOriginalConstructor() + ->getMock(); + + $authenticator = new JWTAuthenticator($mockAuth0); + $providerKey = 'providerKey'; + + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); + $request->headers = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag'); + + $request->headers + ->expects($this->once()) + ->method('get') + ->with('Authorization') + ->will($this->returnValue(NULL)); + + $token = $authenticator->createToken($request, $providerKey); + + $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', $token); + + $this->assertEquals($providerKey, $token->getProviderKey()); + $this->assertNull($token->getCredentials()); } }