Skip to content

Commit

Permalink
Suppress getProviderKey deprecation notices generated from within the…
Browse files Browse the repository at this point in the history
… bundle
  • Loading branch information
scheb committed May 12, 2021
1 parent 338f127 commit b44c172
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 12 deletions.
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@
<referencedClass name="Doctrine\Common\Persistence\ManagerRegistry"/>
</errorLevel>
</UndefinedDocblockClass>
<!-- Suppress error because of deprecation flag -->
<TooManyArguments>
<errorLevel type="suppress">
<referencedFunction name="Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface::getproviderkey"/>
</errorLevel>
</TooManyArguments>
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/bundle/Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function setPreferredProvider(Request $request, TwoFactorTokenInterfac

protected function getTemplateVars(Request $request, TwoFactorTokenInterface $token): array
{
$config = $this->twoFactorFirewallContext->getFirewallConfig($token->getProviderKey());
$config = $this->twoFactorFirewallContext->getFirewallConfig($token->getProviderKey(true));
$pendingTwoFactorProviders = $token->getTwoFactorProviders();
$displayTrustedOption = $this->canSetTrustedDevice($token, $request, $config);
$authenticationException = $this->getLastAuthenticationException($request->getSession());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(
public function supports(TokenInterface $token): bool
{
return $token instanceof TwoFactorTokenInterface
&& $this->twoFactorFirewallConfig->getFirewallName() === $token->getProviderKey();
&& $this->twoFactorFirewallConfig->getFirewallName() === $token->getProviderKey(true);
}

public function authenticate(TokenInterface $token): TokenInterface
Expand All @@ -75,7 +75,7 @@ public function authenticate(TokenInterface $token): TokenInterface
throw new AuthenticationException('There is no active two-factor provider.');
}

if (!$this->preparationRecorder->isTwoFactorProviderPrepared($token->getProviderKey(), $providerName)) {
if (!$this->preparationRecorder->isTwoFactorProviderPrepared($token->getProviderKey(true), $providerName)) {
throw new AuthenticationException(sprintf('The two-factor provider "%s" has not been prepared.', $providerName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

/**
* @method string getFirewallName() To be used instead of getProviderKey(true), which is deprecated since 5.8
*/
interface TwoFactorTokenInterface extends TokenInterface
{
public const ATTRIBUTE_NAME_REMEMBER_ME_COOKIE = 'remember_me_cookie';
Expand All @@ -21,6 +24,8 @@ public function getAuthenticatedToken(): TokenInterface;
public function createWithCredentials(string $credentials): self;

/**
* @deprecated since 5.8, use getFirewallName() instead
*
* Return the provider key (firewall name).
*/
public function getProviderKey(): string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getTwoFactorToken(): TwoFactorTokenInterface

public function getFirewallName(): string
{
return $this->twoFactorToken->getProviderKey();
return $this->twoFactorToken->getProviderKey(true);
}

public function getUser(): UserInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function checkPassport(CheckPassportEvent $event): void
throw new AuthenticationException('There is no active two-factor provider.');
}

if (!$this->preparationRecorder->isTwoFactorProviderPrepared($token->getProviderKey(), $providerName)) {
if (!$this->preparationRecorder->isTwoFactorProviderPrepared($token->getProviderKey(true), $providerName)) {
throw new AuthenticationException(sprintf('The two-factor provider "%s" has not been prepared.', $providerName));
}

Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Security/Http/Firewall/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function onKernelException(ExceptionEvent $event): void
private function handleAccessDeniedException(ExceptionEvent $exceptionEvent): void
{
$token = $this->tokenStorage->getToken();
if (!($token instanceof TwoFactorTokenInterface && $token->getProviderKey() === $this->firewallName)) {
if (!($token instanceof TwoFactorTokenInterface && $token->getProviderKey(true) === $this->firewallName)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/bundle/Security/Http/Firewall/TwoFactorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function authenticate(RequestEvent $event): void
// When the firewall is lazy, the token is not initialized in the "supports" stage, so this check does only work
// within the "authenticate" stage.
$token = $this->tokenStorage->getToken();
if (!($token instanceof TwoFactorTokenInterface) || $token->getProviderKey() !== $this->twoFactorFirewallConfig->getFirewallName()) {
if (!($token instanceof TwoFactorTokenInterface) || $token->getProviderKey(true) !== $this->twoFactorFirewallConfig->getFirewallName()) {
// This should only happen when the check path is called outside of a 2fa process and not protected via access_control
// or when the firewall is configured in an odd way (different firewall name)
throw new AuthenticationServiceException('Tried to perform two-factor authentication, but two-factor authentication is not in progress.');
Expand Down Expand Up @@ -185,7 +185,7 @@ private function onSuccess(Request $request, TokenInterface $token, TwoFactorTok

$this->dispatchTwoFactorAuthenticationEvent(TwoFactorAuthenticationEvents::COMPLETE, $request, $token);

$firewallName = $previousTwoFactorToken->getProviderKey();
$firewallName = $previousTwoFactorToken->getProviderKey(true);
if ($this->trustedDeviceManager
&& $this->twoFactorFirewallConfig->hasTrustedDeviceParameterInRequest($request)
&& $this->trustedDeviceManager->canSetTrustedDevice($token->getUser(), $request, $firewallName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function isTwoFactorProviderPrepared(string $firewallName, string $provid
throw new \RuntimeException('The security token has to be an instance of TwoFactorTokenInterface.');
}

$providerKey = $token->getProviderKey();
$providerKey = $token->getProviderKey(true);
if ($providerKey !== $firewallName) {
throw new \LogicException(sprintf('Cannot store preparation state for firewall "%s" in a TwoFactorToken belonging to "%s".', $firewallName, $providerKey));
}
Expand All @@ -46,7 +46,7 @@ public function setTwoFactorProviderPrepared(string $firewallName, string $provi
throw new \RuntimeException('The security token has to be an instance of TwoFactorTokenInterface.');
}

$providerKey = $token->getProviderKey();
$providerKey = $token->getProviderKey(true);
if ($providerKey !== $firewallName) {
throw new \LogicException(sprintf('Cannot store preparation state for firewall "%s" in a TwoFactorToken belonging to "%s".', $firewallName, $providerKey));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function onKernelResponse(ResponseEvent $event): void
return;
}

$firewallName = $twoFactorToken->getProviderKey();
$firewallName = $twoFactorToken->getProviderKey(true);

if ($this->preparationRecorder->isTwoFactorProviderPrepared($firewallName, $providerName)) {
$this->logger->info(sprintf('Two-factor provider "%s" was already prepared.', $providerName));
Expand All @@ -140,7 +140,7 @@ public function onKernelResponse(ResponseEvent $event): void

private function supports(TokenInterface $token): bool
{
return $token instanceof TwoFactorTokenInterface && $token->getProviderKey() === $this->firewallName;
return $token instanceof TwoFactorTokenInterface && $token->getProviderKey(true) === $this->firewallName;
}

public static function getSubscribedEvents()
Expand Down

0 comments on commit b44c172

Please sign in to comment.