IdToken   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpiresAt() 0 7 2
A getAuthorizationCodeHash() 0 3 2
A getExpiresIn() 0 3 2
A getUserAccountId() 0 7 2
A hasExpired() 0 3 1
A getClientId() 0 7 2
A __construct() 0 4 1
A getAccessTokenHash() 0 3 2
A getId() 0 3 1
A getNonce() 0 3 2
A getClaims() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\OpenIdConnect;
15
16
use OAuth2Framework\Component\Core\Client\ClientId;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
18
19
class IdToken
20
{
21
    /**
22
     * @var IdTokenId
23
     */
24
    private $idTokenId;
25
26
    /**
27
     * @var array
28
     */
29
    private $claims;
30
31
    public function __construct(IdTokenId $idTokenId, array $claims)
32
    {
33
        $this->idTokenId = $idTokenId;
34
        $this->claims = $claims;
35
    }
36
37
    public function getId(): IdTokenId
38
    {
39
        return $this->idTokenId;
40
    }
41
42
    public function getNonce(): ?string
43
    {
44
        return \array_key_exists('nonce', $this->claims) ? $this->claims['nonce'] : null;
45
    }
46
47
    public function getAccessTokenHash(): ?string
48
    {
49
        return \array_key_exists('at_hash', $this->claims) ? $this->claims['at_hash'] : null;
50
    }
51
52
    public function getAuthorizationCodeHash(): ?string
53
    {
54
        return \array_key_exists('c_hash', $this->claims) ? $this->claims['c_hash'] : null;
55
    }
56
57
    public function getClientId(): ClientId
58
    {
59
        if (!\array_key_exists('aud', $this->claims)) {
60
            throw new \InvalidArgumentException('Invalid ID Token.');
61
        }
62
63
        return new ClientId($this->claims['aud']);
64
    }
65
66
    public function getUserAccountId(): UserAccountId
67
    {
68
        if (!\array_key_exists('sub', $this->claims)) {
69
            throw new \InvalidArgumentException('Invalid ID Token.');
70
        }
71
72
        return new UserAccountId($this->claims['sub']);
73
    }
74
75
    public function getExpiresAt(): \DateTimeImmutable
76
    {
77
        if (!\array_key_exists('exp', $this->claims)) {
78
            throw new \InvalidArgumentException('Invalid ID Token.');
79
        }
80
81
        return new \DateTimeImmutable((string) $this->claims['exp']);
82
    }
83
84
    public function hasExpired(): bool
85
    {
86
        return $this->getExpiresAt()->getTimestamp() < time();
87
    }
88
89
    public function getExpiresIn(): int
90
    {
91
        return $this->getExpiresAt()->getTimestamp() - time() < 0 ? 0 : $this->getExpiresAt()->getTimestamp() - time();
92
    }
93
94
    public function getClaims(): array
95
    {
96
        return $this->claims;
97
    }
98
}
99