Skip to content
This repository was archived by the owner on Oct 31, 2018. It is now read-only.
Closed
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
33 changes: 16 additions & 17 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
*/
namespace OCA\Security\AppInfo;

use OC\AppFramework\Utility\TimeFactory;
use OCA\Security\SecurityConfig;
use \OCP\AppFramework\App;

use \OCA\Security\Throttle;
use \OCA\Security\Hooks;
use \OCA\Security\Db\DbService;
Expand All @@ -39,7 +37,7 @@ public function __construct(array $urlParams=array()){
$container->registerService('DbService', function($c) {
return new DbService(
$c->query('ServerContainer')->getDb(),
$c->query('OCP\AppFramework\Utility\ITimeFactory')
$c->query('OCP\AppFramework\Utility\ITimeFactory')
);
});

Expand All @@ -49,26 +47,27 @@ public function __construct(array $urlParams=array()){
);
});

$container->registerService('SecurityConfig', function($c) {
return new SecurityConfig(
$c->query('OCP\IConfig')
);
});
$container->registerService('SecurityConfig', function($c) {
return new SecurityConfig(
$c->query('OCP\IConfig')
);
});

$container->registerService('PasswordValidator', function($c) {
return new PasswordValidator(
$c->query('SecurityConfig'),
$c->query('OCP\IL10N')
);
});
$container->registerService('PasswordValidator', function($c) {
return new PasswordValidator(
$c->query('SecurityConfig'),
$c->query('OCP\IL10N')
);
});

$container->registerService('Hooks', function($c) {
return new Hooks(
$c->query('ServerContainer')->getUserManager(),
$c->query('Throttle'),
$c->query('Request'),
$c->query('PasswordValidator'),
\OC::$server->getEventDispatcher()
$c->query('Request'),
$c->query('PasswordValidator'),
\OC::$server->getEventDispatcher(),
$c->query('SecurityConfig')
);
});

