AddAppPassword.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, NextCloud, Inc.
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Sean Molenaar <sean@seanmolenaar.eu>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\User;
  26. use OC\Authentication\Events\AppPasswordCreatedEvent;
  27. use OC\Authentication\Token\IProvider;
  28. use OC\Authentication\Token\IToken;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IUserManager;
  31. use OCP\Security\ISecureRandom;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Helper\QuestionHelper;
  34. use Symfony\Component\Console\Input\InputArgument;
  35. use Symfony\Component\Console\Input\InputInterface;
  36. use Symfony\Component\Console\Input\InputOption;
  37. use Symfony\Component\Console\Output\OutputInterface;
  38. use Symfony\Component\Console\Question\Question;
  39. class AddAppPassword extends Command {
  40. protected IUserManager $userManager;
  41. protected IProvider $tokenProvider;
  42. private ISecureRandom $random;
  43. private IEventDispatcher $eventDispatcher;
  44. public function __construct(IUserManager $userManager,
  45. IProvider $tokenProvider,
  46. ISecureRandom $random,
  47. IEventDispatcher $eventDispatcher) {
  48. $this->tokenProvider = $tokenProvider;
  49. $this->userManager = $userManager;
  50. $this->random = $random;
  51. $this->eventDispatcher = $eventDispatcher;
  52. parent::__construct();
  53. }
  54. protected function configure() {
  55. $this
  56. ->setName('user:add-app-password')
  57. ->setDescription('Add app password for the named user')
  58. ->addArgument(
  59. 'user',
  60. InputArgument::REQUIRED,
  61. 'Username to add app password for'
  62. )
  63. ->addOption(
  64. 'password-from-env',
  65. null,
  66. InputOption::VALUE_NONE,
  67. 'Read password from environment variable NC_PASS/OC_PASS. Alternatively it will be asked for interactively or an app password without the login password will be created.'
  68. )
  69. ;
  70. }
  71. protected function execute(InputInterface $input, OutputInterface $output): int {
  72. $username = $input->getArgument('user');
  73. $password = null;
  74. $user = $this->userManager->get($username);
  75. if (is_null($user)) {
  76. $output->writeln('<error>User does not exist</error>');
  77. return 1;
  78. }
  79. if ($input->getOption('password-from-env')) {
  80. $password = getenv('NC_PASS') ?? getenv('OC_PASS');
  81. if (!$password) {
  82. $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>');
  83. return 1;
  84. }
  85. } elseif ($input->isInteractive()) {
  86. /** @var QuestionHelper $helper */
  87. $helper = $this->getHelper('question');
  88. $question = new Question('Enter the user password: ');
  89. $question->setHidden(true);
  90. /** @var null|string $password */
  91. $password = $helper->ask($input, $output, $question);
  92. }
  93. if ($password === null) {
  94. $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>');
  95. }
  96. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  97. $generatedToken = $this->tokenProvider->generateToken(
  98. $token,
  99. $user->getUID(),
  100. $user->getUID(),
  101. $password,
  102. 'cli',
  103. IToken::PERMANENT_TOKEN,
  104. IToken::DO_NOT_REMEMBER
  105. );
  106. $this->eventDispatcher->dispatchTyped(
  107. new AppPasswordCreatedEvent($generatedToken)
  108. );
  109. $output->writeln('app password:');
  110. $output->writeln($token);
  111. return 0;
  112. }
  113. }