respect password policy for auto generated passwords

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2017-04-19 16:56:34 +02:00
parent 428d7cdb5e
commit 972b4c04e2
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
3 changed files with 44 additions and 6 deletions

View File

@ -21,6 +21,7 @@
namespace OCA\ShareByMail;
use OC\CapabilitiesManager;
use OC\HintException;
use OC\Share20\Exception\InvalidShare;
use OCA\ShareByMail\Settings\SettingsManager;
@ -43,7 +44,6 @@ use OC\Share20\Share;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IShare;
use OCP\Share\IShareProvider;
use OCP\Template;
/**
* Class ShareByMail
@ -88,6 +88,9 @@ class ShareByMailProvider implements IShareProvider {
/** @var IHasher */
private $hasher;
/** @var CapabilitiesManager */
private $capabilitiesManager;
/**
* Return the identifier of this provider.
*
@ -112,6 +115,7 @@ class ShareByMailProvider implements IShareProvider {
* @param SettingsManager $settingsManager
* @param Defaults $defaults
* @param IHasher $hasher
* @param CapabilitiesManager $capabilitiesManager
*/
public function __construct(
IDBConnection $connection,
@ -125,7 +129,8 @@ class ShareByMailProvider implements IShareProvider {
IManager $activityManager,
SettingsManager $settingsManager,
Defaults $defaults,
IHasher $hasher
IHasher $hasher,
CapabilitiesManager $capabilitiesManager
) {
$this->dbConnection = $connection;
$this->secureRandom = $secureRandom;
@ -139,6 +144,7 @@ class ShareByMailProvider implements IShareProvider {
$this->settingsManager = $settingsManager;
$this->defaults = $defaults;
$this->hasher = $hasher;
$this->capabilitiesManager = $capabilitiesManager;
}
/**
@ -202,12 +208,35 @@ class ShareByMailProvider implements IShareProvider {
);
}
$password = $this->generateToken(8);
$passwordPolicy = $this->getPasswordPolicy();
$passwordCharset = ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS;
$passwordLength = 8;
if (!empty($passwordPolicy)) {
$passwordLength = (int)$passwordPolicy['minLength'] > 0 ? (int)$passwordPolicy['minLength'] : $passwordLength;
$passwordCharset .= $passwordPolicy['enforceSpecialCharacters'] ? ISecureRandom::CHAR_SYMBOLS : '';
}
$password = $this->secureRandom->generate($passwordLength, $passwordCharset);
$share->setPassword($this->hasher->hash($password));
return $password;
}
/**
* get password policy
*
* @return array
*/
protected function getPasswordPolicy() {
$capabilities = $this->capabilitiesManager->getCapabilities();
if (isset($capabilities['password_policy'])) {
return $capabilities['password_policy'];
}
return [];
}
/**
* create activity if a file/folder was shared by mail
*

View File

@ -23,6 +23,7 @@
namespace OCA\ShareByMail\Tests;
use OC\CapabilitiesManager;
use OC\Mail\Message;
use OCA\ShareByMail\Settings\SettingsManager;
use OCA\ShareByMail\ShareByMailProvider;
@ -93,6 +94,9 @@ class ShareByMailProviderTest extends TestCase {
/** @var IHasher | \PHPUnit_Framework_MockObject_MockObject */
private $hasher;
/** @var CapabilitiesManager | \PHPUnit_Framework_MockObject_MockObject */
private $capabilitiesManager;
public function setUp() {
parent::setUp();
@ -115,6 +119,7 @@ class ShareByMailProviderTest extends TestCase {
$this->settingsManager = $this->getMockBuilder(SettingsManager::class)->disableOriginalConstructor()->getMock();
$this->defaults = $this->createMock(Defaults::class);
$this->hasher = $this->getMockBuilder(IHasher::class)->getMock();
$this->capabilitiesManager = $this->getMockBuilder(CapabilitiesManager::class)->disableOriginalConstructor()->getMock();
$this->userManager->expects($this->any())->method('userExists')->willReturn(true);
}
@ -141,7 +146,8 @@ class ShareByMailProviderTest extends TestCase {
$this->activityManager,
$this->settingsManager,
$this->defaults,
$this->hasher
$this->hasher,
$this->capabilitiesManager
]
);
@ -162,7 +168,8 @@ class ShareByMailProviderTest extends TestCase {
$this->activityManager,
$this->settingsManager,
$this->defaults,
$this->hasher
$this->hasher,
$this->capabilitiesManager
);
}

View File

@ -23,6 +23,7 @@
*/
namespace OC\Share20;
use OC\CapabilitiesManager;
use OCA\FederatedFileSharing\AddressHandler;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCA\FederatedFileSharing\FederatedShareProvider;
@ -161,7 +162,8 @@ class ProviderFactory implements IProviderFactory {
$this->serverContainer->getActivityManager(),
$settingsManager,
$this->serverContainer->query(Defaults::class),
$this->serverContainer->getHasher()
$this->serverContainer->getHasher(),
$this->serverContainer->query(CapabilitiesManager::class)
);
}