1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\oauth2server\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\db\{ActiveQuery, ActiveRecord, IntegrityException}; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is the model class for table "oauth_clients". |
10
|
|
|
* |
11
|
|
|
* @property string $client_id |
12
|
|
|
* @property string $client_secret |
13
|
|
|
* @property string $redirect_uri |
14
|
|
|
* @property string $grant_types |
15
|
|
|
* @property string $scope |
16
|
|
|
* @property integer $user_id |
17
|
|
|
* |
18
|
|
|
* @property OauthAccessTokens[] $oauthAccessTokens |
19
|
|
|
* @property OauthAuthorizationCodes[] $oauthAuthorizationCodes |
20
|
|
|
* @property OauthRefreshTokens[] $oauthRefreshTokens |
21
|
|
|
*/ |
22
|
|
|
class OauthClients extends ActiveRecord |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
2 |
|
public static function tableName(): string |
28
|
|
|
{ |
29
|
2 |
|
return '{{%oauth_clients}}'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function rules(): array |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
[ |
39
|
|
|
['client_id', 'client_secret', 'redirect_uri', 'grant_types'], |
40
|
|
|
'required', |
41
|
|
|
], |
42
|
|
|
[['user_id'], 'integer'], |
43
|
|
|
[['client_id', 'client_secret'], 'string', 'max' => 32], |
44
|
|
|
[['redirect_uri'], 'string', 'max' => 1000], |
45
|
|
|
[['grant_types'], 'string', 'max' => 100], |
46
|
|
|
[['scope'], 'string', 'max' => 2000] |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function attributeLabels(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'client_id' => 'Client ID', |
57
|
|
|
'client_secret' => 'Client Secret', |
58
|
|
|
'redirect_uri' => 'Redirect Uri', |
59
|
|
|
'grant_types' => 'Grant Types', |
60
|
|
|
'scope' => 'Scope', |
61
|
|
|
'user_id' => 'User ID', |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ActiveQuery |
67
|
|
|
*/ |
68
|
|
|
public function getOauthAccessTokens(): ActiveQuery |
69
|
|
|
{ |
70
|
|
|
return $this->hasMany( |
71
|
|
|
OauthAccessTokens::class, |
72
|
|
|
['client_id' => 'client_id'] |
73
|
|
|
)->inverseOf('client'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return ActiveQuery |
78
|
|
|
*/ |
79
|
|
|
public function getOauthAuthorizationCodes(): ActiveQuery |
80
|
|
|
{ |
81
|
|
|
return $this->hasMany( |
82
|
|
|
OauthAuthorizationCodes::class, |
83
|
|
|
['client_id' => 'client_id'] |
84
|
|
|
)->inverseOf('client'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return ActiveQuery |
89
|
|
|
*/ |
90
|
|
|
public function getOauthRefreshTokens(): ActiveQuery |
91
|
|
|
{ |
92
|
|
|
return $this->hasMany( |
93
|
|
|
OauthRefreshTokens::class, |
94
|
|
|
['client_id' => 'client_id'] |
95
|
|
|
)->inverseOf('client'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function assureScope(string $scope): OauthScopes |
99
|
|
|
{ |
100
|
|
|
if (!str_contains($this->scopes, $scope)) { |
|
|
|
|
101
|
|
|
$this->unknownScope($scope ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return OauthScopes::findOne($scope) ?: $this->unknownScope($scope); |
|
|
|
|
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function validateUri(string $uri): bool |
109
|
|
|
{ |
110
|
|
|
return str_contains($this->redirect_uri, $uri); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function unknownScope(string $scope): never |
114
|
|
|
{ |
115
|
|
|
throw new IntegrityException( |
116
|
|
|
OauthScopes::class, |
117
|
|
|
"Unknown scope '$scope'" |
|
|
|
|
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|