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

[stable23] Fix occ user:add-app-password #31536

Merged
merged 4 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion apps/settings/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public function parse($language, IEvent $event, IEvent $previousEvent = null): I
} elseif ($event->getSubject() === self::EMAIL_CHANGED) {
$subject = $this->l->t('Your email address was changed by an administrator');
} elseif ($event->getSubject() === self::APP_TOKEN_CREATED) {
$subject = $this->l->t('You created app password "{token}"');
if ($event->getAffectedUser() === $event->getAuthor()) {
$subject = $this->l->t('You created app password "{token}"');
} else {
$subject = $this->l->t('An administrator created app password "{token}"');
}
} elseif ($event->getSubject() === self::APP_TOKEN_DELETED) {
$subject = $this->l->t('You deleted app password "{token}"');
} elseif ($event->getSubject() === self::APP_TOKEN_RENAMED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\Activity\IManager as IActivityManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -40,12 +41,17 @@ class AppPasswordCreatedActivityListener implements IEventListener {
/** @var IActivityManager */
private $activityManager;

/** @var IUserSession */
private $userSession;

/** @var LoggerInterface */
private $logger;

public function __construct(IActivityManager $activityManager,
IUserSession $userSession,
LoggerInterface $logger) {
$this->activityManager = $activityManager;
$this->userSession = $userSession;
$this->logger = $logger;
}

Expand All @@ -58,7 +64,7 @@ public function handle(Event $event): void {
$activity->setApp('settings')
->setType('security')
->setAffectedUser($event->getToken()->getUID())
->setAuthor($event->getToken()->getUID())
->setAuthor($this->userSession->getUser() ? $this->userSession->getUser()->getUID() : '')
->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $event->getToken()->getName()])
->setObject('app_token', $event->getToken()->getId());

Expand Down
23 changes: 15 additions & 8 deletions core/Command/User/AddAppPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
*/
namespace OC\Core\Command\User;

use OC\Authentication\Events\AppPasswordCreatedEvent;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUserManager;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
Expand All @@ -44,17 +45,17 @@ class AddAppPassword extends Command {
protected $tokenProvider;
/** @var ISecureRandom */
private $random;
/** @var ICrypto */
private $crypto;
/** @var IEventDispatcher */
private $eventDispatcher;

public function __construct(IUserManager $userManager,
IProvider $tokenProvider,
ISecureRandom $random,
ICrypto $crypto) {
IEventDispatcher $eventDispatcher) {
$this->tokenProvider = $tokenProvider;
$this->userManager = $userManager;
$this->random = $random;
$this->crypto = $crypto;
$this->eventDispatcher = $eventDispatcher;
parent::__construct();
}

Expand Down Expand Up @@ -108,11 +109,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$output->writeln('<info>The password is not validated so what you provide is what gets recorded in the token</info>');

if (!$this->userManager->checkPassword($user->getUID(), $password)) {
$output->writeln('<error>The provided password is invalid</error>');
return 1;
}

$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
$this->tokenProvider->generateToken(
$generatedToken = $this->tokenProvider->generateToken(
$token,
$user->getUID(),
$user->getUID(),
Expand All @@ -122,6 +125,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
IToken::DO_NOT_REMEMBER
);

$this->eventDispatcher->dispatchTyped(
new AppPasswordCreatedEvent($generatedToken)
);

$output->writeln('app password:');
$output->writeln($token);

Expand Down
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
$application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection()));
$application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\User\AddAppPassword(\OC::$server->get(\OCP\IUserManager::class), \OC::$server->get(\OC\Authentication\Token\IProvider::class), \OC::$server->get(\OCP\Security\ISecureRandom::class), \OC::$server->get(\OCP\Security\ICrypto::class)));
$application->add(new OC\Core\Command\User\AddAppPassword(\OC::$server->get(\OCP\IUserManager::class), \OC::$server->get(\OC\Authentication\Token\IProvider::class), \OC::$server->get(\OCP\Security\ISecureRandom::class), \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class)));

$application->add(new OC\Core\Command\Group\Add(\OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\Group\Delete(\OC::$server->getGroupManager()));
Expand Down