1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Models\AccessTokens; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
7
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
8
|
|
|
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait; |
9
|
|
|
use League\OAuth2\Server\Entities\Traits\EntityTrait; |
10
|
|
|
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Token |
14
|
|
|
* @package ByTIC\Auth\Models\AccessTokens |
15
|
|
|
* |
16
|
|
|
* @property int $user_id |
17
|
|
|
* @property int $client_id |
18
|
|
|
* @property string $revoked |
19
|
|
|
* @property string $expires_at |
20
|
|
|
*/ |
21
|
|
|
class Token extends \Nip\Records\Record implements AccessTokenEntityInterface |
22
|
|
|
{ |
23
|
|
|
use EntityTrait { |
24
|
|
|
setIdentifier as setIdentifierTrait; |
25
|
|
|
} |
26
|
|
|
use TokenEntityTrait { |
27
|
|
|
getClient as getClientTrait; |
28
|
|
|
setUserIdentifier as setUserIdentifierTrait; |
29
|
|
|
} |
30
|
|
|
use AccessTokenTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ClientEntityInterface $clientEntity |
34
|
|
|
*/ |
35
|
1 |
|
public function populateFromClient(ClientEntityInterface $clientEntity) |
36
|
|
|
{ |
37
|
1 |
|
$this->setClient($clientEntity); |
38
|
1 |
|
$this->client_id = $clientEntity->getIdentifier(); |
|
|
|
|
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
2 |
|
public function getClient() |
45
|
|
|
{ |
46
|
2 |
|
$client = $this->getClientTrait(); |
47
|
2 |
|
if ($client instanceof ClientEntityInterface) { |
|
|
|
|
48
|
1 |
|
return $client; |
49
|
|
|
} |
50
|
|
|
/** @var ClientEntityInterface $relationClient */ |
51
|
1 |
|
$relationClient = $this->getRelation('Client')->getResults(); |
52
|
1 |
|
$this->setClient($relationClient); |
53
|
1 |
|
return $relationClient; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
1 |
|
public function setIdentifier($value) |
60
|
|
|
{ |
61
|
1 |
|
$this->setDataValue('identifier', $value); |
62
|
1 |
|
$this->setIdentifierTrait($value); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
2 |
|
public function setExpiresAt($value) |
69
|
|
|
{ |
70
|
2 |
|
$date = new DateTimeImmutable($value); |
71
|
2 |
|
$this->setExpiryDateTime($date); |
72
|
2 |
|
$this->setDataValue('expires_at', $value); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $identifier |
77
|
|
|
*/ |
78
|
2 |
|
public function setUserId($identifier) |
79
|
|
|
{ |
80
|
2 |
|
$this->setUserIdentifier($identifier); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int|string|null $identifier |
86
|
|
|
*/ |
87
|
3 |
|
public function setUserIdentifier($identifier) |
88
|
|
|
{ |
89
|
3 |
|
$this->setUserIdentifierTrait($identifier); |
90
|
3 |
|
$this->setDataValue('user_id', $this->getUserIdentifier()); |
91
|
3 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $scopes |
95
|
|
|
*/ |
96
|
1 |
|
public function addScopes($scopes) |
97
|
|
|
{ |
98
|
1 |
|
foreach ($scopes as $scope) { |
99
|
|
|
$this->addScope($scope); |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
|
|
public function insert() |
107
|
|
|
{ |
108
|
|
|
$this->castExpireDateTime(); |
109
|
|
|
return parent::insert(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
|
|
public function update() |
116
|
|
|
{ |
117
|
|
|
$this->castExpireDateTime(); |
118
|
|
|
return parent::insert(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function castExpireDateTime() |
122
|
|
|
{ |
123
|
|
|
$date = $this->getExpiryDateTime(); |
124
|
|
|
$this->expires_at = ($date) ? $date->format('Y-m-d') : ''; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.