Add.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\AuthTokens;
  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 Add extends Command {
  40. public function __construct(
  41. protected IUserManager $userManager,
  42. protected IProvider $tokenProvider,
  43. private ISecureRandom $random,
  44. private IEventDispatcher $eventDispatcher,
  45. ) {
  46. parent::__construct();
  47. }
  48. protected function configure() {
  49. $this
  50. ->setName('user:auth-tokens:add')
  51. ->setAliases(['user:add-app-password'])
  52. ->setDescription('Add app password for the named account')
  53. ->addArgument(
  54. 'user',
  55. InputArgument::REQUIRED,
  56. 'Login to add app password for'
  57. )
  58. ->addOption(
  59. 'password-from-env',
  60. null,
  61. InputOption::VALUE_NONE,
  62. '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.'
  63. )
  64. ;
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output): int {
  67. $username = $input->getArgument('user');
  68. $password = null;
  69. $user = $this->userManager->get($username);
  70. if (is_null($user)) {
  71. $output->writeln('<error>Account does not exist</error>');
  72. return 1;
  73. }
  74. if ($input->getOption('password-from-env')) {
  75. $password = getenv('NC_PASS') ?? getenv('OC_PASS');
  76. if (!$password) {
  77. $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>');
  78. return 1;
  79. }
  80. } elseif ($input->isInteractive()) {
  81. /** @var QuestionHelper $helper */
  82. $helper = $this->getHelper('question');
  83. $question = new Question('Enter the account password: ');
  84. $question->setHidden(true);
  85. /** @var null|string $password */
  86. $password = $helper->ask($input, $output, $question);
  87. }
  88. if ($password === null) {
  89. $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>');
  90. }
  91. $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
  92. $generatedToken = $this->tokenProvider->generateToken(
  93. $token,
  94. $user->getUID(),
  95. $user->getUID(),
  96. $password,
  97. 'cli',
  98. IToken::PERMANENT_TOKEN,
  99. IToken::DO_NOT_REMEMBER
  100. );
  101. $this->eventDispatcher->dispatchTyped(
  102. new AppPasswordCreatedEvent($generatedToken)
  103. );
  104. $output->writeln('app password:');
  105. $output->writeln($token);
  106. return 0;
  107. }
  108. }