|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Models\AccessTokens; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Hello\Models\Clients\Client; |
|
6
|
|
|
use ByTIC\Hello\Models\Clients\Clients; |
|
7
|
|
|
use ByTIC\Hello\Models\Users\Traits\UserTrait; |
|
8
|
|
|
use Carbon\Carbon; |
|
9
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
|
10
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
11
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
|
12
|
|
|
use League\OAuth2\Server\Entities\TokenInterface; |
|
13
|
|
|
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException; |
|
14
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
|
15
|
|
|
use Nip\Records\Collections\Collection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Tokens |
|
19
|
|
|
* @package ByTIC\Auth\Models\Tokens |
|
20
|
|
|
* |
|
21
|
|
|
* @method Token getNew() |
|
22
|
|
|
*/ |
|
23
|
|
|
class Tokens extends \Nip\Records\RecordManager implements AccessTokenRepositoryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Create a new access token |
|
27
|
|
|
* |
|
28
|
|
|
* @param ClientEntityInterface $clientEntity |
|
29
|
|
|
* @param ScopeEntityInterface[] $scopes |
|
30
|
|
|
* @param mixed $userIdentifier |
|
31
|
|
|
* |
|
32
|
|
|
* @return AccessTokenEntityInterface|Token |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$token = $this->getNew(); |
|
37
|
1 |
|
$token->populateFromClient($clientEntity); |
|
38
|
1 |
|
$token->setUserIdentifier($userIdentifier); |
|
39
|
1 |
|
$token->addScopes($scopes); |
|
40
|
1 |
|
return $token; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Persists a new access token to permanent storage. |
|
45
|
|
|
* |
|
46
|
|
|
* @param AccessTokenEntityInterface|Token $accessTokenEntity |
|
47
|
|
|
* |
|
48
|
|
|
* @throws UniqueTokenIdentifierConstraintViolationException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
|
51
|
|
|
{ |
|
52
|
|
|
$accessTokenEntity->save(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Revoke an access token. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $tokenId |
|
59
|
|
|
*/ |
|
60
|
|
|
public function revokeAccessToken($tokenId) |
|
61
|
|
|
{ |
|
62
|
|
|
$token = $this->getByIdentifier($tokenId); |
|
63
|
|
|
if (!($token instanceof Token)) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
$token->revoked = true; |
|
67
|
|
|
$token->save(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if the access token has been revoked. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $tokenId |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool Return true if this token has been revoked |
|
76
|
|
|
*/ |
|
77
|
|
|
public function isAccessTokenRevoked($tokenId) |
|
78
|
|
|
{ |
|
79
|
|
|
$token = $this->getByIdentifier($tokenId); |
|
80
|
|
|
if ($token instanceof TokenInterface) { |
|
81
|
|
|
return $token->revoked; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param UserTrait $user |
|
|
|
|
|
|
88
|
|
|
* @param Client $client |
|
89
|
|
|
* @return Collection|Token[] |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getValidUserTokens($user, $client) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$params = []; |
|
94
|
1 |
|
$params['where'] = [ |
|
95
|
1 |
|
['user_id = ?', $user->getIdentifier()], |
|
96
|
1 |
|
['client_id = ?', $client->getIdentifier()], |
|
97
|
|
|
['revoked = ?', 0], |
|
98
|
1 |
|
['expires_at > ?', Carbon::now()->toDateString()], |
|
99
|
|
|
]; |
|
100
|
1 |
|
return $this->findByParams($params); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $tokenId |
|
105
|
|
|
* @return Token|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getByIdentifier($tokenId) |
|
108
|
|
|
{ |
|
109
|
|
|
$collection = $this->findByField('identifier', $tokenId); |
|
110
|
|
|
return $collection->current(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
protected function initRelations() |
|
114
|
|
|
{ |
|
115
|
1 |
|
parent::initRelations(); |
|
116
|
1 |
|
$this->belongsTo('Client', ['class' => Clients::class, 'fk' => 'client_id', 'withPK' => 'identifier']); |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
120
|
|
|
* @inheritDoc |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function generateTable() |
|
123
|
|
|
{ |
|
124
|
|
|
return 'oauth_access_tokens'; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|