|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Models\AccessTokens; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Hello\Utility\ModelsHelper; |
|
6
|
|
|
use DateTimeImmutable; |
|
7
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
|
8
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
9
|
|
|
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait; |
|
10
|
|
|
use League\OAuth2\Server\Entities\Traits\EntityTrait; |
|
11
|
|
|
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Token |
|
15
|
|
|
* @package ByTIC\Auth\Models\AccessTokens |
|
16
|
|
|
* |
|
17
|
|
|
* @property int $user_id |
|
18
|
|
|
* @property int $client_id |
|
19
|
|
|
* @property string $revoked |
|
20
|
|
|
* @property string $expires_at |
|
21
|
|
|
*/ |
|
22
|
|
|
class Token extends \Nip\Records\Record implements AccessTokenEntityInterface |
|
23
|
|
|
{ |
|
24
|
1 |
|
use EntityTrait { |
|
25
|
|
|
setIdentifier as setIdentifierTrait; |
|
26
|
|
|
} |
|
27
|
1 |
|
use TokenEntityTrait { |
|
28
|
|
|
TokenEntityTrait::getClient as getClientTrait; |
|
29
|
|
|
TokenEntityTrait::setUserIdentifier as setUserIdentifierTrait; |
|
30
|
|
|
} |
|
31
|
1 |
|
use AccessTokenTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ClientEntityInterface $clientEntity |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function populateFromClient(ClientEntityInterface $clientEntity) |
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->setClient($clientEntity); |
|
39
|
1 |
|
$this->client_id = $clientEntity->getIdentifier(); |
|
|
|
|
|
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritDoc |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function getClient(): ClientEntityInterface |
|
46
|
|
|
{ |
|
47
|
2 |
|
$client = $this->getClientTrait(); |
|
48
|
2 |
|
if ($client instanceof ClientEntityInterface) { |
|
|
|
|
|
|
49
|
1 |
|
return $client; |
|
50
|
|
|
} |
|
51
|
|
|
/** @var ClientEntityInterface $relationClient */ |
|
52
|
1 |
|
$relationClient = $this->getRelation('Client')->getResults(); |
|
53
|
1 |
|
$this->setClient($relationClient); |
|
54
|
1 |
|
return $relationClient; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function setIdentifier($identifier) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->setDataValue('identifier', $identifier); |
|
|
|
|
|
|
63
|
1 |
|
$this->setIdentifierTrait($identifier); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function setExpiresAt($value) |
|
70
|
|
|
{ |
|
71
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
72
|
2 |
|
$date = new DateTimeImmutable($value); |
|
73
|
2 |
|
$this->setExpiryDateTime($date); |
|
74
|
2 |
|
$this->setDataValue('expires_at', $value); |
|
|
|
|
|
|
75
|
2 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $identifier |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function setUserId($identifier) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$this->setUserIdentifier($identifier); |
|
83
|
3 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param int|string|null $identifier |
|
88
|
|
|
*/ |
|
89
|
4 |
|
public function setUserIdentifier($identifier) |
|
90
|
|
|
{ |
|
91
|
4 |
|
$this->setUserIdentifierTrait($identifier); |
|
92
|
4 |
|
$this->setDataValue('user_id', $this->getUserIdentifier()); |
|
|
|
|
|
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $scopes |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function setScopes($scopes) |
|
99
|
|
|
{ |
|
100
|
1 |
|
if (is_string($scopes)) { |
|
101
|
1 |
|
$this->addScopesFromString($scopes); |
|
102
|
1 |
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
$this->scopes = $scopes; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $scopes |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function addScopesFromString($scopes) |
|
111
|
|
|
{ |
|
112
|
1 |
|
$scopes = array_filter(explode(',', $scopes)); |
|
113
|
1 |
|
foreach ($scopes as $key => $scope) { |
|
114
|
|
|
$scopes[$key] = ModelsHelper::scopes()->getScopeEntityByIdentifier($scope); |
|
115
|
|
|
} |
|
116
|
1 |
|
$this->addScopes($scopes); |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $scopes |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function addScopes($scopes) |
|
123
|
|
|
{ |
|
124
|
2 |
|
foreach ($scopes as $scope) { |
|
125
|
|
|
$this->addScope($scope); |
|
126
|
|
|
} |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritDoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function insert() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->castExpireDateTime(); |
|
135
|
|
|
return parent::insert(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritDoc |
|
140
|
|
|
*/ |
|
141
|
|
|
public function update() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->castExpireDateTime(); |
|
144
|
|
|
return parent::insert(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
protected function castExpireDateTime() |
|
148
|
|
|
{ |
|
149
|
|
|
$date = $this->getExpiryDateTime(); |
|
150
|
|
|
$this->expires_at = ($date) ? $date->format('Y-m-d') : ''; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.