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\RefreshTokenGrant; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId; |
17
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
18
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
19
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId; |
20
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
21
|
|
|
|
22
|
|
|
abstract class AbstractRefreshToken implements RefreshToken |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var AccessTokenId[] |
26
|
|
|
*/ |
27
|
|
|
private $accessTokenIds = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \DateTimeImmutable |
31
|
|
|
*/ |
32
|
|
|
private $expiresAt; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ResourceOwnerId |
36
|
|
|
*/ |
37
|
|
|
private $resourceOwnerId; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ClientId |
41
|
|
|
*/ |
42
|
|
|
private $clientId; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var DataBag |
46
|
|
|
*/ |
47
|
|
|
private $parameter; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var DataBag |
51
|
|
|
*/ |
52
|
|
|
private $metadata; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
private $revoked; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var ResourceServerId|null |
61
|
|
|
*/ |
62
|
|
|
private $resourceServerId; |
63
|
|
|
|
64
|
|
|
public function __construct(ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId) |
65
|
|
|
{ |
66
|
|
|
$this->resourceOwnerId = $resourceOwnerId; |
67
|
|
|
$this->clientId = $clientId; |
68
|
|
|
$this->parameter = $parameter; |
69
|
|
|
$this->metadata = $metadata; |
70
|
|
|
$this->expiresAt = $expiresAt; |
71
|
|
|
$this->resourceServerId = $resourceServerId; |
72
|
|
|
$this->revoked = false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addAccessToken(AccessTokenId $accessTokenId): void |
76
|
|
|
{ |
77
|
|
|
$id = $accessTokenId->getValue(); |
78
|
|
|
if (!\array_key_exists($id, $this->accessTokenIds)) { |
79
|
|
|
$this->accessTokenIds[$id] = $accessTokenId; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return AccessTokenId[] |
85
|
|
|
*/ |
86
|
|
|
public function getAccessTokenIds(): array |
87
|
|
|
{ |
88
|
|
|
return $this->accessTokenIds; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getResponseData(): array |
92
|
|
|
{ |
93
|
|
|
$data = $this->getParameter(); |
94
|
|
|
$data->set('access_token', $this->getId()->getValue()); |
95
|
|
|
$data->set('expires_in', $this->getExpiresIn()); |
96
|
|
|
$data->set('refresh_token', $this->getId()); |
97
|
|
|
|
98
|
|
|
return $data->all(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getExpiresAt(): \DateTimeImmutable |
102
|
|
|
{ |
103
|
|
|
return $this->expiresAt; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function hasExpired(): bool |
107
|
|
|
{ |
108
|
|
|
return $this->expiresAt->getTimestamp() < \time(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getResourceOwnerId(): ResourceOwnerId |
112
|
|
|
{ |
113
|
|
|
return $this->resourceOwnerId; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getClientId(): ClientId |
117
|
|
|
{ |
118
|
|
|
return $this->clientId; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getParameter(): DataBag |
122
|
|
|
{ |
123
|
|
|
return $this->parameter; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getMetadata(): DataBag |
127
|
|
|
{ |
128
|
|
|
return $this->metadata; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function isRevoked(): bool |
132
|
|
|
{ |
133
|
|
|
return $this->revoked; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function markAsRevoked(): void |
137
|
|
|
{ |
138
|
|
|
$this->revoked = true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getResourceServerId(): ?ResourceServerId |
142
|
|
|
{ |
143
|
|
|
return $this->resourceServerId; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getExpiresIn(): int |
147
|
|
|
{ |
148
|
|
|
return $this->expiresAt->getTimestamp() - \time() < 0 ? 0 : $this->expiresAt->getTimestamp() - \time(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function jsonSerialize() |
152
|
|
|
{ |
153
|
|
|
$data = [ |
154
|
|
|
'refresh_token_id' => $this->getId()->getValue(), |
155
|
|
|
'access_token_ids' => \array_keys($this->getAccessTokenIds()), |
156
|
|
|
'resource_server_id' => null !== $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null, |
157
|
|
|
'expires_at' => $this->getExpiresAt()->getTimestamp(), |
158
|
|
|
'client_id' => $this->getClientId()->getValue(), |
159
|
|
|
'parameters' => (object) $this->getParameter()->all(), |
160
|
|
|
'metadatas' => (object) $this->getMetadata()->all(), |
161
|
|
|
'is_revoked' => $this->isRevoked(), |
162
|
|
|
'resource_owner_id' => $this->getResourceOwnerId()->getValue(), |
163
|
|
|
'resource_owner_class' => \get_class($this->getResourceOwnerId()), |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
return $data; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|