1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\components\repositories; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
6
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
7
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\base\Oauth2BaseTokenRepository; |
8
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\traits\Oauth2ModelRepositoryTrait; |
9
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\DiHelper; |
10
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2AccessTokenRepositoryInterface; |
11
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2IdentifierInterface; |
12
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2UserIdentifierInterface; |
13
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface; |
14
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
15
|
|
|
use yii\base\InvalidConfigException; |
16
|
|
|
use yii\db\Connection; |
17
|
|
|
|
18
|
|
|
class Oauth2AccessTokenRepository extends Oauth2BaseTokenRepository implements Oauth2AccessTokenRepositoryInterface |
19
|
|
|
{ |
20
|
|
|
use Oauth2ModelRepositoryTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $_revocationValidation = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
* @return class-string<Oauth2AccessTokenInterface> |
|
|
|
|
30
|
|
|
*/ |
31
|
5 |
|
public function getModelClass() |
32
|
|
|
{ |
33
|
5 |
|
return Oauth2AccessTokenInterface::class; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
* @param Oauth2ClientInterface $clientEntity |
39
|
|
|
* @throws InvalidConfigException |
40
|
|
|
*/ |
41
|
3 |
|
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
42
|
|
|
{ |
43
|
|
|
/** @var Oauth2AccessTokenInterface $accessToken */ |
44
|
3 |
|
$accessToken = static::getNewTokenInternally([ |
|
|
|
|
45
|
3 |
|
'client' => $clientEntity, |
46
|
3 |
|
'userIdentifier' => $userIdentifier, |
47
|
3 |
|
]); |
48
|
|
|
|
49
|
3 |
|
$accessToken->setScopes($scopes); |
50
|
|
|
|
51
|
3 |
|
return $accessToken; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
* @throws \yii\db\Exception |
57
|
|
|
* @throws InvalidConfigException |
58
|
|
|
*/ |
59
|
2 |
|
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
60
|
|
|
{ |
61
|
2 |
|
static::persistToken($accessTokenEntity); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
1 |
|
public function revokeAccessToken($tokenIdentifier) |
68
|
|
|
{ |
69
|
1 |
|
static::revokeToken($tokenIdentifier); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
2 |
|
public function isAccessTokenRevoked($tokenIdentifier) |
76
|
|
|
{ |
77
|
2 |
|
$validation = $this->getRevocationValidation(); |
78
|
2 |
|
if ($validation === true) { |
79
|
1 |
|
return static::isTokenRevoked($tokenIdentifier); |
|
|
|
|
80
|
2 |
|
} elseif ($validation === false) { |
|
|
|
|
81
|
1 |
|
return false; |
82
|
2 |
|
} elseif (is_callable($validation)) { |
83
|
1 |
|
return call_user_func($validation, $tokenIdentifier); |
84
|
|
|
} else { |
85
|
1 |
|
throw new InvalidConfigException('Access Token Revocation Validation must be a boolean or callable'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
2 |
|
public function getRevocationValidation() |
93
|
|
|
{ |
94
|
2 |
|
return $this->_revocationValidation; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
1 |
|
public function setRevocationValidation($validation) |
101
|
|
|
{ |
102
|
1 |
|
$this->_revocationValidation = $validation; |
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function revokeAccessTokensByUserId($userId) |
110
|
|
|
{ |
111
|
|
|
$class = $this->getModelClass(); |
112
|
|
|
/** @var class-string<Oauth2AccessTokenInterface> $className */ |
113
|
|
|
$className = DiHelper::getValidatedClassName($class); |
114
|
|
|
|
115
|
|
|
$db = $className::getDb(); |
116
|
|
|
|
117
|
|
|
$transaction = $db->beginTransaction(); |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
/** @var Oauth2AccessTokenInterface[] $accessTokens */ |
121
|
|
|
$accessTokens = $className::findAllByUserId($userId); |
122
|
|
|
foreach ($accessTokens as $accessToken) { |
123
|
|
|
$accessToken->setRevokedStatus(true); |
124
|
|
|
$accessToken->persist(); |
125
|
|
|
} |
126
|
|
|
$transaction->commit(); |
127
|
|
|
} catch (\Exception $e) { |
128
|
|
|
$transaction->rollBack(); |
129
|
|
|
throw $e; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $accessTokens; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|