@@ -45,93 +45,93 @@ |
||
45 | 45 | |
46 | 46 | class AddAppPassword extends Command { |
47 | 47 | |
48 | - /** @var IUserManager */ |
|
49 | - protected $userManager; |
|
50 | - /** @var IProvider */ |
|
51 | - protected $tokenProvider; |
|
52 | - /** @var ISecureRandom */ |
|
53 | - private $random; |
|
54 | - /** @var ICrypto */ |
|
55 | - private $crypto; |
|
56 | - |
|
57 | - public function __construct(IUserManager $userManager, |
|
58 | - IProvider $tokenProvider, |
|
59 | - ISecureRandom $random, |
|
60 | - ICrypto $crypto) { |
|
61 | - $this->tokenProvider = $tokenProvider; |
|
62 | - $this->userManager = $userManager; |
|
63 | - $this->random = $random; |
|
64 | - $this->crypto = $crypto; |
|
65 | - parent::__construct(); |
|
66 | - } |
|
67 | - |
|
68 | - protected function configure() { |
|
69 | - $this |
|
70 | - ->setName('user:add-app-password') |
|
71 | - ->setDescription('Add app password for the named user') |
|
72 | - ->addArgument( |
|
73 | - 'user', |
|
74 | - InputArgument::REQUIRED, |
|
75 | - 'Username to add app password for' |
|
76 | - ) |
|
77 | - ->addOption( |
|
78 | - 'password-from-env', |
|
79 | - null, |
|
80 | - InputOption::VALUE_NONE, |
|
81 | - 'read password from environment variable NC_PASS/OC_PASS' |
|
82 | - ) |
|
83 | - ; |
|
84 | - } |
|
85 | - |
|
86 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
87 | - $username = $input->getArgument('user'); |
|
88 | - |
|
89 | - $user = $this->userManager->get($username); |
|
90 | - if (is_null($user)) { |
|
91 | - $output->writeln('<error>User does not exist</error>'); |
|
92 | - return 1; |
|
93 | - } |
|
94 | - |
|
95 | - if ($input->getOption('password-from-env')) { |
|
96 | - $password = getenv('NC_PASS') ?? getenv('OC_PASS'); |
|
97 | - if (!$password) { |
|
98 | - $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>'); |
|
99 | - return 1; |
|
100 | - } |
|
101 | - } elseif ($input->isInteractive()) { |
|
102 | - /** @var QuestionHelper $helper */ |
|
103 | - $helper = $this->getHelper('question'); |
|
104 | - |
|
105 | - $question = new Question('Enter the user password: '); |
|
106 | - $question->setHidden(true); |
|
107 | - $password = $helper->ask($input, $output, $question); |
|
108 | - |
|
109 | - if ($password === null) { |
|
110 | - $output->writeln("<error>Password cannot be empty!</error>"); |
|
111 | - return 1; |
|
112 | - } |
|
113 | - } else { |
|
114 | - $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
|
115 | - return 1; |
|
116 | - } |
|
117 | - |
|
118 | - $output->writeln('<info>The password is not validated so what you provide is what gets recorded in the token</info>'); |
|
119 | - |
|
120 | - |
|
121 | - $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
122 | - $this->tokenProvider->generateToken( |
|
123 | - $token, |
|
124 | - $user->getUID(), |
|
125 | - $user->getUID(), |
|
126 | - $password, |
|
127 | - 'cli', |
|
128 | - IToken::PERMANENT_TOKEN, |
|
129 | - IToken::DO_NOT_REMEMBER |
|
130 | - ); |
|
131 | - |
|
132 | - $output->writeln('app password:'); |
|
133 | - $output->writeln($token); |
|
134 | - |
|
135 | - return 0; |
|
136 | - } |
|
48 | + /** @var IUserManager */ |
|
49 | + protected $userManager; |
|
50 | + /** @var IProvider */ |
|
51 | + protected $tokenProvider; |
|
52 | + /** @var ISecureRandom */ |
|
53 | + private $random; |
|
54 | + /** @var ICrypto */ |
|
55 | + private $crypto; |
|
56 | + |
|
57 | + public function __construct(IUserManager $userManager, |
|
58 | + IProvider $tokenProvider, |
|
59 | + ISecureRandom $random, |
|
60 | + ICrypto $crypto) { |
|
61 | + $this->tokenProvider = $tokenProvider; |
|
62 | + $this->userManager = $userManager; |
|
63 | + $this->random = $random; |
|
64 | + $this->crypto = $crypto; |
|
65 | + parent::__construct(); |
|
66 | + } |
|
67 | + |
|
68 | + protected function configure() { |
|
69 | + $this |
|
70 | + ->setName('user:add-app-password') |
|
71 | + ->setDescription('Add app password for the named user') |
|
72 | + ->addArgument( |
|
73 | + 'user', |
|
74 | + InputArgument::REQUIRED, |
|
75 | + 'Username to add app password for' |
|
76 | + ) |
|
77 | + ->addOption( |
|
78 | + 'password-from-env', |
|
79 | + null, |
|
80 | + InputOption::VALUE_NONE, |
|
81 | + 'read password from environment variable NC_PASS/OC_PASS' |
|
82 | + ) |
|
83 | + ; |
|
84 | + } |
|
85 | + |
|
86 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
87 | + $username = $input->getArgument('user'); |
|
88 | + |
|
89 | + $user = $this->userManager->get($username); |
|
90 | + if (is_null($user)) { |
|
91 | + $output->writeln('<error>User does not exist</error>'); |
|
92 | + return 1; |
|
93 | + } |
|
94 | + |
|
95 | + if ($input->getOption('password-from-env')) { |
|
96 | + $password = getenv('NC_PASS') ?? getenv('OC_PASS'); |
|
97 | + if (!$password) { |
|
98 | + $output->writeln('<error>--password-from-env given, but NC_PASS is empty!</error>'); |
|
99 | + return 1; |
|
100 | + } |
|
101 | + } elseif ($input->isInteractive()) { |
|
102 | + /** @var QuestionHelper $helper */ |
|
103 | + $helper = $this->getHelper('question'); |
|
104 | + |
|
105 | + $question = new Question('Enter the user password: '); |
|
106 | + $question->setHidden(true); |
|
107 | + $password = $helper->ask($input, $output, $question); |
|
108 | + |
|
109 | + if ($password === null) { |
|
110 | + $output->writeln("<error>Password cannot be empty!</error>"); |
|
111 | + return 1; |
|
112 | + } |
|
113 | + } else { |
|
114 | + $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
|
115 | + return 1; |
|
116 | + } |
|
117 | + |
|
118 | + $output->writeln('<info>The password is not validated so what you provide is what gets recorded in the token</info>'); |
|
119 | + |
|
120 | + |
|
121 | + $token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
122 | + $this->tokenProvider->generateToken( |
|
123 | + $token, |
|
124 | + $user->getUID(), |
|
125 | + $user->getUID(), |
|
126 | + $password, |
|
127 | + 'cli', |
|
128 | + IToken::PERMANENT_TOKEN, |
|
129 | + IToken::DO_NOT_REMEMBER |
|
130 | + ); |
|
131 | + |
|
132 | + $output->writeln('app password:'); |
|
133 | + $output->writeln($token); |
|
134 | + |
|
135 | + return 0; |
|
136 | + } |
|
137 | 137 | } |