Skip to content

Commit

Permalink
Merge pull request #10 from gfreeau/feature-configure-access
Browse files Browse the repository at this point in the history
Added ability to throw exceptions for handling later and to disable the catch-all entry point
  • Loading branch information
Nicolas Cabot committed May 16, 2014
2 parents 99a8d44 + 5bd5f75 commit 27fdd41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
29 changes: 22 additions & 7 deletions DependencyInjection/Security/Factory/JWTFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,

$listenerId = 'security.authentication.listener.jwt.' . $id;
$container
->setDefinition($listenerId, new DefinitionDecorator('lexik_jwt_authentication.security.authentication.listener'));
->setDefinition($listenerId, new DefinitionDecorator('lexik_jwt_authentication.security.authentication.listener'))
->replaceArgument(2, $config)
;

// entry point
$entryPointId = $this->createEntryPoint($container, $id, $defaultEntryPoint);
$entryPointId = $defaultEntryPoint;

if ($config['create_entry_point']) {
$entryPointId = $this->createEntryPoint($container, $id, $defaultEntryPoint);
}

if ($config['authorization_header']['enabled']) {

Expand Down Expand Up @@ -106,15 +111,25 @@ public function addConfiguration(NodeDefinition $node)
->end()
->end()
->end()
->booleanNode('throw_exceptions')
->defaultFalse()
->end()
->booleanNode('create_entry_point')
->defaultTrue()
->end()
->end();
}

/**
* Create an entry point, by default it sends a 401 header and ends the request
*
* @param $container
* @param $id
* @param $defaultEntryPoint
* @return string
*/
protected function createEntryPoint($container, $id, $defaultEntryPoint)
{
if (null !== $defaultEntryPoint) {
return $defaultEntryPoint;
}

$entryPointId = 'lexik_jwt_authentication.security.authentication.entry_point.'.$id;
$container->setDefinition($entryPointId, new DefinitionDecorator('lexik_jwt_authentication.security.authentication.entry_point'));

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ firewalls:
query_parameter: # check token in query string parameter
enabled: true
name: bearer
throw_exceptions: false # When an authentication failure occurs, return a 401 response immediately
create_entry_point: true # When no authentication details are provided, create a default entry point that returns a 401 response
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<service id="lexik_jwt_authentication.security.authentication.listener" class="%lexik_jwt_authentication.security.authentication.listener.class%" public="false">
<argument type="service" id="security.context"/>
<argument type="service" id="security.authentication.manager" />
<argument /> <!-- Options -->
</service>
<!-- Authorization Header Token Extractor -->
<service id="lexik_jwt_authentication.extractor.authorization_header_extractor" class="%lexik_jwt_authentication.extractor.authorization_header_extractor.class%" public="false">
Expand Down
13 changes: 11 additions & 2 deletions Security/Firewall/JWTListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class JWTListener implements ListenerInterface
*/
protected $authenticationManager;

protected $config;

/**
* @var array
*/
Expand All @@ -37,11 +39,16 @@ class JWTListener implements ListenerInterface
/**
* @param SecurityContextInterface $securityContext
* @param AuthenticationManagerInterface $authenticationManager
* @param array $config
*/
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager)
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, array $config = array())
{
$this->securityContext = $securityContext;
$this->authenticationManager = $authenticationManager;
$this->config = array_merge(
array('throw_exceptions' => false),
$config
);
$this->tokenExtractors = array();
}

Expand All @@ -65,11 +72,13 @@ public function handle(GetResponseEvent $event)
return;

} catch (AuthenticationException $failed) {
if ($this->config['throw_exceptions']) {
throw $failed;
}

$response = new Response();
$response->setStatusCode(401);
$event->setResponse($response);

}
}

Expand Down

0 comments on commit 27fdd41

Please sign in to comment.