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

Add PasswordSecurity as a service #2

Merged
merged 6 commits into from
May 15, 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
11 changes: 7 additions & 4 deletions app/sprinkles/account/config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@
]
],
'password_security' => [
'enforce_no_compromised' => '0' // Set to false to turn off this feature. Otherwise, provide a numeric string, which sets the maximum number
// of times that is "acceptable" for a password to have appeared in breaches. The recommended and most secure
// option is '0' - meaning only passwords that are not on the list of compromised passwords will be allowed.
]
'enforce_no_compromised' => [
'breaches' => '0', // Set to '-1' to turn off this feature. Otherwise, provide a numeric string, which sets the maximum number
// of times that is "acceptable" for a password to have appeared in breaches. The recommended and most secure
// option is '0' - meaning only passwords that are not on the list of compromised passwords will be allowed.
'cache' => 10080 // Duration in minutes to store HIBP API responses in cache.
]
]
],

/*
Expand Down
12 changes: 11 additions & 1 deletion app/sprinkles/account/src/Authenticate/PasswordSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,21 @@ private function getHashArrayFromAPI($hashPrefix)
return $hashArray;
}

public function isEnabled()
/**
* Checks if compromised password reset feature is enabeld.
*
* @return bool True if the feature is enabled.
*/
public function resetCompromisedEnabled()
{
return $this->config['site.login.enforce_reset_compromised'];
}

/**
* Checks the maximum number of times that is acceptable for a password to have appeared in breaches.
*
* @return string Numeric string with -1 meaning disabled.
*/
public function breachThreshold()
{
return $this->config['site.password_security.enforce_no_compromised.breaches'];
Expand Down
13 changes: 7 additions & 6 deletions app/sprinkles/account/src/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use UserFrosting\Support\Exception\BadRequestException;
use UserFrosting\Support\Exception\ForbiddenException;
use UserFrosting\Support\Exception\NotFoundException;
use UserFrosting\Sprinkle\Account\Authenticate\PasswordSecurity;

/**
* Controller class for /account/* URLs. Handles account-related activities, including login, registration, password recovery, and account settings.
Expand Down Expand Up @@ -428,12 +427,14 @@ public function login(Request $request, Response $response, $args)

$currentUser = $authenticator->attempt(($isEmail ? 'email' : 'user_name'), $userIdentifier, $data['password'], $data['rememberme']);

// Check if the enforced password update setting is configured.
if ($this->ci->config['site.login.enforce_reset_compromised'] == true) {
$passwordSecurity = $this->ci->passwordSecurity;

// Check if the enforce password update setting is configured.
if ($passwordSecurity->resetCompromisedEnabled()) {
// Check if the password is on the compromised password list.
$numberOfBreaches = PasswordSecurity::checkPassword($data['password']);
$numberOfBreaches = $passwordSecurity->checkPassword($data['password']);

if ($numberOfBreaches > $this->ci->config['site.password_security.enforce_no_compromised']) {
if ($passwordSecurity->breachThreshold() != '-1' && $numberOfBreaches > $passwordSecurity->breachThreshold()) {

// Try to generate a new password reset request.
// Use timeout for "reset password"
Expand All @@ -452,7 +453,7 @@ public function login(Request $request, Response $response, $args)
]
], $token);

$ms->addMessageTranslated('info', 'PASSWORD.SECURITY.RESET_REQUIRED');
$ms->addMessageTranslated('info', 'PASSWORD.SECURITY.RESET_REQUIRED.COMPROMISED');

return $response->withRedirect($forcePasswordChange);
}
Expand Down
14 changes: 14 additions & 0 deletions app/sprinkles/account/src/ServicesProvider/ServicesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Authenticate\AuthGuard;
use UserFrosting\Sprinkle\Account\Authenticate\Hasher;
use UserFrosting\Sprinkle\Account\Authenticate\PasswordSecurity;
use UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager;
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Account\Log\UserActivityDatabaseHandler;
Expand Down Expand Up @@ -173,6 +174,19 @@ public function register(ContainerInterface $container)
return $authenticator;
};

/**
* PasswordSecurity service.
*
* Handles password security features.
*
* @return \UserFrosting\Sprinkle\Account\Authenticate\PasswordSecurity
*/
$container['passwordSecurity'] = function ($c) {
$cache = $c->cache;
$config = $c->config;

return new PasswordSecurity($cache, $config);
};
/**
* Sets up the AuthGuard middleware, used to limit access to authenticated users for certain routes.
*
Expand Down