Passed
Push — master ( 4765f5...78c7e6 )
by Roeland
11:41 queued 14s
created

Authtokens   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 40
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getForm() 0 14 1
A __construct() 0 10 1
A getAppTokens() 0 25 4
A getPriority() 0 2 1
A getSection() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Settings\Settings\Personal\Security;
29
30
use OCP\IUserSession;
31
use function array_map;
32
use OC\Authentication\Exceptions\InvalidTokenException;
33
use OC\Authentication\Token\INamedToken;
34
use OC\Authentication\Token\IProvider as IAuthTokenProvider;
35
use OC\Authentication\Token\IToken;
36
use OCP\AppFramework\Http\TemplateResponse;
37
use OCP\IInitialStateService;
38
use OCP\ISession;
39
use OCP\Session\Exceptions\SessionNotAvailableException;
40
use OCP\Settings\ISettings;
41
42
class Authtokens implements ISettings {
43
44
	/** @var IAuthTokenProvider */
45
	private $tokenProvider;
46
47
	/** @var ISession */
48
	private $session;
49
50
	/** @var IInitialStateService */
51
	private $initialStateService;
52
53
	/** @var string|null */
54
	private $uid;
55
56
	/** @var IUserSession */
57
	private $userSession;
58
59
	public function __construct(IAuthTokenProvider $tokenProvider,
60
								ISession $session,
61
								IUserSession $userSession,
62
								IInitialStateService $initialStateService,
63
								?string $UserId) {
64
		$this->tokenProvider = $tokenProvider;
65
		$this->session = $session;
66
		$this->initialStateService = $initialStateService;
67
		$this->uid = $UserId;
68
		$this->userSession = $userSession;
69
	}
70
71
	public function getForm(): TemplateResponse {
72
		$this->initialStateService->provideInitialState(
73
			'settings',
74
			'app_tokens',
75
			$this->getAppTokens()
76
		);
77
78
		$this->initialStateService->provideInitialState(
79
			'settings',
80
			'can_create_app_token',
81
			$this->userSession->getImpersonatingUserID() === null
82
		);
83
84
		return new TemplateResponse('settings', 'settings/personal/security/authtokens');
85
	}
86
87
	public function getSection(): string {
88
		return 'security';
89
	}
90
91
	public function getPriority(): int {
92
		return 100;
93
	}
94
95
	private function getAppTokens(): array {
96
		$tokens = $this->tokenProvider->getTokenByUser($this->uid);
97
98
		try {
99
			$sessionId = $this->session->getId();
100
		} catch (SessionNotAvailableException $ex) {
101
			return [];
102
		}
103
		try {
104
			$sessionToken = $this->tokenProvider->getToken($sessionId);
105
		} catch (InvalidTokenException $ex) {
106
			return [];
107
		}
108
109
		return array_map(function (IToken $token) use ($sessionToken) {
110
			$data = $token->jsonSerialize();
111
			$data['canDelete'] = true;
112
			$data['canRename'] = $token instanceof INamedToken;
113
			if ($sessionToken->getId() === $token->getId()) {
114
				$data['canDelete'] = false;
115
				$data['canRename'] = false;
116
				$data['current'] = true;
117
			}
118
			return $data;
119
		}, $tokens);
120
	}
121
122
}
123