Passed
Push — master ( 50dbde...ac9260 )
by Morris
12:33
created

RemoteWipe::publishActivity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 14
rs 9.8666
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 BadMethodCallException;
29
use OC\Authentication\Exceptions\InvalidTokenException;
30
use OC\Authentication\Exceptions\WipeTokenException;
31
use OCP\Activity\IManager as IActivityManager;
32
use OCP\AppFramework\Utility\ITimeFactory;
33
use OCP\ILogger;
34
use OCP\Notification\IManager as INotificationManager;
35
36
class RemoteWipe {
37
38
	/** @var IProvider */
39
	private $tokenProvider;
40
41
	/** @var IActivityManager */
42
	private $activityManager;
43
44
	/** @var INotificationManager */
45
	private $notificationManager;
46
47
	/** @var ITimeFactory */
48
	private $timeFactory;
49
50
	/** @var ILogger */
51
	private $logger;
52
53
	public function __construct(IProvider $tokenProvider,
54
								IActivityManager $activityManager,
55
								INotificationManager $notificationManager,
56
								ITimeFactory $timeFactory,
57
								ILogger $logger) {
58
		$this->tokenProvider = $tokenProvider;
59
		$this->activityManager = $activityManager;
60
		$this->notificationManager = $notificationManager;
61
		$this->timeFactory = $timeFactory;
62
		$this->logger = $logger;
63
	}
64
65
	/**
66
	 * @param string $token
67
	 *
68
	 * @return bool whether wiping was started
69
	 * @throws InvalidTokenException
70
	 *
71
	 */
72
	public function start(string $token): bool {
73
		try {
74
			$this->tokenProvider->getToken($token);
75
76
			// We expect a WipedTokenException here. If we reach this point this
77
			// is an ordinary token
78
			return false;
79
		} catch (WipeTokenException $e) {
80
			// Expected -> continue below
81
		}
82
83
		$dbToken = $e->getToken();
84
85
		$this->logger->info("user " . $dbToken->getUID() . " started a remote wipe");
86
		$this->sendNotification('remote_wipe_start', $e->getToken());
87
		$this->publishActivity('remote_wipe_start', $e->getToken());
88
89
		return true;
90
	}
91
92
	/**
93
	 * @param string $token
94
	 *
95
	 * @return bool whether wiping could be finished
96
	 * @throws InvalidTokenException
97
	 */
98
	public function finish(string $token): bool {
99
		try {
100
			$this->tokenProvider->getToken($token);
101
102
			// We expect a WipedTokenException here. If we reach this point this
103
			// is an ordinary token
104
			return false;
105
		} catch (WipeTokenException $e) {
106
			// Expected -> continue below
107
		}
108
109
		$dbToken = $e->getToken();
110
111
		$this->tokenProvider->invalidateToken($token);
112
113
		$this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe");
114
		$this->sendNotification('remote_wipe_finish', $e->getToken());
115
		$this->publishActivity('remote_wipe_finish', $e->getToken());
116
117
		return true;
118
	}
119
120
	private function publishActivity(string $event, IToken $token): void {
121
		$activity = $this->activityManager->generateEvent();
122
		$activity->setApp('core')
123
			->setType('security')
124
			->setAuthor($token->getUID())
125
			->setAffectedUser($token->getUID())
126
			->setSubject($event, [
127
				'name' => $token->getName(),
128
			]);
129
		try {
130
			$this->activityManager->publish($activity);
131
		} catch (BadMethodCallException $e) {
132
			$this->logger->warning('could not publish activity', ['app' => 'core']);
133
			$this->logger->logException($e, ['app' => 'core']);
134
		}
135
	}
136
137
	private function sendNotification(string $event, IToken $token): void {
138
		$notification = $this->notificationManager->createNotification();
139
		$notification->setApp('auth')
140
			->setUser($token->getUID())
141
			->setDateTime($this->timeFactory->getDateTime())
142
			->setObject('token', $token->getId())
143
			->setSubject($event, [
144
				'name' => $token->getName(),
145
			]);
146
		$this->notificationManager->notify($notification);
147
	}
148
149
}
150