Skip to content

Commit

Permalink
Use request matcher from symfony/http-foundation.
Browse files Browse the repository at this point in the history
  • Loading branch information
lakiboy committed Jul 5, 2018
1 parent 4a72af3 commit 750ca9c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 195 deletions.
18 changes: 8 additions & 10 deletions DependencyInjection/Configuration.php
Expand Up @@ -10,16 +10,16 @@

final class Configuration implements ConfigurationInterface
{
const SIGNER_SYMMETRIC = 'symmetric';
const SIGNER_ASYMMETRIC = 'asymmetric';

const STORAGE_FIXED = 'fixed';
const STORAGE_REDIS = 'redis';
const STORAGE_DOCTRINE = 'doctrine';
public const SIGNER_SYMMETRIC = 'symmetric';
public const SIGNER_ASYMMETRIC = 'asymmetric';

private const SYMMETRIC_ALGOS = ['HS256', 'HS384', 'HS512'];
private const ASYMMETRIC_ALGOS = ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512'];

public const STORAGE_FIXED = 'fixed';
public const STORAGE_REDIS = 'redis';
public const STORAGE_DOCTRINE = 'doctrine';

public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
Expand Down Expand Up @@ -259,13 +259,11 @@ private function exceptionsNode(string $name): ArrayNodeDefinition
->beforeNormalization()
->ifString()
->then(function (string $config): array {
return ['enabled' => true, 'base_url' => $config];
return ['enabled' => true, 'path' => $config];
})
->end()
->children()
->scalarNode('base_url')
->defaultValue('/')
->end()
->scalarNode('path')->cannotBeEmpty()->end()
->end()
;
}
Expand Down
14 changes: 6 additions & 8 deletions DependencyInjection/DamaxApiAuthExtension.php
Expand Up @@ -19,7 +19,6 @@
use Damax\Bundle\ApiAuthBundle\Key\Storage\Reader;
use Damax\Bundle\ApiAuthBundle\Key\Storage\Writer;
use Damax\Bundle\ApiAuthBundle\Listener\ExceptionListener;
use Damax\Bundle\ApiAuthBundle\Request\RequestMatcher;
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\Authenticator as ApiKeyAuthenticator;
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\StorageUserProvider;
use Damax\Bundle\ApiAuthBundle\Security\Jwt\AuthenticationHandler;
Expand All @@ -33,6 +32,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;

final class DamaxApiAuthExtension extends ConfigurableExtension
Expand Down Expand Up @@ -127,21 +127,19 @@ private function configureJwt(array $config, ContainerBuilder $container): self
;

// Handler.
$container
->register('damax.api_auth.jwt.handler', AuthenticationHandler::class)
->setAutowired(true)
;
$container->autowire('damax.api_auth.jwt.handler', AuthenticationHandler::class);

return $this;
}

private function configureExceptions(array $config, ContainerBuilder $container): self
{
$matcher = (new Definition(RequestMatcher::class))->addArgument($config['path'] ?? null);

$container
->register(ExceptionListener::class)
->setAutowired(true)
->autowire(ExceptionListener::class)
->setArgument(1, $matcher)
->addTag('kernel.event_listener', ['event' => 'kernel.exception', 'method' => 'onKernelException'])
->setArgument(1, new Definition(RequestMatcher::class, [$config['base_url']]))
;

return $this;
Expand Down
23 changes: 0 additions & 23 deletions Request/RequestMatcher.php

This file was deleted.

139 changes: 53 additions & 86 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -40,7 +40,6 @@ public function it_processes_empty_config()
],
'format_exceptions' => [
'enabled' => false,
'base_url' => '/',
],
]);
}
Expand Down Expand Up @@ -80,7 +79,7 @@ public function it_processes_simplified_api_key_config()
/**
* @test
*/
public function it_processes_api_key_config()
public function it_configures_api_key()
{
$config = [
'api_key' => [
Expand Down Expand Up @@ -151,60 +150,6 @@ public function it_processes_api_key_config()
], 'api_key');
}

