Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ parameters:
- tests/Bridge/Symfony/Bundle/Test/WebTestCaseTest.php
- tests/ProphecyTrait.php
- tests/Behat/CoverageContext.php
- tests/Fixtures/TestBundle/Security/AbstractSecurityUser.php
earlyTerminatingMethodCalls:
PHPUnit\Framework\Constraint\Constraint:
- fail
Expand Down Expand Up @@ -89,6 +90,8 @@ parameters:

# Expected, Symfony 5.3
- '#getCollectionValueT|getCollectionKeyTyp#'
- '#isPasswordValid#'
- '#UserPasswordHasherInterface#'

# Expected, due to PHP 8 attributes
- '#ReflectionProperty::getAttributes\(\)#'
Expand Down
11 changes: 7 additions & 4 deletions tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Ramsey\Uuid\Uuid;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Uid\Uuid as SymfonyUuid;

Expand All @@ -182,7 +183,7 @@ final class DoctrineContext implements Context
*/
private $manager;
private $doctrine;
private $passwordEncoder;
private $passwordHasher;
private $schemaTool;
private $schemaManager;

Expand All @@ -192,11 +193,13 @@ final class DoctrineContext implements Context
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*
* @param UserPasswordEncoderInterface|UserPasswordHasherInterface $passwordHasher
*/
public function __construct(ManagerRegistry $doctrine, UserPasswordEncoderInterface $passwordEncoder)
public function __construct(ManagerRegistry $doctrine, $passwordHasher)
{
$this->doctrine = $doctrine;
$this->passwordEncoder = $passwordEncoder;
$this->passwordHasher = $passwordHasher;
$this->manager = $doctrine->getManager();
$this->schemaTool = $this->manager instanceof EntityManagerInterface ? new SchemaTool($this->manager) : null;
$this->schemaManager = $this->manager instanceof DocumentManager ? $this->manager->getSchemaManager() : null;
Expand Down Expand Up @@ -1198,7 +1201,7 @@ public function thereIsAUrlEncodedIdResource()
public function thePasswordForUserShouldBeHashed(string $password, string $user)
{
$user = $this->doctrine->getRepository($this->isOrm() ? User::class : UserDocument::class)->find($user);
if (!$this->passwordEncoder->isPasswordValid($user, $password)) {
if (!$this->passwordHasher->isPasswordValid($user, $password)) {
throw new \Exception('User password mismatch');
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Document/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\PasswordResetRequestResult;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\RecoverPasswordInput;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\RecoverPasswordOutput;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Security\AbstractSecurityUser;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;

/**
Expand Down Expand Up @@ -58,7 +58,7 @@
* @author Théo FIDRY <theo.fidry@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class User implements UserInterface
class User extends AbstractSecurityUser
{
/**
* @var int
Expand Down Expand Up @@ -156,7 +156,7 @@ public function getRoles(): array
return ['ROLE_USER'];
}

public function getPassword()
public function getPassword(): ?string
{
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\PasswordResetRequestResult;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\RecoverPasswordInput;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\RecoverPasswordOutput;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Security\AbstractSecurityUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;

/**
Expand Down Expand Up @@ -59,7 +59,7 @@
* @author Théo FIDRY <theo.fidry@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class User implements UserInterface
class User extends AbstractSecurityUser
{
/**
* @var int
Expand Down Expand Up @@ -159,7 +159,7 @@ public function getRoles(): array
return ['ROLE_USER'];
}

public function getPassword()
public function getPassword(): ?string
{
return null;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Fixtures/TestBundle/Security/AbstractSecurityUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Security;

use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

// Forward compatibility layer for symfony/password-hasher:^5.3
if (interface_exists(PasswordAuthenticatedUserInterface::class)) {
abstract class AbstractSecurityUser implements UserInterface, PasswordAuthenticatedUserInterface
{
abstract public function getPassword(): ?string;

public function getUserIdentifier(): string
{
return $this->getUsername();
}
}
Comment thread
chalasr marked this conversation as resolved.
} else {
abstract class AbstractSecurityUser implements UserInterface
{
}
}
7 changes: 5 additions & 2 deletions tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
declare(strict_types=1);

use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
use ApiPlatform\Core\Tests\Behat\DoctrineContext;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\TestBundle;
Expand Down Expand Up @@ -117,6 +118,8 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
$loader->load(__DIR__.'/config/config_symfony_uid.yml');
}

$c->getDefinition(DoctrineContext::class)->setArgument('$passwordHasher', class_exists(NativePasswordHasher::class) ? 'security.user_password_encoder' : 'security.user_password_hasher');

$c->prependExtensionConfig('framework', [
'secret' => 'dunglas.fr',
'validation' => ['enable_annotations' => true],
Expand All @@ -136,9 +139,9 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'router' => ['utf8' => true],
]);

$alg = class_exists(NativePasswordHasher::class) || class_exists('Symfony\Component\Security\Core\Encoder\NativePasswordEncoder') ? 'auto' : 'bcrypt';
$alg = class_exists(NativePasswordHasher::class, false) || class_exists('Symfony\Component\Security\Core\Encoder\NativePasswordEncoder') ? 'auto' : 'bcrypt';
$securityConfig = [
'encoders' => [
class_exists(NativePasswordHasher::class) ? 'password_hashers' : 'encoders' => [
Comment thread
chalasr marked this conversation as resolved.
User::class => $alg,
UserDocument::class => $alg,
// Don't use plaintext in production!
Expand Down