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

Added support for cache #51

Merged
merged 5 commits into from
Jul 17, 2017
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
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
language: php

php:
- 5.5
- 7.1

env:
global:
- TEST_COMMAND="vendor/bin/phpunit"
matrix:
- SYMFONY_VERSION=3.3.*
- SYMFONY_VERSION=2.8.*

matrix:
fast_finish: true
include:
- php: 5.5
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_VERSION=2.8.* TEST_COMMAND="vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
- php: hhvm
dist: trusty
env: SYMFONY_VERSION=3.3.*

install:
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction

script:
- $TEST_COMMAND
47 changes: 47 additions & 0 deletions Tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Auth0\JWTAuthBundle\Tests\Security;


use Auth0\JWTAuthBundle\JWTAuthBundle;
use Auth0\JWTAuthBundle\Security\Auth0Service;
use Nyholm\BundleTest\BaseBundleTestCase;


class BundleInitializationTest extends BaseBundleTestCase
{
protected function getBundleClass()
{
return JWTAuthBundle::class;
}

public function testInitBundle()
{
// Boot the kernel.
$this->bootKernel();

// Get the container
$container = $this->getContainer();

// Test if you services exists
$this->assertTrue($container->has('jwt_auth.auth0_service'));
$service = $container->get('jwt_auth.auth0_service');
$this->assertInstanceOf(Auth0Service::class, $service);
}

public function testBundleWithCache()
{
// Create a new Kernel
$kernel = $this->createKernel();

// Add some configuration
$kernel->addConfigFile(__DIR__.'/config/cache.yml');

// Boot the kernel as normal ...
$this->bootKernel();

$container = $this->getContainer();
$service = $container->get('jwt_auth.auth0_service');
$this->assertInstanceOf(Auth0Service::class, $service);
}
}
2 changes: 2 additions & 0 deletions Tests/Functional/config/cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jwt_auth:
cache: jwt_auth.cache.file_system
8 changes: 4 additions & 4 deletions Tests/Security/JWTAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function testTokenCreation()
//generated with http://jwt.io/
$JWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2RvbWFpbi5jb20vIiwic3ViIjoiYXV0aDB8MDAwMDAwMDAwMDAwMDAwMDAwMDAwMCIsImF1ZCI6ImNsaWVudF9pZCIsImV4cCI6MTQyMjQ0MDI3MSwiaWF0IjoxNDIyNDA0MjcxfQ.xSuCAetwfHpCWhE_5NqTrwHq0eQ7CVffQwgSqTHwwrY';

$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->headers = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->headers = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock();

$request->headers
->expects($this->atLeastOnce())
Expand All @@ -48,8 +48,8 @@ public function testNoAuthorization()
$authenticator = new JWTAuthenticator($mockAuth0);
$providerKey = 'providerKey';

$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->headers = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->headers = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock();

$request->headers
->expects($this->once())
Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@
],

"require": {
"php": ">=5.3.3",
"symfony/symfony": "~3.0",
"php": "^5.5 || ^7.0",
"symfony/framework-bundle": "^2.8 || ~3.0",
"auth0/auth0-php": "^5.0.3"
},

"require-dev": {
"phpunit/phpunit": "^5.1"
},

"suggest": {
"phpunit/phpunit": "^4.8.36 || ^5.7.21",
"nyholm/symfony-bundle-test": "^1.0.2"
},

"autoload": {
"psr-4": {
"Auth0\\JWTAuthBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Auth0\\JWTAuthBundle\\": "Tests/"
}
}
}
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function getConfigTreeBuilder()
->defaultValue('RS256')
->end()
->end()
->scalarNode('secret_base64_encoded')->defaultValue(false)->end();
->scalarNode('secret_base64_encoded')->defaultValue(false)->end()
->scalarNode('cache')->defaultNull()->info('The cache service you want to use. Example "jwt_auth.cache.file_system".')->end();

return $treeBuilder;
}
Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/JWTAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

Expand All @@ -26,5 +27,11 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('jwt_auth.authorized_issuer', $config['authorized_issuer']);
$container->setParameter('jwt_auth.secret_base64_encoded', $config['secret_base64_encoded']);
$container->setParameter('jwt_auth.supported_algs', $config['supported_algs']);

if (!empty($config['cache'])) {
$ref = new Reference($config['cache']);
$container->getDefinition('jwt_auth.auth0_service')
->replaceArgument(6, $ref);
}
}
}
11 changes: 8 additions & 3 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
services:
jwt_auth.auth0_service:
class: "Auth0\\JWTAuthBundle\\Security\\Auth0Service"
arguments: ["%jwt_auth.api_secret%","%jwt_auth.domain%","%jwt_auth.api_identifier%","%jwt_auth.authorized_issuer%","%jwt_auth.secret_base64_encoded%", "%jwt_auth.supported_algs%"]
class: Auth0\JWTAuthBundle\Security\Auth0Service
arguments: ["%jwt_auth.api_secret%","%jwt_auth.domain%","%jwt_auth.api_identifier%","%jwt_auth.authorized_issuer%","%jwt_auth.secret_base64_encoded%", "%jwt_auth.supported_algs%", ~]

jwt_auth.jwt_authenticator:
class: "Auth0\\JWTAuthBundle\\Security\\JWTAuthenticator"
class: Auth0\JWTAuthBundle\Security\JWTAuthenticator
arguments: ["@jwt_auth.auth0_service"]

jwt_auth.cache.null:
class: Auth0\SDK\Helpers\Cache\NoCacheHandler

jwt_auth.cache.file_system:
class: Auth0\SDK\Helpers\Cache\FileSystemCacheHandler
21 changes: 17 additions & 4 deletions src/Security/Auth0Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Auth0\JWTAuthBundle\Security;

use Auth0\SDK\Helpers\Cache\CacheHandler;
use Auth0\SDK\JWTVerifier;
use Auth0\SDK\Auth0Api;
use Auth0\SDK\API\Authentication;
Expand All @@ -22,18 +23,24 @@ class Auth0Service {
private $supported_algs;
private $authApi;

/**
* @var CacheHandler|null
*/
private $cache;

/**
* @param string $api_secret
* @param string $domain
*/
public function __construct($api_secret, $domain, $api_identifier, $authorized_issuer, $secret_base64_encoded, $supported_algs)
public function __construct($api_secret, $domain, $api_identifier, $authorized_issuer, $secret_base64_encoded, $supported_algs, CacheHandler $cache = null)
{
$this->api_secret = $api_secret;
$this->domain = $domain;
$this->api_identifier = $api_identifier;
$this->authorized_issuer = $authorized_issuer;
$this->secret_base64_encoded = $secret_base64_encoded;
$this->supported_algs = $supported_algs;
$this->cache = $cache;
$this->authApi = new Authentication($this->domain);
}

Expand All @@ -54,13 +61,19 @@ public function getUserProfileByA0UID($jwt, $a0UID)
*/
public function decodeJWT($encToken)
{
$verifier = new JWTVerifier([
'valid_audiences' => [ $this->api_identifier ],
$config = [
'valid_audiences' => [$this->api_identifier],
'client_secret' => $this->api_secret,
'authorized_iss' => [$this->authorized_issuer],
'supported_algs' => $this->supported_algs,
'secret_base64_encoded' => $this->secret_base64_encoded
]);
];

if (null !== $this->cache) {
$config['cache'] = $this->cache;
}

$verifier = new JWTVerifier($config);

return $verifier->verifyAndDecode($encToken);
}
Expand Down