2008-04-26 08:31:52 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* This class represents a validator for member passwords.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-26 08:31:52 +02:00
|
|
|
* <code>
|
|
|
|
* $pwdVal = new PasswordValidator();
|
|
|
|
* $pwdValidator->minLength(7);
|
|
|
|
* $pwdValidator->checkHistoricalPasswords(6);
|
2013-10-15 12:44:50 +02:00
|
|
|
* $pwdValidator->characterStrength(3, array("lowercase", "uppercase", "digits", "punctuation"));
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-26 08:31:52 +02:00
|
|
|
* Member::set_password_validator($pwdValidator);
|
|
|
|
* </code>
|
2008-06-15 15:33:53 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage security
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
|
|
|
class PasswordValidator extends Object {
|
2013-10-21 19:10:37 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $character_strength_tests = array(
|
2008-04-26 08:31:52 +02:00
|
|
|
'lowercase' => '/[a-z]/',
|
|
|
|
'uppercase' => '/[A-Z]/',
|
|
|
|
'digits' => '/[0-9]/',
|
|
|
|
'punctuation' => '/[^A-Za-z0-9]/',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
protected $minLength, $minScore, $testNames, $historicalPasswordCount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum password length
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function minLength($minLength) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->minLength = $minLength;
|
2013-10-21 19:10:37 +02:00
|
|
|
return $this;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Check the character strength of the password.
|
|
|
|
*
|
|
|
|
* Eg: $this->characterStrength(3, array("lowercase", "uppercase", "digits", "punctuation"))
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-04-26 08:31:52 +02:00
|
|
|
* @param $minScore The minimum number of character tests that must pass
|
|
|
|
* @param $testNames The names of the tests to perform
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function characterStrength($minScore, $testNames) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->minScore = $minScore;
|
|
|
|
$this->testNames = $testNames;
|
2013-10-21 19:10:37 +02:00
|
|
|
return $this;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Check a number of previous passwords that the user has used, and don't let them change to that.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function checkHistoricalPasswords($count) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->historicalPasswordCount = $count;
|
2013-10-21 19:10:37 +02:00
|
|
|
return $this;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:03:19 +02:00
|
|
|
/**
|
|
|
|
* @param String $password
|
|
|
|
* @param Member $member
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function validate($password, $member) {
|
2013-10-21 19:10:37 +02:00
|
|
|
$valid = ValidationResult::create();
|
2013-06-08 00:48:27 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
if($this->minLength) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(strlen($password) < $this->minLength) {
|
|
|
|
$valid->error(
|
2013-06-08 00:48:27 +02:00
|
|
|
sprintf(
|
|
|
|
_t(
|
|
|
|
'PasswordValidator.TOOSHORT',
|
|
|
|
'Password is too short, it must be %s or more characters long'
|
|
|
|
),
|
|
|
|
$this->minLength
|
|
|
|
),
|
|
|
|
'TOO_SHORT'
|
2012-09-26 23:34:00 +02:00
|
|
|
);
|
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($this->minScore) {
|
|
|
|
$score = 0;
|
|
|
|
$missedTests = array();
|
|
|
|
foreach($this->testNames as $name) {
|
2013-06-08 00:48:27 +02:00
|
|
|
if(preg_match(self::config()->character_strength_tests[$name], $password)) {
|
|
|
|
$score++;
|
|
|
|
} else {
|
|
|
|
$missedTests[] = _t(
|
|
|
|
'PasswordValidator.STRENGTHTEST' . strtoupper($name),
|
|
|
|
$name,
|
|
|
|
'The user needs to add this to their password for more complexity'
|
|
|
|
);
|
|
|
|
}
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2013-06-08 00:48:27 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
if($score < $this->minScore) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$valid->error(
|
2013-06-08 00:48:27 +02:00
|
|
|
sprintf(
|
|
|
|
_t(
|
|
|
|
'PasswordValidator.LOWCHARSTRENGTH',
|
|
|
|
'Please increase password strength by adding some of the following characters: %s'
|
|
|
|
),
|
|
|
|
implode(', ', $missedTests)
|
|
|
|
),
|
|
|
|
'LOW_CHARACTER_STRENGTH'
|
2012-09-26 23:34:00 +02:00
|
|
|
);
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-08 00:48:27 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
if($this->historicalPasswordCount) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$previousPasswords = MemberPassword::get()
|
|
|
|
->where(array('"MemberPassword"."MemberID"' => $member->ID))
|
|
|
|
->sort('"Created" DESC, "ID" DESC')
|
|
|
|
->limit($this->historicalPasswordCount);
|
2008-04-26 08:31:52 +02:00
|
|
|
if($previousPasswords) foreach($previousPasswords as $previousPasswords) {
|
|
|
|
if($previousPasswords->checkPassword($password)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$valid->error(
|
2013-06-08 00:48:27 +02:00
|
|
|
_t(
|
|
|
|
'PasswordValidator.PREVPASSWORD',
|
|
|
|
'You\'ve already used that password in the past, please choose a new password'
|
|
|
|
),
|
|
|
|
'PREVIOUS_PASSWORD'
|
2012-09-26 23:34:00 +02:00
|
|
|
);
|
2008-04-26 08:31:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
return $valid;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|