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

RefreshAction::post()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 11
nc 4
nop 1
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