-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAccessTokenEntity.php
More file actions
201 lines (169 loc) · 4.34 KB
/
AccessTokenEntity.php
File metadata and controls
201 lines (169 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @author Ian Simpson <ian@iansimpson.nz>
* @copyright Copyright (c) Ian Simpson
*/
namespace IanSimpson\OAuth2\Entities;
use DateInterval;
use DateTimeImmutable;
use Exception;
use IanSimpson\OAuth2\OauthServerController;
use Lcobucci\JWT\Token;
use League\OAuth2\Server\CryptKey;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
use League\OAuth2\Server\Entities\Traits\EntityTrait;
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\Security\Member;
/**
* @property ?string $Code
* @property int $Expiry
* @property bool $Revoked
* @property int $ClientID
* @property int $MemberID
* @method ClientEntity Client()
* @method Member Member()
* @method ManyManyList|ScopeEntity[] ScopeEntities()
*/
class AccessTokenEntity extends DataObject implements AccessTokenEntityInterface
{
use AccessTokenTrait {
convertToJWT as defaultConvertToJWT;
}
use TokenEntityTrait;
use EntityTrait;
/**
* @config
*/
private static string $table_name = 'OAuth_AccessTokenEntity';
/**
* @var array|string[]
*
* @config
*/
private static array $db = [
'Code' => 'Text',
'Expiry' => 'Datetime',
'Revoked' => 'Boolean',
];
/**
* @var array|string[]
*
* @config
*/
private static array $has_one = [
'Client' => ClientEntity::class,
'Member' => Member::class,
];
/**
* @var array|string[]
*
* @config
*/
private static array $many_many = [
'ScopeEntities' => ScopeEntity::class,
];
/**
*
* @var string[]
*
* @config
*/
private static array $searchable_fields = [
'Code',
];
/**
* TODO: Update to CryptkeyInterface once `league/oauth2-server` is updated to `^9`
*/
public function getPrivateKey(): ?CryptKey
{
return $this->privateKey;
}
/**
* Generate a JWT from the access token
*
* @return Token
*/
public function convertToJWT(): Token
{
$token = null;
// Get token from extension (in case of different implementation than the default)
$this->extend('updateJWT', $token);
if ($token) {
return $token;
}
// Default token generated
return $this->defaultConvertToJWT();
}
public function getIdentifier(): string
{
return (string) $this->Code;
}
/**
* @throws Exception
*/
public function getExpiryDateTime(): DateTimeImmutable
{
return (new DateTimeImmutable())->setTimestamp((int) $this->Expiry)
->add(new DateInterval(OauthServerController::getGrantTypeExpiryInterval()));
}
public function getUserIdentifier(): string
{
return (string) $this->MemberID;
}
public function getScopes(): array
{
return $this->ScopeEntities()->toArray();
}
/**
* @return ClientEntity
*/
public function getClient(): ClientEntityInterface
{
return $this->Client();
}
public function setIdentifier(mixed $code): self
{
$this->Code = (string) $code;
return $this;
}
public function setExpiryDateTime(DateTimeImmutable $expiry): self
{
$this->Expiry = $expiry->getTimestamp();
return $this;
}
public function setUserIdentifier(mixed $id): self
{
$this->MemberID = (int) $id;
return $this;
}
public function addScope(ScopeEntityInterface $scope): self
{
if ($scope instanceof ScopeEntity) {
$this->ScopeEntities()->add($scope);
}
return $this;
}
/**
* @param ScopeEntity[] $scopes
*/
public function setScopes($scopes): self
{
$this->ScopeEntities()->removeAll();
foreach ($scopes as $scope) {
$this->addScope($scope);
}
return $this;
}
public function setClient(ClientEntityInterface $client): self
{
if ($client instanceof ClientEntity) {
$this->ClientID = $client->ID;
}
return $this;
}
}