|
12 | 12 | use OC\Authentication\Events\AppPasswordCreatedEvent; |
13 | 13 | use OC\Authentication\Token\IProvider; |
14 | 14 | use OC\Authentication\Token\IToken; |
| 15 | +use OC\User\Manager as UserManager; |
15 | 16 | use OCP\EventDispatcher\IEventDispatcher; |
16 | | -use OCP\IUserManager; |
17 | 17 | use OCP\Security\ISecureRandom; |
18 | 18 | use Symfony\Component\Console\Command\Command; |
19 | 19 | use Symfony\Component\Console\Helper\QuestionHelper; |
|
25 | 25 |
|
26 | 26 | class Add extends Command { |
27 | 27 | public function __construct( |
28 | | - protected IUserManager $userManager, |
29 | | - protected IProvider $tokenProvider, |
| 28 | + private UserManager $userManager, |
| 29 | + private IProvider $tokenProvider, |
30 | 30 | private ISecureRandom $random, |
31 | 31 | private IEventDispatcher $eventDispatcher, |
32 | 32 | ) { |
@@ -92,12 +92,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
92 | 92 | $password = $helper->ask($input, $output, $question); |
93 | 93 | } |
94 | 94 |
|
| 95 | + $loginName = $input->getOption('login-name') ?? $user->getUID(); |
| 96 | + |
95 | 97 | if ($password === null) { |
96 | 98 | $output->writeln('<info>No password provided. The generated app password will therefore have limited capabilities. Any operation that requires the login password will fail.</info>'); |
| 99 | + } else { |
| 100 | + $loggedInUser = $this->userManager->checkPasswordNoLogging($loginName, $password); |
| 101 | + if ($loggedInUser === false) { |
| 102 | + $output->writeln('<error>The given password is invalid for login ' . $loginName . '</error>'); |
| 103 | + return self::FAILURE; |
| 104 | + } elseif ($loggedInUser->getUID() !== $user->getUID()) { |
| 105 | + $output->writeln('<error>The user ' . $username . ' does not match the given login ' . $loginName . '</error>'); |
| 106 | + return self::FAILURE; |
| 107 | + } |
97 | 108 | } |
98 | 109 |
|
99 | | - $loginName = $input->getOption('login-name') ?? $user->getUID(); |
100 | | - |
101 | 110 | $tokenName = $input->getOption('name') ?: 'cli'; |
102 | 111 |
|
103 | 112 | $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
0 commit comments