Skip to content

Commit

Permalink
Clear token on user mistmatch
Browse files Browse the repository at this point in the history
  • Loading branch information
veteran29 committed Nov 6, 2023
1 parent d608327 commit 1f92525
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

cs:
docker run --rm -it -w=/app -v ${CURDIR}:/app oskarstark/php-cs-fixer-ga:latest
docker run --rm -w=/app -v $(CURDIR):/app oskarstark/php-cs-fixer-ga:latest
2 changes: 1 addition & 1 deletion src/DependencyInjection/OryKratosAuthenticatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
->setDefinition($authenticatorId, new ChildDefinition($config['authenticator']))
->replaceArgument(0, new Reference($clientId))
->replaceArgument(1, new Reference($userProviderId))
->replaceArgument(3, $config['session_check'])
->replaceArgument(4, $config['session_check'])
;

return $authenticatorId;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/authenticator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<argument /> <!-- Ory Kratos Client -->
<argument /> <!-- User Provider -->
<argument type="service" id="security.helper" on-invalid="exception" />
<argument type="service" id="security.token_storage" on-invalid="exception" />
<argument /> <!-- Check session -->
<argument type="service" id="debug.stopwatch" on-invalid="null" />
</service>
Expand Down
9 changes: 7 additions & 2 deletions src/Security/Authenticator/OryKratosAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use StethoMe\OryAuthBundle\Services\OryKratosClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\ChainUserProvider;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
Expand All @@ -30,6 +32,7 @@ public function __construct(
private readonly OryKratosClient $kratos,
private readonly UserProviderInterface $userProvider,
private readonly Security $security,
private readonly TokenStorageInterface $tokenStorage,
private readonly bool $checkSession = true,
private readonly ?Stopwatch $stopwatch = null,
) {
Expand Down Expand Up @@ -66,8 +69,10 @@ public function authenticate(Request $request): Passport
// keep current user if we're only checking if the Ory Kratos session has not expired
if ($this->checkSession && $user = $this->security->getUser()) {
// handle user switching accounts outside our application
if ($this->loadUser($session->getIdentity()->getId(), $session)->getUserIdentifier() !== $user->getUserIdentifier()) {
throw new AuthenticationException('Session user not equal application user!');
$newUser = $this->loadUser($session->getIdentity()->getId(), $session);
if ($newUser instanceof EquatableInterface && !$newUser->isEqualTo($user)) {
$this->tokenStorage->setToken(null);
throw new UserNotFoundException('Session user not equal application user!');
}

return new SelfValidatingPassport(
Expand Down

0 comments on commit 1f92525

Please sign in to comment.