|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\oauth2server\filters; |
|
4
|
|
|
|
|
5
|
|
|
use roaresearch\yii2\oauth2server\RevokeAccessTokenInterface; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\{base\InvalidConfigException, web\ForbiddenHttpException}; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Revokes access tokens before executing an action. |
|
11
|
|
|
* |
|
12
|
|
|
* > Note: this is called on the before action event to make sure the token is |
|
13
|
|
|
* > always revoked even if there was an error in the request, for that reason |
|
14
|
|
|
* > its mandatory to have the authentication logic before this behavior is |
|
15
|
|
|
* > attached. |
|
16
|
|
|
* |
|
17
|
|
|
* Usage: |
|
18
|
|
|
* ```php |
|
19
|
|
|
* public function behaviors() |
|
20
|
|
|
* { |
|
21
|
|
|
* return [ |
|
22
|
|
|
* 'authenticator' => [ |
|
23
|
|
|
* // logic to auth the user with the token. |
|
24
|
|
|
* ], |
|
25
|
|
|
* 'revokeToken' => [ |
|
26
|
|
|
* 'classs' => RevokeAccessToken::class, |
|
27
|
|
|
* // other options. |
|
28
|
|
|
* ], |
|
29
|
|
|
* ]; |
|
30
|
|
|
* } |
|
31
|
|
|
* ``` |
|
32
|
|
|
* |
|
33
|
|
|
* It is also possible to attach this behavior to a controller with one of its |
|
34
|
|
|
* parent modules handling the authentication logic. |
|
35
|
|
|
* |
|
36
|
|
|
* For this behavior to work the class configured in |
|
37
|
|
|
* `Yii::$app->user->$identityClass` must implement |
|
38
|
|
|
* `RevokeAccessTokenInterface`. |
|
39
|
|
|
*/ |
|
40
|
|
|
class RevokeAccessToken extends \yii\base\ActionFilter |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string[] allows you to define scopes that when found revoke the |
|
44
|
|
|
* access token. When empty it revokes the access token regardless of scope. |
|
45
|
|
|
*/ |
|
46
|
|
|
public array $revokableScopes = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool if all access token must be revoked or just the active one. |
|
50
|
|
|
*/ |
|
51
|
|
|
public bool $revokeAll = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var bool whether or not allow guest users from accessing the action. |
|
55
|
|
|
*/ |
|
56
|
|
|
public bool $allowGuests = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function beforeAction($action): bool |
|
62
|
|
|
{ |
|
63
|
1 |
|
if (Yii::$app->user->getIsGuest()) { |
|
|
|
|
|
|
64
|
|
|
if ($this->allowGuests) { |
|
65
|
|
|
return true; |
|
66
|
|
|
} else { |
|
67
|
|
|
throw new ForbiddenHttpException( |
|
68
|
|
|
'User must be authenticated for this request.' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$user = Yii::$app->user->getIdentity(); |
|
74
|
1 |
|
if (!$user instanceof RevokeAccessTokenInterface) { |
|
75
|
|
|
throw new InvalidConfigException( |
|
76
|
|
|
$user::class . ' must implement ' |
|
77
|
|
|
. RevokeAccessTokenInterface::class |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
if (empty($this->revokableScopes) |
|
82
|
|
|
|| preg_match( |
|
83
|
|
|
'/\b(' . implode('|', $this->revokableScopes) . ')\b/', |
|
84
|
1 |
|
$user->getAccessTokenData()->scope |
|
85
|
|
|
) |
|
86
|
|
|
) { |
|
87
|
1 |
|
return $this->revokeAll |
|
88
|
|
|
? $user->revokeAllAccessTokens() |
|
89
|
1 |
|
: $user->revokeActiveAccessToken(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.