Completed
Pull Request — master (#31)
by Blizzz
13:09 queued 04:36
created

TokenController::generateToken()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 3
b 0
f 0
nc 4
nop 3
dl 0
loc 26
rs 8.439
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('token' => $token); (array<string,string>) is incompatible with the return type documented by OC\Core\Controller\TokenController::generateToken of type OCP\AppFramework\Http\JSONResponse.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
100
			'token' => $token,
101
		];
102
	}
103
104
}
105