|
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\AccessToken; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\AccessToken\Event as AccessTokenEvent; |
|
17
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
|
18
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
|
19
|
|
|
use OAuth2Framework\Component\Core\Event\Event; |
|
20
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId; |
|
21
|
|
|
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId; |
|
22
|
|
|
use OAuth2Framework\Component\Core\Token\Token; |
|
23
|
|
|
use OAuth2Framework\Component\Core\Token\TokenId; |
|
24
|
|
|
|
|
25
|
|
|
class AccessToken extends Token |
|
26
|
|
|
{ |
|
27
|
|
|
public function __construct(AccessTokenId $accessTokenId, ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($accessTokenId, $clientId, $resourceOwnerId, $parameter, $metadata, $expiresAt, $resourceServerId); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function getSchema(): string |
|
36
|
|
|
{ |
|
37
|
|
|
return 'https://oauth2-framework.spomky-labs.com/schemas/model/access-token/1.0/schema'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setTokenId(TokenId $tokenId): void |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$tokenId instanceof AccessTokenId) { |
|
43
|
|
|
throw new \RuntimeException('The token ID must be an Access Token ID.'); |
|
44
|
|
|
} |
|
45
|
|
|
parent::setTokenId($tokenId); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function jsonSerialize() |
|
52
|
|
|
{ |
|
53
|
|
|
$data = parent::jsonSerialize() + [ |
|
54
|
|
|
'access_token_id' => $this->getTokenId()->getValue(), |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
return $data; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getResponseData(): array |
|
61
|
|
|
{ |
|
62
|
|
|
$data = $this->getParameter()->all(); |
|
63
|
|
|
$data['access_token'] = $this->getTokenId()->getValue(); |
|
64
|
|
|
$data['expires_in'] = $this->getExpiresIn(); |
|
65
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function apply(Event $event): void |
|
70
|
|
|
{ |
|
71
|
|
|
$map = $this->getEventMap(); |
|
72
|
|
|
if (!\array_key_exists($event->getType(), $map)) { |
|
73
|
|
|
throw new \InvalidArgumentException('Unsupported event.'); |
|
74
|
|
|
} |
|
75
|
|
|
if ($this->getTokenId()->getValue() !== $event->getDomainId()->getValue()) { |
|
76
|
|
|
throw new \InvalidArgumentException('Event not applicable for this access token.'); |
|
77
|
|
|
} |
|
78
|
|
|
$method = $map[$event->getType()]; |
|
79
|
|
|
$this->$method($event); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function getEventMap(): array |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
|
|
AccessTokenEvent\AccessTokenCreatedEvent::class => 'applyAccessTokenCreatedEvent', |
|
86
|
|
|
AccessTokenEvent\AccessTokenRevokedEvent::class => 'applyAccessTokenRevokedEvent', |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function applyAccessTokenCreatedEvent(AccessTokenEvent\AccessTokenCreatedEvent $event): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->setTokenId($event->getAccessTokenId()); |
|
93
|
|
|
$this->setResourceOwnerId($event->getResourceOwnerId()); |
|
94
|
|
|
$this->setClientId($event->getClientId()); |
|
95
|
|
|
$this->setParameter($event->getParameter()); |
|
96
|
|
|
$this->setMetadata($event->getMetadata()); |
|
97
|
|
|
$this->setExpiresAt($event->getExpiresAt()); |
|
98
|
|
|
$this->setResourceServerId($event->getResourceServerId()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function applyAccessTokenRevokedEvent(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->markAsRevoked(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|