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
|
|
|
|
21
|
|
|
abstract class Token implements \JsonSerializable |
22
|
|
|
{ |
23
|
|
|
protected $tokenId; |
24
|
|
|
private $expiresAt; |
25
|
|
|
private $resourceOwnerId; |
26
|
|
|
private $clientId; |
27
|
|
|
private $parameter; |
28
|
|
|
private $metadata; |
29
|
|
|
private $revoked; |
30
|
|
|
private $resourceServerId; |
31
|
|
|
|
32
|
|
|
public function __construct(TokenId $tokenId, ClientId $clientId, ResourceOwnerId $resourceOwnerId, DataBag $parameter, DataBag $metadata, \DateTimeImmutable $expiresAt, ?ResourceServerId $resourceServerId) |
33
|
|
|
{ |
34
|
|
|
$this->tokenId = $tokenId; |
35
|
|
|
$this->resourceOwnerId = $resourceOwnerId; |
36
|
|
|
$this->clientId = $clientId; |
37
|
|
|
$this->parameter = $parameter; |
38
|
|
|
$this->metadata = $metadata; |
39
|
|
|
$this->expiresAt = $expiresAt; |
40
|
|
|
$this->resourceServerId = $resourceServerId; |
41
|
|
|
$this->revoked = false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getTokenId(): TokenId |
45
|
|
|
{ |
46
|
|
|
return $this->tokenId; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getExpiresAt(): \DateTimeImmutable |
50
|
|
|
{ |
51
|
|
|
return $this->expiresAt; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function hasExpired(): bool |
55
|
|
|
{ |
56
|
|
|
return $this->expiresAt->getTimestamp() < \time(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getResourceOwnerId(): ResourceOwnerId |
60
|
|
|
{ |
61
|
|
|
return $this->resourceOwnerId; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getClientId(): ClientId |
65
|
|
|
{ |
66
|
|
|
return $this->clientId; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getParameter(): DataBag |
70
|
|
|
{ |
71
|
|
|
return $this->parameter; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getMetadata(): DataBag |
75
|
|
|
{ |
76
|
|
|
return $this->metadata; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function isRevoked(): bool |
80
|
|
|
{ |
81
|
|
|
return $this->revoked; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function markAsRevoked(): void |
85
|
|
|
{ |
86
|
|
|
$this->revoked = true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getResourceServerId(): ?ResourceServerId |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
return $this->resourceServerId; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getExpiresIn(): int |
95
|
|
|
{ |
96
|
|
|
$expiresAt = $this->expiresAt; |
97
|
|
|
if (null === $expiresAt) { |
98
|
|
|
return 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return (int) ($this->expiresAt->getTimestamp() - \time() < 0 ? 0 : $this->expiresAt->getTimestamp() - \time()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function jsonSerialize() |
105
|
|
|
{ |
106
|
|
|
$data = [ |
107
|
|
|
'expires_at' => $this->getExpiresAt()->getTimestamp(), |
108
|
|
|
'client_id' => $this->getClientId()->getValue(), |
109
|
|
|
'parameters' => (object) $this->getParameter()->all(), |
110
|
|
|
'metadatas' => (object) $this->getMetadata()->all(), |
111
|
|
|
'is_revoked' => $this->isRevoked(), |
112
|
|
|
'resource_owner_id' => $this->getResourceOwnerId()->getValue(), |
113
|
|
|
'resource_owner_class' => \get_class($this->getResourceOwnerId()), |
114
|
|
|
'resource_server_id' => $this->getResourceServerId() ? $this->getResourceServerId()->getValue() : null, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return $data; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.