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

QueryParamAuth::authenticate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 5
nop 3
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