Passed
Push — master ( 22de68...e9bf19 )
by Roeland
10:44 queued 10s
created

RemoteWipe::markAllTokensForWipe()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2019 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2019 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\Authentication\Token;
27
28
use function array_filter;
29
use OC\Authentication\Events\RemoteWipeFinished;
30
use OC\Authentication\Events\RemoteWipeStarted;
31
use OC\Authentication\Exceptions\ExpiredTokenException;
32
use OC\Authentication\Exceptions\InvalidTokenException;
33
use OC\Authentication\Exceptions\WipeTokenException;
34
use OCP\EventDispatcher\IEventDispatcher;
35
use OCP\ILogger;
36
use OCP\IUser;
37
38
class RemoteWipe {
39
40
	/** @var IProvider */
41
	private $tokenProvider;
42
43
	/** @var IEventDispatcher */
44
	private $eventDispatcher;
45
46
	/** @var ILogger */
47
	private $logger;
48
49
	public function __construct(IProvider $tokenProvider,
50
								IEventDispatcher $eventDispatcher,
51
								ILogger $logger) {
52
		$this->tokenProvider = $tokenProvider;
53
		$this->eventDispatcher = $eventDispatcher;
54
		$this->logger = $logger;
55
	}
56
57
	/**
58
	 * @param int $id
59
	 *
60
	 * @return bool
61
	 *
62
	 * @throws InvalidTokenException
63
	 * @throws WipeTokenException
64
	 * @throws ExpiredTokenException
65
	 */
66
	public function markTokenForWipe(int $id): bool {
67
		$token = $this->tokenProvider->getTokenById($id);
68
69
		if (!($token instanceof IWipeableToken)) {
70
			return false;
71
		}
72
73
		$token->wipe();
74
		$this->tokenProvider->updateToken($token);
75
76
		return true;
77
	}
78
79
	/**
80
	 * @param IUser $user
81
	 *
82
	 * @return bool true if any tokens have been marked for remote wipe
83
	 */
84
	public function markAllTokensForWipe(IUser $user): bool {
85
		$tokens = $this->tokenProvider->getTokenByUser($user->getUID());
86
87
		/** @var IWipeableToken[] $wipeable */
88
		$wipeable = array_filter($tokens, function (IToken $token) {
89
			return $token instanceof IWipeableToken;
90
		});
91
92
		if (empty($wipeable)) {
93
			return false;
94
		}
95
96
		foreach ($wipeable as $token) {
97
			$token->wipe();
98
			$this->tokenProvider->updateToken($token);
99
		}
100
101
		return true;
102
	}
103
104
	/**
105
	 * @param string $token
106
	 *
107
	 * @return bool whether wiping was started
108
	 * @throws InvalidTokenException
109
	 *
110
	 */
111
	public function start(string $token): bool {
112
		try {
113
			$this->tokenProvider->getToken($token);
114
115
			// We expect a WipedTokenException here. If we reach this point this
116
			// is an ordinary token
117
			return false;
118
		} catch (WipeTokenException $e) {
119
			// Expected -> continue below
120
		}
121
122
		$dbToken = $e->getToken();
123
124
		$this->logger->info("user " . $dbToken->getUID() . " started a remote wipe");
125
126
		$this->eventDispatcher->dispatch(RemoteWipeStarted::class, new RemoteWipeStarted($dbToken));
127
128
		return true;
129
	}
130
131
	/**
132
	 * @param string $token
133
	 *
134
	 * @return bool whether wiping could be finished
135
	 * @throws InvalidTokenException
136
	 */
137
	public function finish(string $token): bool {
138
		try {
139
			$this->tokenProvider->getToken($token);
140
141
			// We expect a WipedTokenException here. If we reach this point this
142
			// is an ordinary token
143
			return false;
144
		} catch (WipeTokenException $e) {
145
			// Expected -> continue below
146
		}
147
148
		$dbToken = $e->getToken();
149
150
		$this->tokenProvider->invalidateToken($token);
151
152
		$this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe");
153
		$this->eventDispatcher->dispatch(RemoteWipeFinished::class, new RemoteWipeFinished($dbToken));
154
155
		return true;
156
	}
157
158
}
159