1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gandung\JWT\Accessor; |
4
|
|
|
|
5
|
|
|
use Gandung\JWT\Validator\Validator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Paulus Gandung Prakosa <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class PayloadAccessor implements PayloadAccessorInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $payload; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Gandung\JWT\ValidatorInterface |
19
|
|
|
*/ |
20
|
|
|
private $validator; |
21
|
|
|
|
22
|
|
|
public function __construct($payload) |
23
|
|
|
{ |
24
|
|
|
$this->payload = $payload; |
25
|
|
|
$this->configureValidator(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getIssuedBy(): string |
32
|
|
|
{ |
33
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
34
|
|
|
$this->payload, |
35
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\IssuedBy |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
return $this->payload[\Gandung\JWT\Token\Claim::ISSUER]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function getRelatedTo(): string |
45
|
|
|
{ |
46
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
47
|
|
|
$this->payload, |
48
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\RelatedTo |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return $this->payload[\Gandung\JWT\Token\Claim::SUBJECT]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getIntendedFor(): string |
58
|
|
|
{ |
59
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
60
|
|
|
$this->payload, |
61
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\IntendedFor |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return $this->payload[\Gandung\JWT\Token\Claim::AUDIENCE]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getExpireAt(): \DateTimeInterface |
71
|
|
|
{ |
72
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
73
|
|
|
$this->payload, |
74
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\ExpirationTime |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$exp = $this->payload[\Gandung\JWT\Token\Claim::EXPIRATION_TIME]; |
78
|
|
|
|
79
|
|
|
return !($exp instanceof \DateTimeImmutable) && is_int($exp) |
80
|
|
|
? new \DateTimeImmutable('@' . (string)$exp) |
81
|
|
|
: $exp; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getCanOnlyBeUsedAfter(): \DateTimeInterface |
88
|
|
|
{ |
89
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
90
|
|
|
$this->payload, |
91
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\NotBefore |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$nbf = $this->payload[\Gandung\JWT\Token\Claim::NOT_BEFORE]; |
95
|
|
|
|
96
|
|
|
return !($nbf instanceof \DateTimeImmutable) && is_int($nbf) |
97
|
|
|
? new \DateTimeImmutable('@' . (string)$nbf) |
98
|
|
|
: $nbf; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getIssuedAt(): \DateTimeInterface |
105
|
|
|
{ |
106
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
107
|
|
|
$this->payload, |
108
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\IssuedAt |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$iat = $this->payload[\Gandung\JWT\Token\Claim::ISSUED_AT]; |
112
|
|
|
|
113
|
|
|
return !($iat instanceof \DateTimeImmutable) && is_int($iat) |
114
|
|
|
? new \DateTimeImmutable('@' . (string)$iat) |
115
|
|
|
: $iat; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getIdentifiedBy(): string |
122
|
|
|
{ |
123
|
|
|
$this->validator->validateFromArray( |
|
|
|
|
124
|
|
|
$this->payload, |
125
|
|
|
new \Gandung\JWT\Validator\Constraints\Payload\JWTID |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
return $this->payload[\Gandung\JWT\Token\Claim::JWT_ID]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function get(): array |
135
|
|
|
{ |
136
|
|
|
return $this->payload; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Configure validator object. |
141
|
|
|
* |
142
|
|
|
* @param array $constraints |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
private function configureValidator($constraints = []) |
146
|
|
|
{ |
147
|
|
|
$this->validator = new Validator; |
148
|
|
|
|
149
|
|
|
foreach ($constraints as $v) { |
150
|
|
|
$this->validator->addConstraint($v); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.