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
21 changes: 18 additions & 3 deletions docs/blacklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,18 @@ this library provides a number of command-line commands which you can use.
To use these commands you need to install the [Symfony Console component][2].
And register these commands for usage (see the Symfony manual for details).

Each command expects a PSR-11 compatible container with the service-id
as the provider name. At least "default" is expected to exists.

### Commands

```php
$providersContainer = ...; // \Psr\Container\ContainerInterface

$application->add(new Rollerworks\Component\PasswordStrength\Command\BlacklistListCommand($providersContainer));
```


To add new passwords to the blacklist:

```bash
Expand All @@ -155,21 +165,26 @@ $ bin/console rollerworks-password:blacklist:update --file="/tmp/passwords-black
To remove the database completely (**this will remove all the blacklisted passwords from your database**).

```bash
$ app/console rollerworks-password:blacklist:purge
$ bin/console rollerworks-password:blacklist:purge
```

To export the database (this will display all the blacklisted passwords (one per line)) use.

```bash
$ app/console rollerworks-password:blacklist:list
$ bin/console rollerworks-password:blacklist:list
```

You can also forward the result to a text file.

```bash
$ app/console rollerworks-password:blacklist:list > /tmp/exported-blacklist.txt
$ bin/console rollerworks-password:blacklist:list > /tmp/exported-blacklist.txt
```

### Use a different provider

To use a different provider then the de "default" use the `--provider` option, eg.
`bin/console rollerworks-password:blacklist:purge --provider=sqlite`

## Existing blacklists

To get started you can use the bad/leaked passwords databases provider by
Expand Down
24 changes: 20 additions & 4 deletions src/Command/BlacklistCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Rollerworks\Component\PasswordStrength\Command;

use Psr\Container\ContainerInterface;
use Rollerworks\Component\PasswordStrength\Blacklist\BlacklistProviderInterface;
use Rollerworks\Component\PasswordStrength\Blacklist\UpdatableBlacklistProviderInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Sebastiaan Stok <[email protected]>
Expand All @@ -25,15 +29,27 @@ abstract class BlacklistCommand extends Command
*/
protected $blacklistProvider;

public function __construct(BlacklistProviderInterface $blacklistProvider)
/**
* @var ContainerInterface
*/
private $providers;

public function __construct(ContainerInterface $providers)
{
parent::__construct(null);

$this->blacklistProvider = $blacklistProvider;
$this->addOption('provider', null, InputOption::VALUE_REQUIRED, 'Blacklist Provider name', 'default');
$this->providers = $providers;
}

public function isEnabled()
protected function initialize(InputInterface $input, OutputInterface $output)
{
return $this->blacklistProvider instanceof UpdatableBlacklistProviderInterface;
$this->blacklistProvider = $this->providers->get($input->getOption('provider'));

if (!$this->blacklistProvider instanceof UpdatableBlacklistProviderInterface) {
throw new \RuntimeException(
sprintf('Blacklist provider "%s" is not updatable.', $input->getOption('provider'))
);
}
}
}
64 changes: 64 additions & 0 deletions tests/Command/BlacklistCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the RollerworksPasswordStrengthValidator package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Rollerworks\Component\PasswordStrength\Tests\Command;

use Rollerworks\Component\PasswordStrength\Command\BlacklistListCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class BlacklistCommandTest extends BlacklistCommandTestCase
{
public function testValidatesProviderMustBeUpdatable()
{
$container = $this->createLoadersContainer([
'default' => self::$blackListProvider,
'second' => $this->createMockedProvider('nope'),
]);

$application = new Application();
$application->add(new BlacklistListCommand($container));

$commandTester = new CommandTester($application->find('rollerworks-password:blacklist:list'));

$this->expectException('\RuntimeException');
$this->expectExceptionMessage('Blacklist provider "second" is not updatable.');

$commandTester->execute(['command' => $application->find('rollerworks-password:blacklist:list')->getName(), '--provider' => 'second']);
}

public function testSecondProviderIsUsed()
{
$blackListedWords = ['test', 'foobar', 'kaboom'];
foreach ($blackListedWords as $word) {
self::$blackListProvider->add($word);
}

$container = $this->createLoadersContainer([
'default' => $this->createMockedProvider('nope'),
'second' => self::$blackListProvider,
]);

$application = new Application();
$application->add(new BlacklistListCommand($container));

$commandTester = new CommandTester($application->find('rollerworks-password:blacklist:list'));
$commandTester->execute(['command' => $application->find('rollerworks-password:blacklist:list')->getName(), '--provider' => 'second']);

$display = $commandTester->getDisplay(true);

// Words may be displayed in any order, so check each of them
foreach ($blackListedWords as $word) {
self::assertRegExp("/([\n]|^){$word}[\n]/s", $display);
self::$blackListProvider->add($word);
}
}
}
3 changes: 3 additions & 0 deletions tests/Command/BlacklistCommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

use PHPUnit\Framework\TestCase;
use Rollerworks\Component\PasswordStrength\Blacklist\SqliteProvider;
use Rollerworks\Component\PasswordStrength\Tests\BlackListMockProviderTrait;

abstract class BlacklistCommandTestCase extends TestCase
{
use BlackListMockProviderTrait;

protected static $dbFile;
protected static $storage;

Expand Down
4 changes: 3 additions & 1 deletion tests/Command/BlacklistDeleteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public function testImportFromEmptyFile()
private function getCommand()
{
$application = new Application();
$application->add(new BlacklistDeleteCommand(self::$blackListProvider));
$application->add(new BlacklistDeleteCommand(
$this->createLoadersContainer(['default' => self::$blackListProvider])
));

return $application->find('rollerworks-password:blacklist:delete');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/BlacklistListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BlacklistListCommandTest extends BlacklistCommandTestCase
public function testList()
{
$application = new Application();
$command = new BlacklistListCommand(self::$blackListProvider);
$command = new BlacklistListCommand($this->createLoadersContainer(['default' => self::$blackListProvider]));
$application->add($command);

$command = $application->find('rollerworks-password:blacklist:list');
Expand Down
4 changes: 3 additions & 1 deletion tests/Command/BlacklistPurgeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public function testNoAsk()
private function getCommand()
{
$application = new Application();
$application->add(new BlacklistPurgeCommand(self::$blackListProvider));
$application->add(
new BlacklistPurgeCommand($this->createLoadersContainer(['default' => self::$blackListProvider]))
);

return $application->find('rollerworks-password:blacklist:purge');
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Command/BlacklistUpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public function testImportFromEmptyFile()
private function getCommand()
{
$application = new Application();
$application->add(new BlacklistUpdateCommand(self::$blackListProvider));
$application->add(
new BlacklistUpdateCommand($this->createLoadersContainer(['default' => self::$blackListProvider]))
);

return $application->find('rollerworks-password:blacklist:update');
}
Expand Down