Completed
Push — master ( d2d39f...01dd42 )
by Charles
02:06
created

RefreshAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B post() 0 26 4
1
<?php
2
3
namespace yrc\api\actions;
4
5
use yrc\api\actions\AuthenticationAction;
6
use app\models\Token;
7
use yrc\rest\Action as RestAction;
8
use yii\helpers\ArrayHelper;
9
use Yii;
10
11
/**
12
 * @class RefreshAction
13
 * Handles token refresh
14
 */
15
class RefreshAction extends RestAction
16
{
17
    public $extraAttributes = [];
18
19
    public $identityAttributes = [];
20
21
    /**
22
     * Refreshes the user's token
23
     * @return bool
24
     */
25
    public static function post($params)
26
    {
27
        // Get the token
28
        $token = AuthenticationAction::getAccessTokenFromHeader();
29
        
30
        $refreshToken = Yii::$app->request->post('refresh_token', false);
31
32
        if ($refreshToken !== $token->refresh_token) {
33
            return false;
34
        }
35
36
        // If we can delete the token, send a newly generated token out
37
        if ($token->delete()) {
38
            // Merge any extra attributes with the generated tokens
39
            $tokens = ArrayHelper::merge($params['class']['extraAttributes'], Token::generate(Yii::$app->user->id));
40
            // Merge the identity attributes
41
            foreach ($params['class']['identityAttributes'] as $attr) {
42
                $tokens[$attr] = Yii::$app->user->getIdentity()->$attr;
43
            }
44
45
            return $tokens;
46
        }
47
48
        // Return false for any other reasons
49
        return false;
50
    }
51
}
52