|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\oauth2server\filters\auth; |
|
4
|
|
|
|
|
5
|
|
|
use roaresearch\yii2\oauth2server\filters\ErrorToExceptionTrait; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\filters\auth\{HttpBearerAuth, QueryParamAuth}; |
|
8
|
|
|
use yii\{helpers\StringHelper, web\HttpException}; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Filter to handle OAuth2 authentication. To do so it initialize the OAuth2 |
|
12
|
|
|
* Server and handles it responses. |
|
13
|
|
|
*/ |
|
14
|
|
|
class CompositeAuth extends \yii\filters\auth\CompositeAuth |
|
15
|
|
|
{ |
|
16
|
|
|
use ErrorToExceptionTrait { |
|
17
|
|
|
ErrorToExceptionTrait::beforeAction as traitBeforeAction; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string[] pairs of $actionPattern => $scope to require an scope for |
|
22
|
|
|
* specific actions comparing them with their action id. Wildcards like '*' |
|
23
|
|
|
* are allowed. |
|
24
|
|
|
* |
|
25
|
|
|
* If several $actionPatterns match the action being processed only the |
|
26
|
|
|
* first one will be used. |
|
27
|
|
|
* |
|
28
|
|
|
* @see https://www.yiiframework.com/doc/api/2.0/yii-helpers-basestringhelper#matchWildcard()-detail |
|
29
|
|
|
*/ |
|
30
|
|
|
public array $actionScopes = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public $authMethods = [ |
|
36
|
|
|
['class' => HttpBearerAuth::class], |
|
37
|
|
|
[ |
|
38
|
|
|
'class' => QueryParamAuth::class, |
|
39
|
|
|
'tokenParam' => 'accessToken', |
|
40
|
|
|
], |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function beforeAction($action): bool |
|
47
|
|
|
{ |
|
48
|
5 |
|
if ($this->traitBeforeAction($action)) { |
|
49
|
4 |
|
$this->oauth2Module->getServer()->verifyResourceRequest( |
|
50
|
|
|
null, |
|
51
|
|
|
null, |
|
52
|
4 |
|
$this->fetchActionScope($action->getUniqueId()) |
|
53
|
|
|
); |
|
54
|
4 |
|
$this->ensureSuccessResponse(); |
|
55
|
|
|
|
|
56
|
3 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Fetch the scope required for the action id. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $actionId |
|
66
|
|
|
* @return ?string the required scope or `null` if no scope is required. |
|
67
|
|
|
*/ |
|
68
|
4 |
|
protected function fetchActionScope(string $actionId): ?string |
|
69
|
|
|
{ |
|
70
|
4 |
|
if (empty($this->actionScopes)) { |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
$ownerId = $this->owner->getUniqueId(); |
|
|
|
|
|
|
75
|
4 |
|
foreach ($this->actionScopes as $actionPattern => $scope) { |
|
76
|
|
|
|
|
77
|
4 |
|
if (StringHelper::matchWildcard( |
|
78
|
4 |
|
"$ownerId/$actionPattern", |
|
79
|
|
|
$actionId |
|
80
|
|
|
)) { |
|
81
|
2 |
|
return $scope; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.