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
4 changes: 0 additions & 4 deletions src/User/Credential/PasswordProtectedCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MsgPhp\User\Credential;

use MsgPhp\User\Password\PasswordAlgorithm;

/**
* Represents a user credential that is password protected. The password is usually a *hashed* value (thus secret).
*
Expand All @@ -16,6 +14,4 @@ interface PasswordProtectedCredential extends Credential
public static function getPasswordField(): string;

public function getPassword(): string;

public function getPasswordAlgorithm(): ?PasswordAlgorithm;
}
7 changes: 0 additions & 7 deletions src/User/Credential/PasswordProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace MsgPhp\User\Credential;

use MsgPhp\User\Password\PasswordAlgorithm;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
Expand All @@ -23,9 +21,4 @@ public function getPassword(): string
{
return $this->password;
}

public function getPasswordAlgorithm(): ?PasswordAlgorithm
{
return null;
}
}
50 changes: 13 additions & 37 deletions src/User/Infrastructure/Form/Type/HashedPasswordType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace MsgPhp\User\Infrastructure\Form\Type;

use MsgPhp\User\Password\GenericPasswordHashing;
use MsgPhp\User\Password\PasswordAlgorithm;
use MsgPhp\User\Password\PasswordHashing;
use MsgPhp\User\Infrastructure\Security\UserIdentity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
Expand All @@ -23,16 +23,16 @@
*/
final class HashedPasswordType extends AbstractType
{
private $hashing;
private $hashingFactory;

public function __construct(PasswordHashing $hashing = null)
public function __construct(EncoderFactoryInterface $hashingFactory)
{
$this->hashing = $hashing;
$this->hashingFactory = $hashingFactory;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$password = new Password($options['password_hashing'] ?? $this->hashing ?? new GenericPasswordHashing(), $this->createAlgorithm($options['password_algorithm']));
$password = new Password($this->hashingFactory->getEncoder($options['hashing']));
$fieldOptions = [
'required' => $options['required'],
'translation_domain' => $options['translation_domain'],
Expand Down Expand Up @@ -62,7 +62,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
throw new TransformationFailedException();
}

$password->hash = $password->hashing->hash($value['plain'], $password->algorithm);
$password->hash = $password->hashing->encodePassword($value['plain'], null);

if (\function_exists('sodium_memzero')) {
sodium_memzero($value['plain']);
Expand All @@ -86,7 +86,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

$valid = false;
if (\is_string($value)) {
$valid = null === $password->hash ? false : $password->hashing->isValid($password->hash, $value, $password->algorithm);
$valid = null === $password->hash ? false : $password->hashing->isPasswordValid($password->hash, $value, null);
if (\function_exists('sodium_memzero')) {
sodium_memzero($value);
}
Expand All @@ -110,20 +110,18 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'label' => false,
'password_hashing' => null,
'password_algorithm' => null,
'hashing' => UserIdentity::class,
'password_options' => [],
'password_confirm' => false,
'password_confirm_options' => static function (Options $options, $value) {
return $value ?? $options['password_options'];
},
]);

$resolver->setAllowedTypes('password_hashing', ['null', PasswordHashing::class]);
$resolver->setAllowedTypes('password_algorithm', ['null', 'callable', 'int', 'string', PasswordAlgorithm::class]);
$resolver->setAllowedTypes('hashing', ['string']);
$resolver->setAllowedTypes('password_options', ['array']);
$resolver->setAllowedTypes('password_confirm', ['bool']);
$resolver->setAllowedTypes('password_confirm_options', ['null', 'array']);
$resolver->setAllowedTypes('password_options', ['array']);
}

private static function withConstraint(array $options, Constraint $constraint): array
Expand All @@ -138,26 +136,6 @@ private static function withConstraint(array $options, Constraint $constraint):

return $options;
}

/**
* @param callable|int|string|PasswordAlgorithm|null $algorithm
*/
private function createAlgorithm($algorithm): ?PasswordAlgorithm
{
if ($algorithm instanceof PasswordAlgorithm) {
return $algorithm;
}

if (\is_int($algorithm)) {
return PasswordAlgorithm::create($algorithm);
}

if (\is_string($algorithm)) {
return PasswordAlgorithm::createLegacy($algorithm);
}

return \is_callable($algorithm) ? $algorithm() : null;
}
}

/**
Expand All @@ -166,13 +144,11 @@ private function createAlgorithm($algorithm): ?PasswordAlgorithm
final class Password
{
public $hashing;
public $algorithm;
/** @var string|null */
public $hash;

public function __construct(PasswordHashing $hashing, ?PasswordAlgorithm $algorithm)
public function __construct(PasswordEncoderInterface $hashing)
{
$this->hashing = $hashing;
$this->algorithm = $algorithm;
}
}
84 changes: 0 additions & 84 deletions src/User/Infrastructure/Security/HashingFactory.php

This file was deleted.

56 changes: 0 additions & 56 deletions src/User/Infrastructure/Security/PasswordHashing.php

This file was deleted.

34 changes: 17 additions & 17 deletions src/User/Infrastructure/Security/UserIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
namespace MsgPhp\User\Infrastructure\Security;

use MsgPhp\User\Credential\PasswordProtectedCredential;
use MsgPhp\User\Password\PasswordAlgorithm;
use MsgPhp\User\User;
use MsgPhp\User\UserId;
use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class UserIdentity implements UserInterface, EquatableInterface
final class UserIdentity implements UserInterface, EquatableInterface, EncoderAwareInterface
{
/** @var UserId */
private $id;
Expand All @@ -24,29 +24,29 @@ final class UserIdentity implements UserInterface, EquatableInterface
private $roles;
/** @var string|null */
private $password;
/** @var PasswordAlgorithm|null */
private $passwordAlgorithm;
/** @var string */
private $hashing;

/**
* @param array<int, string> $roles
*/
public function __construct(User $user, string $originUsername = null, array $roles = [])
public function __construct(User $user, string $originUsername = null, array $roles = [], string $hashing = null)
{
$this->id = $user->getId();

if ($this->id->isEmpty()) {
throw new \LogicException('The user ID cannot be empty.');
}

$this->originUsername = $originUsername;
$this->roles = $roles;

$credential = $user->getCredential();

if ($credential instanceof PasswordProtectedCredential) {
$this->password = $credential->getPassword();
$this->passwordAlgorithm = $credential->getPasswordAlgorithm();
}

$this->originUsername = $originUsername;
$this->roles = $roles;
$this->hashing = $hashing ?? self::class;
}

public function getUserId(): UserId
Expand Down Expand Up @@ -77,23 +77,23 @@ public function getPassword(): string
return $this->password ?? '';
}

public function getPasswordAlgorithm(): ?PasswordAlgorithm
{
return $this->passwordAlgorithm;
}

public function getSalt(): ?string
{
return $this->passwordAlgorithm->salt->token ?? null;
return null;
}

public function eraseCredentials(): void
{
$this->password = $this->passwordAlgorithm = null;
$this->password = null;
}

public function isEqualTo(UserInterface $user)
public function isEqualTo(UserInterface $user): bool
{
return $user instanceof self && $user->getUserId()->equals($this->id);
}

public function getEncoderName(): string
{
return $this->hashing;
}
}
Loading