Completed
Push — master ( 8646f0...c3aea9 )
by Morris
278:02 queued 258:09
created

SettingsController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A addClient() 0 19 1
A deleteClient() 0 7 1
A getClients() 0 17 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
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
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\OAuth2\Controller;
24
25
use OC\Authentication\Token\DefaultTokenMapper;
26
use OCA\OAuth2\Db\AccessTokenMapper;
27
use OCA\OAuth2\Db\Client;
28
use OCA\OAuth2\Db\ClientMapper;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\IRequest;
32
use OCP\Security\ISecureRandom;
33
34
class SettingsController extends Controller {
35
	/** @var ClientMapper */
36
	private $clientMapper;
37
	/** @var ISecureRandom */
38
	private $secureRandom;
39
	/** @var AccessTokenMapper  */
40
	private $accessTokenMapper;
41
	/** @var  DefaultTokenMapper */
42
	private $defaultTokenMapper;
43
44
	const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
45
46
	/**
47
	 * @param string $appName
48
	 * @param IRequest $request
49
	 * @param ClientMapper $clientMapper
50
	 * @param ISecureRandom $secureRandom
51
	 * @param AccessTokenMapper $accessTokenMapper
52
	 * @param DefaultTokenMapper $defaultTokenMapper
53
	 */
54
	public function __construct(string $appName,
55
								IRequest $request,
56
								ClientMapper $clientMapper,
57
								ISecureRandom $secureRandom,
58
								AccessTokenMapper $accessTokenMapper,
59
								DefaultTokenMapper $defaultTokenMapper
60
	) {
61
		parent::__construct($appName, $request);
62
		$this->secureRandom = $secureRandom;
63
		$this->clientMapper = $clientMapper;
64
		$this->accessTokenMapper = $accessTokenMapper;
65
		$this->defaultTokenMapper = $defaultTokenMapper;
66
	}
67
68
	public function addClient(string $name,
69
							  string $redirectUri): JSONResponse {
70
		$client = new Client();
71
		$client->setName($name);
72
		$client->setRedirectUri($redirectUri);
73
		$client->setSecret($this->secureRandom->generate(64, self::validChars));
74
		$client->setClientIdentifier($this->secureRandom->generate(64, self::validChars));
75
		$client = $this->clientMapper->insert($client);
76
77
		$result = [
78
			'id' => $client->getId(),
79
			'name' => $client->getName(),
0 ignored issues
show
Documentation Bug introduced by
The method getName does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
			'redirectUri' => $client->getRedirectUri(),
0 ignored issues
show
Documentation Bug introduced by
The method getRedirectUri does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
			'clientId' => $client->getClientIdentifier(),
0 ignored issues
show
Documentation Bug introduced by
The method getClientIdentifier does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
82
			'clientSecret' => $client->getSecret(),
0 ignored issues
show
Documentation Bug introduced by
The method getSecret does not exist on object<OCP\AppFramework\Db\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
83
		];
84
85
		return new JSONResponse($result);
86
	}
87
88
	public function deleteClient(int $id): JSONResponse {
89
		$client = $this->clientMapper->getByUid($id);
90
		$this->accessTokenMapper->deleteByClientId($id);
91
		$this->defaultTokenMapper->deleteByName($client->getName());
92
		$this->clientMapper->delete($client);
93
		return new JSONResponse([]);
94
	}
95
96
	public function getClients(): JSONResponse {
97
		$clients = $this->clientMapper->getClients();
98
99
		$result = [];
100
101
		foreach ($clients as $client) {
102
			$result[] = [
103
				'id' => $client->getId(),
104
				'name' => $client->getName(),
105
				'redirectUri' => $client->getRedirectUri(),
106
				'clientId' => $client->getClientIdentifier(),
107
				'clientSecret' => $client->getSecret(),
108
			];
109
		}
110
111
		return new JSONResponse($result);
112
	}
113
}
114