1<?php
2
3declare(strict_types=1);
4/**
5 * @author Joas Schilling <coding@schilljs.com>
6 *
7 * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com>
8 *
9 * @license AGPL-3.0
10 *
11 * This code is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License, version 3,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License, version 3,
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22 *
23 */
24
25namespace OCA\TwoFactorNextcloudNotification\Notification;
26
27use OCA\TwoFactorNextcloudNotification\AppInfo\Application;
28use OCP\IURLGenerator;
29use OCP\L10N\IFactory;
30use OCP\Notification\INotification;
31use OCP\Notification\INotifier;
32
33class Notifier implements INotifier {
34
35	/** @var IFactory */
36	protected $l10nFactory;
37
38	/** @var IURLGenerator */
39	protected $urlGenerator;
40
41	public function __construct(IFactory $l10nFactory,
42								IURLGenerator $urlGenerator) {
43		$this->l10nFactory = $l10nFactory;
44		$this->urlGenerator = $urlGenerator;
45	}
46
47	/**
48	 * Identifier of the notifier, only use [a-z0-9_]
49	 *
50	 * @return string
51	 * @since 17.0.0
52	 */
53	public function getID(): string {
54		return Application::APP_ID;
55	}
56
57	/**
58	 * Human readable name describing the notifier
59	 *
60	 * @return string
61	 * @since 17.0.0
62	 */
63	public function getName(): string {
64		return $this->l10nFactory->get(Application::APP_ID)->t('TwoFactor Nextcloud notification');
65	}
66
67	/**
68	 * @param INotification $notification
69	 * @param string $languageCode The code of the language that should be used to prepare the notification
70	 * @return INotification
71	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
72	 */
73	public function prepare(INotification $notification, string $languageCode): INotification {
74		if ($notification->getApp() !== Application::APP_ID ||
75			$notification->getSubject() !== 'login_attempt') {
76			throw new \InvalidArgumentException('Unhandled app or subject');
77		}
78
79		$l = $this->l10nFactory->get(Application::APP_ID, $languageCode);
80		$attemptId = $notification->getObjectId();
81		$param = $notification->getSubjectParameters();
82
83		$approveAction = $notification->createAction()
84			->setParsedLabel($l->t('Approve'))
85			->setPrimary(true)
86			->setLink(
87				$this->urlGenerator->getAbsoluteURL(
88					$this->urlGenerator->linkTo(
89						'',
90						'ocs/v2.php/apps/twofactor_nextcloud_notification/api/v1/attempt/' . $attemptId
91					)
92				),
93				'POST'
94			);
95
96		$disapproveAction = $notification->createAction()
97			->setParsedLabel($l->t('Cancel'))
98			->setPrimary(false)
99			->setLink(
100				$this->urlGenerator->getAbsoluteURL(
101					$this->urlGenerator->linkTo(
102						'',
103						'ocs/v2.php/apps/twofactor_nextcloud_notification/api/v1/attempt/' . $attemptId
104					)
105				),
106				'DELETE'
107			);
108
109		$notification->addParsedAction($approveAction)
110			->addParsedAction($disapproveAction)
111			->setParsedSubject(str_replace('{ip}', $param['ip'], $l->t('Login attempt from {ip}')))
112			->setRichSubject(
113				$l->t('Login attempt from {ip}'),
114				[
115					'ip' => [
116						'type' => 'highlight',
117						'id' => $notification->getObjectId(),
118						'name' => $param['ip'],
119					],
120				])
121			->setParsedMessage($l->t('Please approve or deny the login attempt.'))
122			->setRichMessage($l->t('Please approve or deny the login attempt.'))
123		;
124		return $notification;
125	}
126}
127