Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (warning)
81.48%
22 / 27
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionService
81.48% covered (warning)
81.48%
22 / 27
77.78% covered (warning)
77.78%
7 / 9
18.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSignStartTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSessionId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 setIdentifyMethodId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIdentifyMethodId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 resetDurationOfSignPage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setEmailSignatureKey
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getEmailSignatureKey
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 isAuthenticated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service;
10
11use OCP\ISession;
12
13class SessionService {
14    public const NO_RENEWAL_INTERVAL = 0;
15    public const NO_MAXIMUM_VALIDITY = 0;
16
17    public function __construct(
18        protected ISession $session,
19    ) {
20    }
21
22    public function getSignStartTime(): int {
23        return $this->session->get('libresign-sign-start-time') ?? self::NO_RENEWAL_INTERVAL;
24    }
25
26    public function getSessionId(): string {
27        if ($this->isAuthenticated()) {
28            return $this->session->getId();
29        }
30        $uuid = $this->session->get('libresign-uuid');
31        if (is_string($uuid) && $uuid !== '') {
32            // Verification in one browser must not release saved images to another
33            // browser holding the same signing link.
34            return hash('sha256', $uuid . ':' . $this->session->getId());
35        }
36        return $this->session->getId();
37    }
38
39    public function setIdentifyMethodId(int $id): void {
40        $this->session->set('identify_method_id', $id);
41    }
42
43    public function getIdentifyMethodId(): ?int {
44        $id = $this->session->get('identify_method_id');
45        return $id;
46    }
47
48    public function resetDurationOfSignPage(int $renewalInterval): void {
49        if ($renewalInterval <= self::NO_RENEWAL_INTERVAL) {
50            return;
51        }
52        $this->session->reopen();
53        $this->session->set('libresign-sign-start-time', time());
54    }
55
56    public function setEmailSignatureKey(string $key): void {
57        $this->session->reopen();
58        $keys = $this->session->get('libresign-email-signature-keys');
59        if (!is_array($keys)) {
60            $keys = [];
61        }
62        $keys[$this->getSessionId()] = $key;
63        $this->session->set('libresign-email-signature-keys', $keys);
64    }
65
66    public function getEmailSignatureKey(): string {
67        $keys = $this->session->get('libresign-email-signature-keys');
68        if (!is_array($keys)) {
69            return '';
70        }
71        $key = $keys[$this->getSessionId()] ?? null;
72        return is_string($key) ? $key : '';
73    }
74
75    public function isAuthenticated(): bool {
76        return $this->session->get('user_id') ? true : false;
77    }
78}