Skip to content

Commit

Permalink
Throw access denied when 2fa check path is called outside of a 2fa pr…
Browse files Browse the repository at this point in the history
…ocess, fix #52
  • Loading branch information
scheb committed Jan 29, 2021
1 parent f064193 commit b4a2511
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
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\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
Expand Down Expand Up @@ -100,8 +100,9 @@ public function authenticate(Request $request): PassportInterface
// within the "authenticate" stage.
$currentToken = $this->tokenStorage->getToken();
if (!($currentToken instanceof TwoFactorTokenInterface)) {
// This should only happen when the check path is called outside of a 2fa process and not protected via access_control
throw new AuthenticationServiceException('Tried to perform two-factor authentication, but two-factor authentication is not in progress.');
// This should only happen when the check path is called outside of a 2fa process
// access_control can't handle this, as it's called after the authenticator
throw new AccessDeniedException('User is not in a two-factor authentication process.');
}

$this->dispatchTwoFactorAuthenticationEvent(TwoFactorAuthenticationEvents::ATTEMPT, $request, $currentToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
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\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
Expand Down Expand Up @@ -235,10 +235,10 @@ public function supports_isCheckPath_returnTrue(): void
/**
* @test
*/
public function authenticate_notTwoFactorToken_throwAuthenticationServiceException(): void
public function authenticate_notTwoFactorToken_throwAccessDeniedException(): void
{
$this->stubTokenStorageHasToken($this->createMock(TokenInterface::class));
$this->expectException(AuthenticationServiceException::class);
$this->expectException(AccessDeniedException::class);
$this->authenticator->authenticate($this->request);
}

Expand Down

0 comments on commit b4a2511

Please sign in to comment.