Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Introduced Password PseudoRandomBytesGenerator.php
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLelaisant committed Apr 7, 2017
1 parent d752fad commit e32f29b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Knp/Rad/User/Bundle/UserBundle.php
Expand Up @@ -2,11 +2,20 @@

namespace Knp\Rad\User\Bundle;

use Knp\Rad\User\DependencyInjection\Compiler\PasswordGenerator;
use Knp\Rad\User\DependencyInjection\UserExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class UserBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new PasswordGenerator());
}

/**
* {@inheritdoc}
*/
Expand Down
@@ -0,0 +1,28 @@
<?php

namespace Knp\Rad\User\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;

class PasswordGenerator implements CompilerPassInterface
{
const GENERATOR_INDEX = 0;

public function process(ContainerBuilder $container)
{
if ( ! extension_loaded('openssl')) {
return;
}

$listener = $container->findDefinition('knp_rad_user.event_listener.persistence.password_generation_listener');

try {
$generator = $listener->getArgument(self::GENERATOR_INDEX);
} catch (OutOfBoundsException $e) {
$sslGenerator = $container->findDefinition('knp_rad_user.password.generator.pseudo_random_bytes_generator');
$listener->setArguments([$sslGenerator]);
}
}
}
26 changes: 26 additions & 0 deletions src/Knp/Rad/User/Password/Generator/PseudoRandomBytesGenerator.php
@@ -0,0 +1,26 @@
<?php

namespace Knp\Rad\User\Password\Generator;

use Knp\Rad\User\Password\Generator;

class PseudoRandomBytesGenerator implements Generator
{
/**
* @var int
*/
private $length;

/**
* @param int $length
*/
public function __construct($length)
{
$this->length = $length;
}

public function generate()
{
return openssl_random_pseudo_bytes($this->length);
}
}
5 changes: 5 additions & 0 deletions src/Knp/Rad/User/Password/Generator/UniqidGenerator.php
Expand Up @@ -2,8 +2,13 @@

namespace Knp\Rad\User\Password\Generator;

trigger_error(E_USER_DEPRECATED, 'UniqidGenerator is now deprecated and will be removed in future versions.');

use Knp\Rad\User\Password\Generator;

/**
* @deprecated Use Knp\Rad\User\Password\Generator\PseudoRandomBytesGenerator instead
*/
class UniqidGenerator implements Generator
{
/**
Expand Down
7 changes: 6 additions & 1 deletion src/Knp/Rad/User/Resources/config/services.xml
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="knp_rad_user.password.generator.random_bytes.length">20</parameter>
<parameter key="knp_rad_user.salt.generator.random_bytes.length">255</parameter>
</parameters>

Expand Down Expand Up @@ -33,8 +34,12 @@
<argument type="service" id="knp_rad_user.salt.generator"/>
</service>

<service id="knp_rad_user.password.generator.pseudo_random_bytes_generator" class="Knp\Rad\User\Password\Generator\PseudoRandomBytesGenerator" public="false" lazy="true">
<argument>%knp_rad_user.password.generator.random_bytes.length%</argument>
</service>

<service id="knp_rad_user.salt.generator.pseudo_random_bytes_generator" class="Knp\Rad\User\Salt\Generator\PseudoRandomBytesGenerator" public="false" lazy="true">
<argument type="parameter" id="knp_rad_user.salt.generator.random_bytes.length"/>
<argument>%knp_rad_user.salt.generator.random_bytes.length%</argument>
</service>

<service id="knp_rad_user.salt.generator" alias="knp_rad_user.salt.generator.pseudo_random_bytes_generator" />
Expand Down

0 comments on commit e32f29b

Please sign in to comment.