1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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\Core\Token; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
17
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
18
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId; |
19
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
20
|
|
|
use OAuth2Framework\Component\Core\Domain\DomainObject; |
21
|
|
|
use SimpleBus\Message\Recorder\ContainsRecordedMessages; |
22
|
|
|
use SimpleBus\Message\Recorder\PrivateMessageRecorderCapabilities; |
23
|
|
|
|
24
|
|
|
abstract class Token implements \JsonSerializable, ContainsRecordedMessages, DomainObject |
25
|
|
|
{ |
26
|
|
|
use PrivateMessageRecorderCapabilities; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \DateTimeImmutable|null |
30
|
|
|
*/ |
31
|
|
|
protected $expiresAt = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ResourceOwnerId|null |
35
|
|
|
*/ |
36
|
|
|
protected $resourceOwnerId = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ClientId|null |
40
|
|
|
*/ |
41
|
|
|
protected $clientId = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var DataBag |
45
|
|
|
*/ |
46
|
|
|
protected $parameters; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var DataBag |
50
|
|
|
*/ |
51
|
|
|
protected $metadatas; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
protected $revoked = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var null|ResourceServerId |
60
|
|
|
*/ |
61
|
|
|
protected $resourceServerId = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Token constructor. |
65
|
|
|
*/ |
66
|
|
|
protected function __construct() |
67
|
|
|
{ |
68
|
|
|
$this->parameters = DataBag::create([]); |
69
|
|
|
$this->metadatas = DataBag::create([]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return TokenId |
74
|
|
|
*/ |
75
|
|
|
abstract public function getTokenId(): TokenId; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return ResourceOwnerId |
79
|
|
|
*/ |
80
|
|
|
public function getResourceOwnerId(): ResourceOwnerId |
81
|
|
|
{ |
82
|
|
|
return $this->resourceOwnerId; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return \DateTimeImmutable |
87
|
|
|
*/ |
88
|
|
|
public function getExpiresAt(): \DateTimeImmutable |
89
|
|
|
{ |
90
|
|
|
return $this->expiresAt; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function hasExpired(): bool |
97
|
|
|
{ |
98
|
|
|
return $this->expiresAt->getTimestamp() < time(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getExpiresIn(): int |
105
|
|
|
{ |
106
|
|
|
$expiresAt = $this->expiresAt; |
107
|
|
|
if (null === $expiresAt) { |
108
|
|
|
return 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->expiresAt->getTimestamp() - time() < 0 ? 0 : $this->expiresAt->getTimestamp() - time(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return ClientId |
116
|
|
|
*/ |
117
|
|
|
public function getClientId(): ClientId |
118
|
|
|
{ |
119
|
|
|
return $this->clientId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return DataBag |
124
|
|
|
*/ |
125
|
|
|
public function getParameter(): DataBag |
126
|
|
|
{ |
127
|
|
|
return $this->parameters; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function isRevoked(): bool |
134
|
|
|
{ |
135
|
|
|
return $this->revoked; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return DataBag |
140
|
|
|
*/ |
141
|
|
|
public function getMetadata(): DataBag |
142
|
|
|
{ |
143
|
|
|
return $this->metadatas; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return null|ResourceServerId |
148
|
|
|
*/ |
149
|
|
|
public function getResourceServerId(): ? ResourceServerId |
150
|
|
|
{ |
151
|
|
|
return $this->resourceServerId; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function jsonSerialize() |
158
|
|
|
{ |
159
|
|
|
$data = [ |
160
|
|
|
'$schema' => $this->getSchema(), |
161
|
|
|
'type' => get_class($this), |
162
|
|
|
'expires_at' => $this->getExpiresAt()->getTimestamp(), |
163
|
|
|
'client_id' => $this->getClientId()->getValue(), |
164
|
|
|
'parameters' => (object) $this->getParameter()->all(), |
165
|
|
|
'metadatas' => (object) $this->getMetadata()->all(), |
166
|
|
|
'is_revoked' => $this->isRevoked(), |
167
|
|
|
'resource_owner_id' => $this->getResourceOwnerId()->getValue(), |
168
|
|
|
'resource_owner_class' => get_class($this->getResourceOwnerId()), |
169
|
|
|
'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null, |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
return $data; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|