-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two-Factor: extract code validation into constraint and add test to e…
…nable two-factor
- Loading branch information
Showing
8 changed files
with
209 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ PANTHER_APP_ENV=panther | |
DATABASE_URL="mysql://[email protected]:3306/packagist?serverVersion=8.0.28" | ||
MAILER_DSN=null://null | ||
REDIS_URL=redis://localhost/14 | ||
APP_MAILER_FROM_EMAIL=[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Form\Validation; | ||
|
||
use App\Entity\User; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
class TwoFactorCodeConstraint extends Constraint | ||
{ | ||
public function __construct( | ||
public readonly User $user, | ||
public readonly string $message = 'Invalid authenticator code', | ||
) { | ||
parent::__construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Form\Validation; | ||
|
||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
class TwoFactorCodeConstraintValidator extends ConstraintValidator | ||
{ | ||
public function __construct( | ||
private readonly TotpAuthenticatorInterface $totpAuthenticator, | ||
) {} | ||
|
||
/** | ||
* @param TwoFactorCodeConstraint $constraint | ||
*/ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$this->totpAuthenticator->checkCode($constraint->user, (string) $value)) { | ||
$this->context->addViolation($constraint->message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Controller; | ||
|
||
use App\Entity\User; | ||
use App\Tests\Mock\TotpAuthenticatorStub; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
class UserControllerTest extends WebTestCase | ||
{ | ||
private KernelBrowser $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = self::createClient(); | ||
$this->client->disableReboot(); // Prevent reboot between requests | ||
static::getContainer()->get(Connection::class)->beginTransaction(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
static::getContainer()->get(Connection::class)->rollBack(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testEnableTwoFactoCode(): void | ||
{ | ||
$user = new User; | ||
$user->setEnabled(true); | ||
$user->setUsername('test'); | ||
$user->setEmail('[email protected]'); | ||
$user->setPassword('testtest'); | ||
$user->setApiToken('token'); | ||
|
||
$em = static::getContainer()->get(ManagerRegistry::class)->getManager(); | ||
$em->persist($user); | ||
$em->flush(); | ||
|
||
$this->client->loginUser($user); | ||
|
||
$crawler = $this->client->request('GET', sprintf('/users/%s/2fa/enable', $user->getUsername())); | ||
$form = $crawler->selectButton('Enable Two-Factor Authentication')->form(); | ||
$form->setValues([ | ||
'enable_two_factor_auth[code]' => 123456, | ||
]); | ||
|
||
$crawler = $this->client->submit($form); | ||
$this->assertResponseStatusCodeSame(422); | ||
|
||
$form = $crawler->selectButton('Enable Two-Factor Authentication')->form(); | ||
$form->setValues([ | ||
'enable_two_factor_auth[code]' => TotpAuthenticatorStub::MOCKED_VALID_CODE, | ||
]); | ||
|
||
$this->client->submit($form); | ||
$this->assertResponseStatusCodeSame(302); | ||
|
||
$em->clear(); | ||
$this->assertTrue($em->getRepository(User::class)->find($user->getId())->isTotpAuthenticationEnabled()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Mock; | ||
|
||
use Scheb\TwoFactorBundle\Model\Totp\TwoFactorInterface; | ||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface; | ||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpFactory; | ||
use ParagonIE\ConstantTime\Base32; | ||
|
||
class TotpAuthenticatorStub implements TotpAuthenticatorInterface | ||
{ | ||
public const MOCKED_VALID_CODE = '999999'; | ||
|
||
public function __construct( | ||
private readonly TotpFactory $totpFactory, | ||
) {} | ||
|
||
public function checkCode(TwoFactorInterface $user, string $code): bool | ||
{ | ||
return $code === self::MOCKED_VALID_CODE; | ||
} | ||
|
||
public function getQRContent(TwoFactorInterface $user): string | ||
{ | ||
return $this->totpFactory->createTotpForUser($user)->getProvisioningUri(); | ||
} | ||
|
||
public function generateSecret(): string | ||
{ | ||
return Base32::encodeUpperUnpadded(random_bytes(32)); | ||
} | ||
} |