CookieValue   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 82
rs 10
c 1
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentityId() 0 3 1
A from() 0 9 1
A authenticationTime() 0 10 3
A secondFactorId() 0 3 1
A deserialize() 0 10 1
A getLoa() 0 3 1
A meetsRequiredLoa() 0 3 1
A serialize() 0 7 1
A issuedTo() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2022 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject;
20
21
use DateTime;
22
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidAuthenticationTimeException;
23
use function strtolower;
24
use function strtotime;
25
26
class CookieValue implements CookieValueInterface
27
{
28
    private $tokenId;
29
    private $identityId;
30
    private $loa;
31
    private $authenticationTime;
32
33
    /**
34
     * The cookie value consists of:
35
     * - Token used: SecondFactorId from SecondFactor
36
     * - Identifier: IdentityId from SecondFactor
37
     * - The resolved LoA: LoA (resolved using Loa resolution service)
38
     * - Authentication time (Atom formatted date time string)
39
     */
40
    public static function from(string $identityId, string $secondFactorId, float $loa): self
41
    {
42
        $cookieValue = new self;
43
        $cookieValue->tokenId = $secondFactorId;
44
        $cookieValue->identityId = $identityId;
45
        $cookieValue->loa = $loa;
46
        $dateTime = new DateTime();
47
        $cookieValue->authenticationTime = $dateTime->format(DATE_RFC3339_EXTENDED);
48
        return $cookieValue;
49
    }
50
51
    public static function deserialize(string $serializedData): CookieValueInterface
52
    {
53
        $data = json_decode($serializedData, true);
54
        $cookieValue = new self;
55
        $cookieValue->tokenId = $data['tokenId'];
56
        $cookieValue->identityId = $data['identityId'];
57
        $cookieValue->loa = (float) $data['loa'];
58
        $cookieValue->authenticationTime = $data['authenticationTime'];
59
60
        return $cookieValue;
61
    }
62
63
    public function serialize(): string
64
    {
65
        return json_encode([
66
            'tokenId' => $this->tokenId,
67
            'identityId' => $this->identityId,
68
            'loa' => $this->loa,
69
            'authenticationTime' => $this->authenticationTime,
70
        ]);
71
    }
72
73
    public function meetsRequiredLoa(float $requiredLoa): bool
74
    {
75
        return $this->loa >= $requiredLoa;
76
    }
77
78
    public function getLoa(): float
79
    {
80
        return $this->loa;
81
    }
82
83
    public function getIdentityId(): string
84
    {
85
        return $this->identityId;
86
    }
87
88
    public function secondFactorId(): string
89
    {
90
        return $this->tokenId;
91
    }
92
93
    public function issuedTo(string $identityNameId): bool
94
    {
95
        return strtolower($identityNameId) === strtolower($this->identityId);
96
    }
97
98
    public function authenticationTime(): int
99
    {
100
        $dateTime = DateTime::createFromFormat(DATE_RFC3339_EXTENDED, $this->authenticationTime);
101
        if (!$dateTime) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
102
            $dateTime = DateTime::createFromFormat(DATE_RFC3339, $this->authenticationTime);
103
        }
104
        if (!$dateTime) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
105
            throw new InvalidAuthenticationTimeException();
106
        }
107
        return $dateTime->getTimestamp();
108
    }
109
}
110