|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Christoph Wurst <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Core\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use OC\AppFramework\Http; |
|
25
|
|
|
use OC\Authentication\Token\DefaultTokenProvider; |
|
26
|
|
|
use OC\Authentication\Token\IToken; |
|
27
|
|
|
use OC\User\Manager; |
|
28
|
|
|
use OCP\AppFramework\Controller; |
|
29
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
30
|
|
|
use OCP\AppFramework\Http\Response; |
|
31
|
|
|
use OCP\IRequest; |
|
32
|
|
|
use OCP\Security\ISecureRandom; |
|
33
|
|
|
|
|
34
|
|
|
class TokenController extends Controller { |
|
35
|
|
|
|
|
36
|
|
|
/** @var Manager */ |
|
37
|
|
|
private $userManager; |
|
38
|
|
|
|
|
39
|
|
|
/** @var DefaultTokenProvider */ |
|
40
|
|
|
private $tokenProvider; |
|
41
|
|
|
|
|
42
|
|
|
/** @var ISecureRandom */ |
|
43
|
|
|
private $secureRandom; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $appName |
|
47
|
|
|
* @param IRequest $request |
|
48
|
|
|
* @param Manager $userManager |
|
49
|
|
|
* @param DefaultTokenProvider $tokenProvider |
|
50
|
|
|
* @param ISecureRandom $secureRandom |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($appName, IRequest $request, Manager $userManager, DefaultTokenProvider $tokenProvider, |
|
53
|
|
|
ISecureRandom $secureRandom) { |
|
54
|
|
|
parent::__construct($appName, $request); |
|
55
|
|
|
$this->userManager = $userManager; |
|
56
|
|
|
$this->tokenProvider = $tokenProvider; |
|
57
|
|
|
$this->secureRandom = $secureRandom; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Generate a new access token clients can authenticate with |
|
62
|
|
|
* |
|
63
|
|
|
* @PublicPage |
|
64
|
|
|
* @NoCSRFRequired |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $user |
|
67
|
|
|
* @param string $password |
|
68
|
|
|
* @param string $name the name of the client |
|
69
|
|
|
* @return JSONResponse |
|
70
|
|
|
*/ |
|
71
|
|
|
public function generateToken($user, $password, $name = 'unknown client') { |
|
72
|
|
|
if (is_null($user) || is_null($password)) { |
|
73
|
|
|
$response = new Response(); |
|
74
|
|
|
$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); |
|
75
|
|
|
return $response; |
|
76
|
|
|
} |
|
77
|
|
|
$loginResult = $this->userManager->checkPassword($user, $password); |
|
78
|
|
|
if ($loginResult === false) { |
|
79
|
|
|
$response = new Response(); |
|
80
|
|
|
$response->setStatus(Http::STATUS_UNAUTHORIZED); |
|
81
|
|
|
return $response; |
|
82
|
|
|
} |
|
83
|
|
|
$token = $this->secureRandom->generate(128); |
|
84
|
|
|
$this->tokenProvider->generateToken($token, $loginResult->getUID(), $user, $password, $name, IToken::PERMANENT_TOKEN); |
|
85
|
|
|
return [ |
|
|
|
|
|
|
86
|
|
|
'token' => $token, |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.