|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Christoph Wurst <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
7
|
|
|
* @license AGPL-3.0 |
|
8
|
|
|
* |
|
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
11
|
|
|
* as published by the Free Software Foundation. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU Affero General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace OC\Core\Controller; |
|
24
|
|
|
|
|
25
|
|
|
use OC\AppFramework\Http; |
|
26
|
|
|
use OC\Authentication\Token\DefaultTokenProvider; |
|
27
|
|
|
use OC\Authentication\Token\IProvider; |
|
28
|
|
|
use OC\Authentication\Token\IToken; |
|
29
|
|
|
use OC\Authentication\TwoFactorAuth\Manager as TwoFactorAuthManager; |
|
30
|
|
|
use OC\User\Manager as UserManager; |
|
31
|
|
|
use OCA\User_LDAP\User\Manager; |
|
32
|
|
|
use OCP\AppFramework\Controller; |
|
33
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
34
|
|
|
use OCP\IRequest; |
|
35
|
|
|
use OCP\Security\ISecureRandom; |
|
36
|
|
|
|
|
37
|
|
|
class TokenController extends Controller { |
|
38
|
|
|
|
|
39
|
|
|
/** @var UserManager */ |
|
40
|
|
|
private $userManager; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IProvider */ |
|
43
|
|
|
private $tokenProvider; |
|
44
|
|
|
|
|
45
|
|
|
/** @var TwoFactorAuthManager */ |
|
46
|
|
|
private $twoFactorAuthManager; |
|
47
|
|
|
|
|
48
|
|
|
/** @var ISecureRandom */ |
|
49
|
|
|
private $secureRandom; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $appName |
|
53
|
|
|
* @param IRequest $request |
|
54
|
|
|
* @param Manager $userManager |
|
55
|
|
|
* @param DefaultTokenProvider $tokenProvider |
|
56
|
|
|
* @param ISecureRandom $secureRandom |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public function __construct($appName, IRequest $request, UserManager $userManager, IProvider $tokenProvider, TwoFactorAuthManager $twoFactorAuthManager, ISecureRandom $secureRandom) { |
|
|
|
|
|
|
59
|
|
|
parent::__construct($appName, $request); |
|
60
|
|
|
$this->userManager = $userManager; |
|
61
|
|
|
$this->tokenProvider = $tokenProvider; |
|
62
|
|
|
$this->secureRandom = $secureRandom; |
|
63
|
|
|
$this->twoFactorAuthManager = $twoFactorAuthManager; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Generate a new access token clients can authenticate with |
|
68
|
|
|
* |
|
69
|
|
|
* @PublicPage |
|
70
|
|
|
* @NoCSRFRequired |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $user |
|
73
|
|
|
* @param string $password |
|
74
|
|
|
* @param string $name the name of the client |
|
75
|
|
|
* @return JSONResponse |
|
76
|
|
|
*/ |
|
77
|
|
|
public function generateToken($user, $password, $name = 'unknown client') { |
|
78
|
|
|
if (is_null($user) || is_null($password)) { |
|
79
|
|
|
$response = new JSONResponse(); |
|
80
|
|
|
$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); |
|
81
|
|
|
return $response; |
|
82
|
|
|
} |
|
83
|
|
|
$loginName = $user; |
|
84
|
|
|
$user = $this->userManager->checkPassword($loginName, $password); |
|
85
|
|
|
if ($user === false) { |
|
86
|
|
|
$response = new JSONResponse(); |
|
87
|
|
|
$response->setStatus(Http::STATUS_UNAUTHORIZED); |
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($this->twoFactorAuthManager->isTwoFactorAuthenticated($user)) { |
|
92
|
|
|
$resp = new JSONResponse(); |
|
93
|
|
|
$resp->setStatus(Http::STATUS_UNAUTHORIZED); |
|
94
|
|
|
return $resp; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$token = $this->secureRandom->generate(128); |
|
98
|
|
|
$this->tokenProvider->generateToken($token, $user->getUID(), $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
99
|
|
|
return [ |
|
|
|
|
|
|
100
|
|
|
'token' => $token, |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.