Expand Down
38 changes: 23 additions & 15 deletions lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,46 @@ class Hooks {

/** @var PasswordValidator */
private $passValidator;
/** @var EventDispatcherInterface */
private $dispatcher;

/** @var EventDispatcherInterface */
private $dispatcher;

/** @var SecurityConfig */
private $config;

/**
* @param IUserManager $userManager
* @param Throttle $throttle
* @param IRequest $request
* @param PasswordValidator $passValidator
* @param EventDispatcherInterface $dispatcher
* @param PasswordValidator $passValidator
* @param EventDispatcherInterface $dispatcher
* @param SecurityConfig $config
*/
public function __construct($userManager, $throttle, $request, $passValidator, $dispatcher){
public function __construct($userManager, $throttle, $request, $passValidator, $dispatcher, $config){
$this->userManager = $userManager;
$this->throttle = $throttle;
$this->request = $request;
$this->passValidator = $passValidator;
$this->dispatcher = $dispatcher;
$this->config = $config;

}

public function register() {
$this->userManager->listen('\OC\User', 'failedLogin', function($uid) {
$this->failedLoginCallback($uid);
if($this->config->getIsBruteForceProtectionEnabled() === true) {
$this->userManager->listen('\OC\User', 'failedLogin', function ($uid) {
$this->failedLoginCallback($uid);
});

$this->userManager->listen('\OC\User', 'postLogin', function ($user) {
$this->postLoginCallback($user);
});
}

$this->dispatcher->addListener('OCP\User::validatePassword', function(GenericEvent $event) {
$this->passValidator->validate($event->getArgument('password'));
});

$this->userManager->listen('\OC\User', 'postLogin', function($user) {
$this->postLoginCallback($user);
});

$this->dispatcher->addListener('OCP\User::validatePassword', function(GenericEvent $event) {
$this->passValidator->validate($event->getArgument('password'));
});

}

/**
Expand Down
258 changes: 129 additions & 129 deletions lib/SecurityConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,137 +29,137 @@
*/
class SecurityConfig {

/** @var IConfig */
private $config;
/** @var IConfig */
private $config;

/**
* Config constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
}
/**
* Config constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
}

/**
* is brute force protection enabled
*
* @return array
*/
public function getAllSecurityConfigs() {
return [
'isBruteForceProtectionEnabled' => $this->getIsBruteForceProtectionEnabled(),
'minPasswordLength' => $this->getMinPasswordLength(),
'isUpperLowerCaseEnforced' => $this->getIsUpperLowerCaseEnforced(),
'isNumericalCharsEnforced' => $this->getIsNumericCharactersEnforced(),
'isSpecialCharsEnforced' => $this->getIsSpecialCharactersEnforced()
];
}
/**
* get all security configs
*
* @return array
*/
public function getAllSecurityConfigs() {
return [
'isBruteForceProtectionEnabled' => $this->getIsBruteForceProtectionEnabled(),
'minPasswordLength' => $this->getMinPasswordLength(),
'isUpperLowerCaseEnforced' => $this->getIsUpperLowerCaseEnforced(),
'isNumericalCharsEnforced' => $this->getIsNumericCharactersEnforced(),
'isSpecialCharsEnforced' => $this->getIsSpecialCharactersEnforced()
];
}

/**
* is brute force protection enabled
*
* @return bool
*/
public function getIsBruteForceProtectionEnabled() {
$enableBruteForceProtection = $this->config->getAppValue(
'security',
'enable_brute_force_protection',
'0'
);
return boolval($enableBruteForceProtection);
}
/**
* is brute force protection enabled
*
* @return bool
*/
public function getIsBruteForceProtectionEnabled() {
$enableBruteForceProtection = $this->config->getAppValue(
'security',
'enable_brute_force_protection',
'0'
);
return boolval($enableBruteForceProtection);
}

/**
* get the enforced minimum length of passwords
*
* @return int
*/
public function getMinPasswordLength() {
$minLength = $this->config->getAppValue('security', 'min_password_length', '8');
return intval($minLength);
}
/**
* does the password need to contain upper and lower case characters
*
* @return bool
*/
public function getIsUpperLowerCaseEnforced() {
$enforceUpperLowerCase = $this->config->getAppValue(
'security',
'enforce_upper_lower_case',
'0'
);
return boolval($enforceUpperLowerCase);
}
/**
* does the password need to contain numeric characters
*
* @return bool
*/
public function getIsNumericCharactersEnforced() {
$enforceNumericCharacters = $this->config->getAppValue(
'security',
'enforce_numeric_characters',
'0'
);
return boolval($enforceNumericCharacters);
}
/**
* does the password need to contain special characters
*
* @return bool
*/
public function getIsSpecialCharactersEnforced() {
$enforceSpecialCharacters = $this->config->getAppValue(
'security',
'enforce_special_characters',
'0'
);
return boolval($enforceSpecialCharacters);
}
/**
* enable brute force protection
*
* @param bool $enableBruteForceProtection
*/
public function setIsBruteForceProtectionEnabled($enableBruteForceProtection) {
$value = $enableBruteForceProtection === true ? '1' : '0';
$this->config->setAppValue('security', 'enable_brute_force_protection', $value);
}
/**
* set minimal length of passwords
*
* @param int $minLength
*/
public function setMinPasswordLength($minLength) {
$this->config->setAppValue('security', 'min_password_length', $minLength);
}
/**
* enforce upper and lower characters case
*
* @param bool $enforceUpperLowerCase
*/
public function setIsUpperLowerCaseEnforced($enforceUpperLowerCase) {
$value = $enforceUpperLowerCase === true ? '1' : '0';
$this->config->setAppValue('security', 'enforce_upper_lower_case', $value);
}
/**
* enforce numeric characters
*
* @param bool $enforceNumericCharacters
*/
public function setIsNumericCharactersEnforced($enforceNumericCharacters) {
$value = $enforceNumericCharacters === true ? '1' : '0';
$this->config->setAppValue('security', 'enforce_numeric_characters', $value);
}
/**
* enforce special characters
*
* @param bool $enforceSpecialCharacters
*/
public function setIsSpecialCharactersEnforced($enforceSpecialCharacters) {
$value = $enforceSpecialCharacters === true ? '1' : '0';
$this->config->setAppValue('security', 'enforce_special_characters', $value);
}
/**
* get the enforced minimum length of passwords
*
* @return int
*/
public function getMinPasswordLength() {
$minLength = $this->config->getAppValue('security', 'min_password_length', '8');
return intval($minLength);
}
/**
* does the password need to contain upper and lower case characters
*
* @return bool
*/
public function getIsUpperLowerCaseEnforced() {
$enforceUpperLowerCase = $this->config->getAppValue(
'security',
'enforce_upper_lower_case',
'0'
);
return boolval($enforceUpperLowerCase);
}
/**
* does the password need to contain numeric characters
*
* @return bool
*/
public function getIsNumericCharactersEnforced() {
$enforceNumericCharacters = $this->config->getAppValue(
'security',
'enforce_numeric_characters',
'0'
);
return boolval($enforceNumericCharacters);
}
/**
* does the password need to contain special characters
*
* @return bool
*/
public function getIsSpecialCharactersEnforced() {
$enforceSpecialCharacters = $this->config->getAppValue(
'security',
'enforce_special_characters',
'0'
);
return boolval($enforceSpecialCharacters);
}
/**
* enable brute force protection
*
* @param bool $enableBruteForceProtection
*/
public function setIsBruteForceProtectionEnabled($enableBruteForceProtection) {
$value = $enableBruteForceProtection === true ? '1' : '0';
$this->config->setAppValue('security', 'enable_brute_force_protection', $value);
}
/**
* set minimal length of passwords
*
* @param int $minLength
*/
public function setMinPasswordLength($minLength) {
$this->config->setAppValue('security', 'min_password_length', $minLength);
}
/**
* enforce upper and lower characters case
*
* @param bool $enforceUpperLowerCase
*/
public function setIsUpperLowerCaseEnforced($enforceUpperLowerCase) {
$value = $enforceUpperLowerCase === true ? '1' : '0';
$this->config->setAppValue('security', 'enforce_upper_lower_case', $value);
}
/**
* enforce numeric characters
*
* @param bool $enforceNumericCharacters
*/
public function setIsNumericCharactersEnforced($enforceNumericCharacters) {
$value = $enforceNumericCharacters === true ? '1' : '0';
$this->config->setAppValue('security', 'enforce_numeric_characters', $value);
}
/**
* enforce special characters
*
* @param bool $enforceSpecialCharacters
*/
public function setIsSpecialCharactersEnforced($enforceSpecialCharacters) {
$value = $enforceSpecialCharacters === true ? '1' : '0';
$this->config->setAppValue('security', 'enforce_special_characters', $value);
}
}