Skip to content

Commit

Permalink
Added support for cache (#51)
Browse files Browse the repository at this point in the history
* Added support for cache

* Bugfixes

* Fixed broken build

* Minor

* Include bugfix versions
  • Loading branch information
Nyholm authored and cocojoe committed Jul 17, 2017
1 parent 5906699 commit 0752ec3
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 18 deletions.
28 changes: 28 additions & 0 deletions .travis.yml
@@ -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
@@ -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
@@ -0,0 +1,2 @@
jwt_auth:
cache: jwt_auth.cache.file_system
8 changes: 4 additions & 4 deletions Tests/Security/JWTAuthenticatorTest.php
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
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
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
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
@@ -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
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

0 comments on commit 0752ec3

Please sign in to comment.