Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.24% covered (danger)
17.24%
5 / 29
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TwofactorGateway
17.24% covered (danger)
17.24%
5 / 29
50.00% covered (danger)
50.00%
3 / 6
34.77
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validateToIdentify
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 validateToSign
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 isTwofactorGatewayEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGatewayName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSettings
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\IdentifyMethod;
10
11use OCA\Libresign\Db\FileElementMapper;
12use OCA\Libresign\Db\IdentifyMethodMapper;
13use OCA\Libresign\Service\IdentifyMethodService;
14use OCA\Libresign\Service\SessionService;
15use OCA\Libresign\Service\TwofactorGatewayService;
16use OCP\AppFramework\Utility\ITimeFactory;
17use OCP\Files\IRootFolder;
18use OCP\IUserSession;
19
20class TwofactorGateway extends AbstractIdentifyMethod {
21    public function __construct(
22        protected IdentifyService $identifyService,
23        private IdentifyMethodMapper $identifyMethodMapper,
24        private IRootFolder $root,
25        private ITimeFactory $timeFactory,
26        private SessionService $sessionService,
27        private FileElementMapper $fileElementMapper,
28        private IUserSession $userSession,
29        private TwofactorGatewayService $twofactorGatewayService,
30    ) {
31        parent::__construct(
32            $identifyService,
33        );
34    }
35
36    #[\Override]
37    public function validateToIdentify(): void {
38        $this->throwIfMaximumValidityExpired();
39        $this->throwIfRenewalIntervalExpired();
40        $this->throwIfFileNotFound();
41        $this->throwIfAlreadySigned();
42        $this->renewSession();
43        $this->updateIdentifiedAt();
44    }
45
46    #[\Override]
47    public function validateToSign(): void {
48        $this->throwIfInvalidToken();
49        $this->throwIfMaximumValidityExpired();
50        $this->throwIfRenewalIntervalExpired();
51        $this->throwIfFileNotFound();
52        $this->throwIfAlreadySigned();
53        $this->renewSession();
54        $this->updateIdentifiedAt();
55    }
56
57    public function isTwofactorGatewayEnabled(): bool {
58        return $this->twofactorGatewayService->isGatewayComplete($this->getGatewayName());
59    }
60
61    private function getGatewayName(): string {
62        return IdentifyMethodService::resolveTwofactorGatewayName($this->getId());
63    }
64
65    #[\Override]
66    public function getSettings(): array {
67        if (!empty($this->settings)) {
68            return $this->settings;
69        }
70        $this->settings = parent::getSettingsFromDatabase(
71            default: [
72                'enabled' => false,
73            ],
74            immutable: [
75                'test_url' => $this->identifyService->getUrlGenerator()->linkToRoute('settings.PersonalSettings.index', ['section' => 'security']),
76            ]
77        );
78        return $this->settings;
79    }
80}