Skip to content

Commit

Permalink
Use a UserChecker (#56)
Browse files Browse the repository at this point in the history
* Add user_checker, fixes #52

* Add documentation

* Fix styleci warning
  • Loading branch information
sandermarechal authored and markitosgv committed Dec 6, 2018
1 parent 428ef3d commit 604b70c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 0 deletions.
25 changes: 25 additions & 0 deletions DependencyInjection/Compiler/UserCheckerCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* UserCheckerCompilerPass.
*/
final class UserCheckerCompilerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$userCheckerId = $container->getParameter('gesdinet.jwtrefreshtoken.user_checker.id');
if (!$userCheckerId) {
return;
}

$container->setAlias('gesdinet.jwtrefreshtoken.user_checker', $userCheckerId);
}
}
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getConfigTreeBuilder()
->defaultNull()
->info('Set object manager to use (default: doctrine.orm.entity_manager)')
->end()
->scalarNode('user_checker')->defaultValue('security.user_checker')->end()
->scalarNode('refresh_token_entity')
->defaultNull()
->info('Deprecated, use refresh_token_class instead')
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/GesdinetJWTRefreshTokenExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('gesdinet.jwtrefreshtoken.refresh_token.class', $refreshTokenClass);
$container->setParameter('gesdinet.jwtrefreshtoken.object_manager.id', $objectManager);
$container->setParameter('gesdinet.jwtrefreshtoken.user_checker.id', $config['user_checker']);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions GesdinetJWTRefreshTokenBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler\CustomUserProviderCompilerPass;
use Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler\DoctrineMappingsCompilerPass;
use Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler\ObjectManagerCompilerPass;
use Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler\UserCheckerCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -17,5 +18,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new CustomUserProviderCompilerPass());
$container->addCompilerPass(new ObjectManagerCompilerPass());
$container->addCompilerPass(new DoctrineMappingsCompilerPass());
$container->addCompilerPass(new UserCheckerCompilerPass());
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ gesdinet_jwt_refresh_token:
manager_type: mongodb
```

### Config UserChecker

You can define your own UserChecker. By default the Symfony UserChecker will be used. You can change this value by adding this line to your config.yml file:

```yaml
gesdinet_jwt_refresh_token:
user_checker: user_checker_service_id
```

You will probably want to use a custom UserProvider along with your UserChecker to ensure that the checker recieves the right type of user.


### Use another entity for refresh tokens

You can define your own refresh token class on your project.
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:

gesdinet.jwtrefreshtoken.authenticator:
class: Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator
arguments: [ "@gesdinet.jwtrefreshtoken.user_checker" ]

Gesdinet\JWTRefreshTokenBundle\Command\:
resource: '../../Command/*'
Expand Down
19 changes: 19 additions & 0 deletions Security/Authenticator/RefreshTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -36,6 +37,21 @@ abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Secur
*/
class RefreshTokenAuthenticator extends RefreshTokenAuthenticatorBase implements AuthenticationFailureHandlerInterface
{
/**
* @var UserCheckerInterface
*/
private $userChecker;

/**
* Constructor.
*
* @param UserCheckerInterface $userChecker
*/
public function __construct(UserCheckerInterface $userChecker)
{
$this->userChecker = $userChecker;
}

public function createToken(Request $request, $providerKey)
{
$refreshTokenString = RequestRefreshToken::getRefreshToken($request);
Expand Down Expand Up @@ -69,6 +85,9 @@ public function authenticateToken(TokenInterface $token, UserProviderInterface $

$user = $userProvider->loadUserByUsername($username);

$this->userChecker->checkPreAuth($user);
$this->userChecker->checkPostAuth($user);

return new PreAuthenticatedToken(
$user,
$refreshToken,
Expand Down
6 changes: 6 additions & 0 deletions spec/Security/Authenticator/RefreshTokenAuthenticatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;

class RefreshTokenAuthenticatorSpec extends ObjectBehavior
{
public function let(UserCheckerInterface $userChecker)
{
$this->beConstructedWith($userChecker);
}

public function it_is_initializable()
{
$this->shouldHaveType('Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator');
Expand Down

0 comments on commit 604b70c

Please sign in to comment.