TokenBehavior   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 57
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 7 1
A afterAction() 0 13 2
A afterTokenCreate() 0 17 1
A revokeUserTokens() 0 8 2
1
<?php
2
3
namespace zacksleo\yii2\oauth2\common\behaviors;
4
5
use zacksleo\yii2\oauth2\common\helpers\Predis;
6
use yii;
7
use yii\base\Behavior;
8
use yii\web\Controller;
9
use zacksleo\yii2\oauth2\common\models\storage\AccessToken;
10
use zacksleo\yii2\oauth2\common\models\OauthClients;
11
12
/**
13
 * Class TokenBehavior
14
 * @package common\models\behaviors
15
 * @property $owner \common\helpers\utils\Token
16
 */
17
class TokenBehavior extends Behavior
18
{
19
    const EVENT_AFTER_TOKEN_CREATE = 'afterTokenCreate';
20
21
    public function events()
22
    {
23
        return [
24
            Controller::EVENT_AFTER_ACTION => 'afterAction',
25
            self::EVENT_AFTER_TOKEN_CREATE => 'afterTokenCreate'
26
        ];
27
    }
28
29
    public function afterAction($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $grantType = Yii::$app->request->getBodyParam('grant_type');
32
        $clientId = Yii::$app->request->getBodyParam('client_id');
0 ignored issues
show
Unused Code introduced by
$clientId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
        $clientSecret = Yii::$app->request->getBodyParam('client_secret');
0 ignored issues
show
Unused Code introduced by
$clientSecret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
        if (!in_array($grantType, ['password', 'refresh_token'])) {
35
            return true;
36
        }
37
        $token = Yii::$app->getModule('oauth2')->getServer()->getResponse()->getParameters();
38
        /* @var $oauthAccessToken array */
39
        $oauthAccessToken = (new AccessToken())->getAccessToken($token['access_token']);
40
        $this->revokeUserTokens($oauthAccessToken);
41
    }
42
43
    public function afterTokenCreate()
44
    {
45
        /**
46
         * Array
47
            (
48
            [access_token] => 97d7445c03e1551b5d22bfd4306c86f94ea0f9b3
49
            [expires_in] => 604800
50
            [token_type] => Bearer
51
            [scope] =>
52
            [refresh_token] => 8e01cc423cd74935fccd7b84bb0b6b3794ceed62
53
            )
54
         */
55
        $token = $this->owner->getToken();
56
        /* @var $oauthAccessToken array */
57
        $oauthAccessToken = (new AccessToken())->getAccessToken($token['access_token']);
58
        $this->revokeUserTokens($oauthAccessToken);
59
    }
60
61
    /**
62
     * 删除该用户该设备下的其他Token
63
     * @param $oauthAccessToken array
64
     */
65
    protected function revokeUserTokens($oauthAccessToken)
66
    {
67
        $client = OauthClients::findOne(['client_id' => $oauthAccessToken['client_id']]);
68
        if ($client->one_token_per_user) {
69
            //删除缓存
70
            Predis::getInstance()->getClient()->deleteOldUserClientToken($oauthAccessToken['access_token'], $oauthAccessToken['client_id'], $oauthAccessToken['user_id']);
71
        }
72
    }
73
}
74