1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OC\Core\Controller; |
26
|
|
|
|
27
|
|
|
use OC\Authentication\Token\IProvider; |
28
|
|
|
use OC\Authentication\Token\IToken; |
29
|
|
|
use OCP\AppFramework\Http\DataResponse; |
30
|
|
|
use OCP\AppFramework\OCS\OCSForbiddenException; |
31
|
|
|
use OCP\Authentication\Exceptions\CredentialsUnavailableException; |
32
|
|
|
use OCP\Authentication\Exceptions\PasswordUnavailableException; |
33
|
|
|
use OCP\Authentication\LoginCredentials\IStore; |
34
|
|
|
use OCP\IRequest; |
35
|
|
|
use OCP\ISession; |
36
|
|
|
use OCP\Security\ISecureRandom; |
37
|
|
|
|
38
|
|
|
class AppPasswordController extends \OCP\AppFramework\OCSController { |
39
|
|
|
|
40
|
|
|
/** @var ISession */ |
41
|
|
|
private $session; |
42
|
|
|
|
43
|
|
|
/** @var ISecureRandom */ |
44
|
|
|
private $random; |
45
|
|
|
|
46
|
|
|
/** @var IProvider */ |
47
|
|
|
private $tokenProvider; |
48
|
|
|
|
49
|
|
|
/** @var IStore */ |
50
|
|
|
private $credentialStore; |
51
|
|
|
|
52
|
|
View Code Duplication |
public function __construct(string $appName, |
53
|
|
|
IRequest $request, |
54
|
|
|
ISession $session, |
55
|
|
|
ISecureRandom $random, |
56
|
|
|
IProvider $tokenProvider, |
57
|
|
|
IStore $credentialStore) { |
58
|
|
|
parent::__construct($appName, $request); |
59
|
|
|
|
60
|
|
|
$this->session = $session; |
61
|
|
|
$this->random = $random; |
62
|
|
|
$this->tokenProvider = $tokenProvider; |
63
|
|
|
$this->credentialStore = $credentialStore; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @NoAdminRequired |
68
|
|
|
* |
69
|
|
|
* @return DataResponse |
70
|
|
|
* @throws OCSForbiddenException |
71
|
|
|
*/ |
72
|
|
|
public function getAppPassword(): DataResponse { |
73
|
|
|
// We do not allow the creation of new tokens if this is an app password |
74
|
|
|
if ($this->session->exists('app_password')) { |
75
|
|
|
throw new OCSForbiddenException('You cannot request an new apppassword with an apppassword'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
$credentials = $this->credentialStore->getLoginCredentials(); |
80
|
|
|
} catch (CredentialsUnavailableException $e) { |
81
|
|
|
throw new OCSForbiddenException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$password = $credentials->getPassword(); |
86
|
|
|
} catch (PasswordUnavailableException $e) { |
87
|
|
|
$password = null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$userAgent = $this->request->getHeader('USER_AGENT'); |
91
|
|
|
|
92
|
|
|
$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
93
|
|
|
|
94
|
|
|
$this->tokenProvider->generateToken( |
95
|
|
|
$token, |
96
|
|
|
$credentials->getUID(), |
97
|
|
|
$credentials->getLoginName(), |
98
|
|
|
$password, |
99
|
|
|
$userAgent, |
100
|
|
|
IToken::PERMANENT_TOKEN, |
101
|
|
|
IToken::DO_NOT_REMEMBER |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return new DataResponse([ |
105
|
|
|
'apppassword' => $token |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|