/**
* @test
*/
public function it_processes_basic_jwt_config()
{
$config = [
'jwt' => [
'builder' => [
'issuer' => 'damax-api-auth-bundle',
'audience' => 'symfony',
'ttl' => 600,
],
'parser' => [
'issuers' => ['symfony', 'zend'],
'audience' => 'zend',
],
'extractors' => [
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'],
['type' => 'query', 'name' => 'token'],
['type' => 'cookie', 'name' => 'token'],
],
'signer' => [
'signing_key' => 'secret',
],
],
];

$this->assertProcessedConfigurationEquals([$config], [
'jwt' => [
'enabled' => true,
'builder' => [
'issuer' => 'damax-api-auth-bundle',
'audience' => 'symfony',
'ttl' => 600,
],
'parser' => [
'issuers' => ['symfony', 'zend'],
'audience' => 'zend',
],
'extractors' => [
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'],
['type' => 'query', 'name' => 'token'],
['type' => 'cookie', 'name' => 'token'],
],
'signer' => [
'type' => 'symmetric',
'algorithm' => 'HS256',
'signing_key' => 'secret',
'passphrase' => '',
],
],
], 'jwt');
}

/**
* @test
*/
Expand Down Expand Up @@ -315,18 +260,10 @@ public function it_requires_no_passphrase_for_symmetric_signer()
/**
* @test
*/
public function it_processes_jwt_config()
public function it_processes_simplified_jwt_config()
{
$filename = tempnam(sys_get_temp_dir(), 'key_');

$config = [
'jwt' => [
'signer' => [
'type' => 'asymmetric',
'signing_key' => $filename,
'verification_key' => $filename,
],
],
'jwt' => 'secret',
];

$this->assertProcessedConfigurationEquals([$config], [
Expand All @@ -339,78 +276,108 @@ public function it_processes_jwt_config()
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'],
],
'signer' => [
'type' => 'asymmetric',
'algorithm' => 'RS256',
'signing_key' => 'file://' . $filename,
'verification_key' => 'file://' . $filename,
'type' => 'symmetric',
'algorithm' => 'HS256',
'signing_key' => 'secret',
'passphrase' => '',
],
],
], 'jwt');

unlink($filename);
}

/**
* @test
*/
public function it_processes_minimal_jwt_config()
public function it_configures_jwt()
{
$filename = tempnam(sys_get_temp_dir(), 'key_');

$config = [
'jwt' => 'secret',
'jwt' => [
'builder' => [
'issuer' => 'damax-api-auth-bundle',
'audience' => 'symfony',
'ttl' => 600,
],
'parser' => [
'issuers' => ['symfony', 'zend'],
'audience' => 'zend',
],
'extractors' => [
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'],
['type' => 'query', 'name' => 'token'],
['type' => 'cookie', 'name' => 'token'],
],
'signer' => [
'type' => 'asymmetric',
'signing_key' => $filename,
'verification_key' => $filename,
],
],
];

$this->assertProcessedConfigurationEquals([$config], [
'jwt' => [
'enabled' => true,
'builder' => [
'ttl' => 3600,
'issuer' => 'damax-api-auth-bundle',
'audience' => 'symfony',
'ttl' => 600,
],
'parser' => [
'issuers' => ['symfony', 'zend'],
'audience' => 'zend',
],
'extractors' => [
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'],
['type' => 'query', 'name' => 'token'],
['type' => 'cookie', 'name' => 'token'],
],
'signer' => [
'type' => 'symmetric',
'algorithm' => 'HS256',
'signing_key' => 'secret',
'type' => 'asymmetric',
'algorithm' => 'RS256',
'signing_key' => 'file://' . $filename,
'verification_key' => 'file://' . $filename,
'passphrase' => '',
],
],
], 'jwt');

unlink($filename);
}

/**
* @test
*/
public function it_processes_exceptions_config()
public function it_processes_simplified_exceptions_config()
{
$config = [
'format_exceptions' => [
'base_url' => '/api',
],
'format_exceptions' => '/api',
];

$this->assertProcessedConfigurationEquals([$config], [
'format_exceptions' => [
'enabled' => true,
'base_url' => '/api',
'path' => '/api',
],
], 'format_exceptions');
}

/**
* @test
*/
public function it_processes_minimal_exceptions_config()
public function it_configures_exceptions_formatting()
{
$config = [
'format_exceptions' => '/api',
'format_exceptions' => [
'path' => '/api',
],
];

$this->assertProcessedConfigurationEquals([$config], [
'format_exceptions' => [
'enabled' => true,
'base_url' => '/api',
'path' => '/api',
],
], 'format_exceptions');
}
Expand Down

0 comments on commit 750ca9c

Please sign in to comment.