Skip to content

Commit d243a2a

Browse files
committed
Command: Add app password generation
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
1 parent 691409c commit d243a2a

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2016, ownCloud, Inc.
4+
*
5+
* @author Andreas Fischer <bantu@owncloud.com>
6+
* @author Christopher Schäpers <kondou@ts.unde.re>
7+
* @author Clark Tomlinson <fallen013@gmail.com>
8+
* @author Joas Schilling <coding@schilljs.com>
9+
* @author Laurens Post <lkpost@scept.re>
10+
* @author Morris Jobke <hey@morrisjobke.de>
11+
* @author Roeland Jago Douma <roeland@famdouma.nl>
12+
* @author Sujith H <sharidasan@owncloud.com>
13+
* @author Sean Molenaar <sean@seanmolenaar.eu>
14+
*
15+
* @license AGPL-3.0
16+
*
17+
* This code is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU Affero General Public License, version 3,
19+
* as published by the Free Software Foundation.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License, version 3,
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>
28+
*
29+
*/
30+
31+
namespace OC\Core\Command\User;
32+
33+
use OC\Authentication\Token\IProvider;
34+
use OC\Authentication\Token\IToken;
35+
use OCA\OAuth2\Db\AccessToken;
36+
use OCA\OAuth2\Db\AccessTokenMapper;
37+
use OCP\IUserManager;
38+
use OCP\Security\ICrypto;
39+
use OCP\Security\ISecureRandom;
40+
use Symfony\Component\Console\Command\Command;
41+
use Symfony\Component\Console\Helper\QuestionHelper;
42+
use Symfony\Component\Console\Input\InputArgument;
43+
use Symfony\Component\Console\Input\InputInterface;
44+
use Symfony\Component\Console\Input\InputOption;
45+
use Symfony\Component\Console\Output\OutputInterface;
46+
use Symfony\Component\Console\Question\Question;
47+
48+
class AddAppPassword extends Command {
49+
50+
/** @var IUserManager */
51+
protected $userManager;
52+
/** @var IProvider */
53+
protected $tokenProvider;
54+
/** @var ISecureRandom */
55+
private $random;
56+
/** @var AccessTokenMapper */
57+
private $accessTokenMapper;
58+
/** @var ICrypto */
59+
private $crypto;
60+
61+
public function __construct(IUserManager $userManager,
62+
IProvider $tokenProvider,
63+
ISecureRandom $random,
64+
ICrypto $crypto,
65+
AccessTokenMapper $accessTokenMapper) {
66+
$this->accessTokenMapper = $accessTokenMapper;
67+
$this->tokenProvider = $tokenProvider;
68+
$this->userManager = $userManager;
69+
$this->random = $random;
70+
$this->crypto = $crypto;
71+
parent::__construct();
72+
}
73+
74+
protected function configure() {
75+
$this
76+
->setName('user:add-app-password')
77+
->setDescription('Add app password for the named user')
78+
->addArgument(
79+
'user',
80+
InputArgument::REQUIRED,
81+
'Username to add app password for'
82+
)
83+
->addOption(
84+
'password-from-env',
85+
null,
86+
InputOption::VALUE_NONE,
87+
'read password from environment variable OC_PASS'
88+
)
89+
;
90+
}
91+
92+
protected function execute(InputInterface $input, OutputInterface $output): int {
93+
$username = $input->getArgument('user');
94+
95+
$user = $this->userManager->get($username);
96+
if (is_null($user)) {
97+
$output->writeln('<error>User does not exist</error>');
98+
return 1;
99+
}
100+
101+
if ($input->getOption('password-from-env')) {
102+
$password = getenv('OC_PASS');
103+
if (!$password) {
104+
$output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
105+
return 1;
106+
}
107+
} elseif ($input->isInteractive()) {
108+
/** @var QuestionHelper $helper */
109+
$helper = $this->getHelper('question');
110+
111+
$question = new Question('Enter the user password: ');
112+
$question->setHidden(true);
113+
$password = $helper->ask($input, $output, $question);
114+
115+
if ($password === null) {
116+
$output->writeln("<error>Password cannot be empty!</error>");
117+
return 1;
118+
}
119+
} else {
120+
$output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>");
121+
return 1;
122+
}
123+
124+
125+
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
126+
$generatedToken = $this->tokenProvider->generateToken(
127+
$token,
128+
$user->getUID(),
129+
$user->getDisplayName(),
130+
$password,
131+
'cli',
132+
IToken::PERMANENT_TOKEN,
133+
IToken::DO_NOT_REMEMBER
134+
);
135+
136+
try {
137+
$code = $this->random->generate(128, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
138+
$accessToken = new AccessToken();
139+
$accessToken->setClientId(0);
140+
$accessToken->setEncryptedToken($this->crypto->encrypt($token, $code));
141+
$accessToken->setHashedCode(hash('sha512', $code));
142+
$accessToken->setTokenId($generatedToken->getId());
143+
$this->accessTokenMapper->insert($accessToken);
144+
145+
$output->writeln('app password:');
146+
$output->writeln($token);
147+
} catch (\Exception $e) {
148+
$output->writeln('<error>' . $e->getMessage() . '</error>');
149+
return 1;
150+
}
151+
152+
return 0;
153+
}
154+
}

core/register_command.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*/
4545

4646
/** @var Symfony\Component\Console\Application $application */
47+
4748
$application->add(new \Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand());
4849
$application->add(new OC\Core\Command\Status);
4950
$application->add(new OC\Core\Command\Check(\OC::$server->getSystemConfig()));
@@ -177,6 +178,7 @@
177178
$application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection()));
178179
$application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
179180
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
181+
$application->add(new OC\Core\Command\User\AddAppPassword(\OC::$server->getUserManager(), \OC::$server->get(\OC\Authentication\Token\IProvider::class), \OC::$server->get(\OCP\Security\ISecureRandom::class), \OC::$server->get(\OCP\Security\ICrypto::class), \OC::$server->get(\OCA\OAuth2\Db\AccessTokenMapper::class)));
180182

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

0 commit comments

Comments
 (0)