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\ClientRegistrationEndpoint; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\Event as InitialAccessTokenEvent; |
17
|
|
|
use OAuth2Framework\Component\Core\Domain\DomainObject; |
18
|
|
|
use OAuth2Framework\Component\Core\Event\Event; |
19
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
20
|
|
|
|
21
|
|
|
class InitialAccessToken implements DomainObject |
22
|
|
|
{ |
23
|
|
|
private $revoked; |
24
|
|
|
private $initialAccessTokenId; |
25
|
|
|
private $expiresAt; |
26
|
|
|
private $userAccountId; |
27
|
|
|
|
28
|
|
|
public function __construct(InitialAccessTokenId $initialAccessTokenId, UserAccountId $userAccountId, ?\DateTimeImmutable $expiresAt) |
29
|
|
|
{ |
30
|
|
|
$this->initialAccessTokenId = $initialAccessTokenId; |
31
|
|
|
$this->expiresAt = $expiresAt; |
32
|
|
|
$this->userAccountId = $userAccountId; |
33
|
|
|
$this->revoked = false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getTokenId(): InitialAccessTokenId |
37
|
|
|
{ |
38
|
|
|
return $this->initialAccessTokenId; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getUserAccountId(): UserAccountId |
42
|
|
|
{ |
43
|
|
|
return $this->userAccountId; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getExpiresAt(): ?\DateTimeImmutable |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return $this->expiresAt; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function hasExpired(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->expiresAt->getTimestamp() < \time(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isRevoked(): bool |
57
|
|
|
{ |
58
|
|
|
return $this->revoked; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function markAsRevoked(): void |
62
|
|
|
{ |
63
|
|
|
$this->revoked = true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function getSchema(): string |
67
|
|
|
{ |
68
|
|
|
return 'https://oauth2-framework.spomky-labs.com/schemas/model/initial-access-token/1.0/schema'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function jsonSerialize() |
72
|
|
|
{ |
73
|
|
|
$data = [ |
74
|
|
|
'$schema' => $this->getSchema(), |
75
|
|
|
'type' => \get_class($this), |
76
|
|
|
'initial_access_token_id' => $this->getTokenId() ? $this->getTokenId()->getValue() : null, |
77
|
|
|
'user_account_id' => $this->getUserAccountId() ? $this->getUserAccountId()->getValue() : null, |
78
|
|
|
'expires_at' => $this->getExpiresAt() ? $this->getExpiresAt()->getTimestamp() : null, |
79
|
|
|
'is_revoked' => $this->isRevoked(), |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
return $data; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function apply(Event $event): void |
86
|
|
|
{ |
87
|
|
|
$map = $this->getEventMap(); |
88
|
|
|
if (!\array_key_exists($event->getType(), $map)) { |
89
|
|
|
throw new \InvalidArgumentException('Unsupported event.'); |
90
|
|
|
} |
91
|
|
|
if ($this->initialAccessTokenId->getValue() !== $event->getDomainId()->getValue()) { |
92
|
|
|
throw new \InvalidArgumentException('Event not applicable for this initial access token.'); |
93
|
|
|
} |
94
|
|
|
$method = $map[$event->getType()]; |
95
|
|
|
$this->$method($event); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getEventMap(): array |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
InitialAccessTokenEvent\InitialAccessTokenCreatedEvent::class => 'applyInitialAccessTokenCreatedEvent', |
102
|
|
|
InitialAccessTokenEvent\InitialAccessTokenRevokedEvent::class => 'applyInitialAccessTokenRevokedEvent', |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function applyInitialAccessTokenCreatedEvent(InitialAccessTokenEvent\InitialAccessTokenCreatedEvent $event): void |
107
|
|
|
{ |
108
|
|
|
$this->initialAccessTokenId = $event->getInitialAccessTokenId(); |
109
|
|
|
$this->expiresAt = $event->getExpiresAt(); |
110
|
|
|
$this->userAccountId = $event->getUserAccountId(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function applyInitialAccessTokenRevokedEvent(InitialAccessTokenEvent\InitialAccessTokenRevokedEvent $event): void |
114
|
|
|
{ |
115
|
|
|
$this->revoked = true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.