Completed
Push — master ( 932cc5...b796f0 )
by Andrii
12:16
created

QueryParamAuth   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 26
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 15 4
1
<?php
2
3
namespace hiapi\components;
4
5
use yii\filters\auth\AuthMethod;
6
7
/**
8
 * QueryParamAuth is an action filter that supports the authentication based on the access token passed through a query parameter.
9
 *
10
 * @author Qiang Xue <[email protected]>
11
 */
12
class QueryParamAuth extends AuthMethod
13
{
14
    /**
15
     * @var string the parameter name for passing the access token
16
     */
17
    public $tokenParam = 'access-token';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function authenticate($user, $request, $response)
23
    {
24
        $accessToken = $request->get($this->tokenParam) ?? $request->post($this->tokenParam);
25
        if (is_string($accessToken)) {
26
            $identity = $user->loginByAccessToken($accessToken, get_class($this));
27
            if ($identity !== null) {
28
                return $identity;
29
            }
30
        }
31
        if ($accessToken !== null) {
32
            $this->handleFailure($response);
33
        }
34
35
        return null;
36
    }
37
}
38