Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split all the validators #84

Merged
merged 4 commits into from
Aug 27, 2019
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
195 changes: 35 additions & 160 deletions lib/PasswordValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,27 @@


use OC\HintException;
use OCP\Http\Client\IClientService;
use OCP\IL10N;
use OCA\Password_Policy\Validator\CommonPasswordsValidator;
use OCA\Password_Policy\Validator\HIBPValidator;
use OCA\Password_Policy\Validator\IValidator;
use OCA\Password_Policy\Validator\LengthValidator;
use OCA\Password_Policy\Validator\NumericCharacterValidator;
use OCA\Password_Policy\Validator\SpecialCharactersValidator;
use OCA\Password_Policy\Validator\UpperCaseLoweCaseValidator;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
use OCP\ILogger;

class PasswordValidator {

/** @var PasswordPolicyConfig */
private $config;
/** @var IAppContainer */
private $container;
/** @var ILogger */
private $logger;

/** @var IL10N */
private $l;

/** @var IClientService */
private $clientService;

/**
* PasswordValidator constructor.
*
* @param PasswordPolicyConfig $config
* @param IL10N $l
* @param IClientService $clientService
*/
public function __construct(PasswordPolicyConfig $config,
IL10N $l,
IClientService $clientService) {
$this->config = $config;
$this->l = $l;
$this->clientService = $clientService;
public function __construct(IAppContainer $container, ILogger $logger) {
$this->container = $container;
$this->logger = $logger;
}

/**
Expand All @@ -60,146 +54,27 @@ public function __construct(PasswordPolicyConfig $config,
* @param string $password
* @throws HintException
*/
public function validate($password) {
$this->checkCommonPasswords($password);
$this->checkPasswordLength($password);
$this->checkNumericCharacters($password);
$this->checkUpperLowerCase($password);
$this->checkSpecialCharacters($password);
$this->checkHaveIBeenPwned($password);
}

/**
* check if password matches the minimum length defined by the admin
*
* @param string $password
* @throws HintException
*/
protected function checkPasswordLength($password) {
$minLength = $this->config->getMinLength();
if(strlen($password) < $minLength) {
$message = 'Password needs to be at least ' . $minLength . ' characters long';
$message_t = $this->l->t(
'Password needs to be at least %s characters long', [$minLength]
);
throw new HintException($message, $message_t);
}
}

/**
* check if password contain at least one upper and one lower case character
*
* @param string $password
* @throws HintException
*/
protected function checkUpperLowerCase($password) {
$enforceUpperLowerCase = $this->config->getEnforceUpperLowerCase();
if($enforceUpperLowerCase) {
if (preg_match('/^(?=.*[a-z])(?=.*[A-Z]).+$/', $password) !== 1) {
$message = 'Password needs to contain at least one lower and one upper case character.';
$message_t = $this->l->t(
'Password needs to contain at least one lower and one upper case character.'
);
throw new HintException($message, $message_t);
}
}
}

/**
* check if password contain at least one numeric character
*
* @param string $password
* @throws HintException
*/
protected function checkNumericCharacters($password) {
$enforceNumericCharacters = $this->config->getEnforceNumericCharacters();
if($enforceNumericCharacters) {
if (preg_match('/^(?=.*\d).+$/', $password) !== 1) {
$message = 'Password needs to contain at least one numeric character';
$message_t = $this->l->t(
'Password needs to contain at least one numeric character.'
);
throw new HintException($message, $message_t);
}
}
}

/**
* check if password contain at least one special character
*
* @param string $password
* @throws HintException
*/
protected function checkSpecialCharacters($password) {
$enforceSpecialCharacters = $this->config->getEnforceSpecialCharacters();
if($enforceSpecialCharacters && ctype_alnum($password)) {
$message = 'Password needs to contain at least one special character.';
$message_t = $this->l->t(
'Password needs to contain at least one special character.'
);
throw new HintException($message, $message_t);
}
}


/**
* Checks if password is within the 100,000 most used passwords.
*
* @param string $password
* @throws HintException
*/
protected function checkCommonPasswords($password) {
$enforceNonCommonPassword = $this->config->getEnforceNonCommonPassword();
if($enforceNonCommonPassword) {
$passwordFile = __DIR__ . '/../lists/list-'.strlen($password).'.php';
if(file_exists($passwordFile)) {
$commonPasswords = require_once $passwordFile;
if (isset($commonPasswords[strtolower($password)])) {
$message = 'Password is among the 1,000,000 most common ones. Please make it unique.';
$message_t = $this->l->t(
'Password is among the 1,000,000 most common ones. Please make it unique.'
);
throw new HintException($message, $message_t);
}
}
}
}

/**
* Check if a password is in the list of breached passwords from
* haveibeenpwned.com
*
* @param string $password
* @throws HintException
*/
protected function checkHaveIBeenPwned($password) {
if ($this->config->getEnforceHaveIBeenPwned()) {
$hash = sha1($password);
$range = substr($hash, 0, 5);
$needle = strtoupper(substr($hash, 5));

$client = $this->clientService->newClient();

public function validate(string $password): void {
$validators = [
CommonPasswordsValidator::class,
LengthValidator::class,
NumericCharacterValidator::class,
UpperCaseLoweCaseValidator::class,
SpecialCharactersValidator::class,
HIBPValidator::class,
];

foreach ($validators as $validator) {
try {
$response = $client->get(
'https://api.pwnedpasswords.com/range/' . $range,
[
'timeout' => 5
]
);
} catch (\Exception $e) {
return;
/** @var IValidator $instance */
$instance = $this->container->query($validator);
} catch (QueryException $e) {
//ignore and continue
rullzer marked this conversation as resolved.
Show resolved Hide resolved
$this->logger->logException($e, ['level' => ILogger::INFO]);
continue;
}

$result = $response->getBody();

if (strpos($result, $needle) !== false) {
$message = 'Password is present in compromised password list. Please choose a different password.';
$message_t = $this->l->t(
'Password is present in compromised password list. Please choose a different password.'
);
throw new HintException($message, $message_t);
}
$instance->validate($password);
}
}

Expand Down
58 changes: 58 additions & 0 deletions lib/Validator/CommonPasswordsValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Password_Policy\Validator;

use OC\HintException;
use OCA\Password_Policy\PasswordPolicyConfig;
use OCP\IL10N;

class CommonPasswordsValidator implements IValidator {

/** @var PasswordPolicyConfig */
private $config;
/** @var IL10N */
private $l;

public function __construct(PasswordPolicyConfig $config, IL10N $l) {
$this->config = $config;
$this->l = $l;
}

public function validate(string $password): void {
$enforceNonCommonPassword = $this->config->getEnforceNonCommonPassword();
$passwordFile = __DIR__ . '/../../lists/list-'.strlen($password).'.php';
if($enforceNonCommonPassword && file_exists($passwordFile)) {
$commonPasswords = require_once $passwordFile;
if (isset($commonPasswords[strtolower($password)])) {
$message = 'Password is among the 1,000,000 most common ones. Please make it unique.';
$message_t = $this->l->t(
'Password is among the 1,000,000 most common ones. Please make it unique.'
);
throw new HintException($message, $message_t);
}
}
}

}
86 changes: 86 additions & 0 deletions lib/Validator/HIBPValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Password_Policy\Validator;

use OC\HintException;
use OCA\Password_Policy\PasswordPolicyConfig;
use OCP\Http\Client\IClientService;
use OCP\IL10N;
use OCP\ILogger;

class HIBPValidator implements IValidator {

/** @var PasswordPolicyConfig */
private $config;
/** @var IL10N */
private $l;
/** @var IClientService */
private $clientService;
/** @var ILogger */
private $logger;

public function __construct(PasswordPolicyConfig $config,
IL10N $l,
IClientService $clientService,
ILogger $logger) {
$this->config = $config;
$this->l = $l;
$this->clientService = $clientService;
$this->logger = $logger;
}

public function validate(string $password): void {
if ($this->config->getEnforceHaveIBeenPwned()) {
$hash = sha1($password);
$range = substr($hash, 0, 5);
$needle = strtoupper(substr($hash, 5));

$client = $this->clientService->newClient();

try {
$response = $client->get(
'https://api.pwnedpasswords.com/range/' . $range,
[
'timeout' => 5
]
);
} catch (\Exception $e) {
$this->logger->logException($e, ['level' => ILogger::INFO]);
return true;
}

$result = $response->getBody();

if (strpos($result, $needle) !== false) {
$message = 'Password is present in compromised password list. Please choose a different password.';
$message_t = $this->l->t(
'Password is present in compromised password list. Please choose a different password.'
);
throw new HintException($message, $message_t);
}
}
}

}
Loading