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

Use a UserChecker #56

Merged
merged 3 commits into from Dec 6, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions DependencyInjection/Compiler/UserCheckerCompilerPass.php
@@ -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
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
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
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
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
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
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
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