-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from Saeven/feature/password-validator
Feature/password validator
- Loading branch information
Showing
17 changed files
with
265 additions
and
88 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
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
77 changes: 77 additions & 0 deletions
77
bundle/Spec/CirclicalUser/Factory/Service/PasswordChecker/PasswordCheckerFactorySpec.php
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 | ||
|
||
namespace Spec\CirclicalUser\Factory\Service\PasswordChecker; | ||
|
||
use CirclicalUser\Exception\PasswordStrengthCheckerException; | ||
use CirclicalUser\Factory\Service\PasswordChecker\PasswordCheckerFactory; | ||
use CirclicalUser\Mapper\RoleMapper; | ||
use CirclicalUser\Provider\PasswordCheckerInterface; | ||
use CirclicalUser\Service\PasswordChecker\PasswordNotChecked; | ||
use CirclicalUser\Service\PasswordChecker\Zxcvbn; | ||
use Interop\Container\ContainerInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class PasswordCheckerFactorySpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(PasswordCheckerFactory::class); | ||
} | ||
|
||
public function it_creates_plain_types(ContainerInterface $container) | ||
{ | ||
$config = [ | ||
'circlical' => [ | ||
'user' => [ | ||
'providers' => [ | ||
'role' => RoleMapper::class, | ||
], | ||
], | ||
], | ||
]; | ||
$container->get('config')->willReturn($config); | ||
$this->__invoke($container, PasswordCheckerInterface::class, [])->shouldBeAnInstanceOf(PasswordNotChecked::class); | ||
} | ||
|
||
public function it_creates_specific_types(ContainerInterface $container) | ||
{ | ||
$config = [ | ||
'circlical' => [ | ||
'user' => [ | ||
'providers' => [ | ||
'role' => RoleMapper::class, | ||
], | ||
'password_strength_checker' => [ | ||
'implementation' => \CirclicalUser\Service\PasswordChecker\Zxcvbn::class, | ||
'config' => ['required_strength' => 3,], | ||
], | ||
], | ||
], | ||
]; | ||
$container->get('config')->willReturn($config); | ||
$this->__invoke($container, PasswordCheckerInterface::class, [])->shouldBeAnInstanceOf(Zxcvbn::class); | ||
} | ||
|
||
public function it_requires_options_when_array_notation_is_used(ContainerInterface $container) | ||
{ | ||
$config = [ | ||
'circlical' => [ | ||
'user' => [ | ||
'providers' => [ | ||
'role' => RoleMapper::class, | ||
], | ||
'password_strength_checker' => [ | ||
'implementation' => \CirclicalUser\Service\PasswordChecker\Zxcvbn::class, | ||
], | ||
], | ||
], | ||
]; | ||
$container->get('config')->willReturn($config); | ||
$this->shouldThrow(PasswordStrengthCheckerException::class) | ||
->during('__invoke', [ | ||
$container, | ||
PasswordCheckerInterface::class, | ||
[], | ||
]); | ||
} | ||
} |
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
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
39 changes: 39 additions & 0 deletions
39
src/CirclicalUser/Factory/Service/PasswordChecker/PasswordCheckerFactory.php
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,39 @@ | ||
<?php | ||
|
||
namespace CirclicalUser\Factory\Service\PasswordChecker; | ||
|
||
use CirclicalUser\Exception\PasswordStrengthCheckerException; | ||
use CirclicalUser\Provider\PasswordCheckerInterface; | ||
use CirclicalUser\Service\PasswordChecker\PasswordNotChecked; | ||
use Interop\Container\ContainerInterface; | ||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
|
||
class PasswordCheckerFactory implements FactoryInterface | ||
{ | ||
|
||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
$config = $container->get('config'); | ||
$userConfig = $config['circlical']['user']; | ||
$passwordChecker = null; | ||
if (!empty($userConfig['password_strength_checker'])) { | ||
if (is_array($userConfig['password_strength_checker'])) { | ||
if (!is_string($userConfig['password_strength_checker']['implementation'] ?? null) || !is_array($userConfig['password_strength_checker']['config'] ?? null)) { | ||
throw new PasswordStrengthCheckerException("When using array notation, the password strength checker must contain 'implementation' and 'config'"); | ||
} | ||
$checkerImplementation = new $userConfig['password_strength_checker']['implementation']($userConfig['password_strength_checker']['config']); | ||
} else { | ||
$checkerImplementation = new $userConfig['password_strength_checker']; | ||
} | ||
|
||
if (!$checkerImplementation instanceof PasswordCheckerInterface) { | ||
throw new \RuntimeException("An invalid type of password checker was specified!"); | ||
} | ||
|
||
return $checkerImplementation; | ||
} | ||
|
||
return new PasswordNotChecked(); | ||
} | ||
} | ||
|
Oops, something went wrong.