AuthenticationAction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 15 3
A delete() 0 10 3
A getTokenFromAccessToken() 0 16 4
1
<?php
2
3
namespace yrc\actions;
4
5
use common\forms\Login;
0 ignored issues
show
Bug introduced by
The type common\forms\Login was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use ncryptf\Authorization;
7
use yrc\filters\auth\HMACSignatureAuth;
8
use yrc\rest\Action as RestAction;
9
use yii\web\UnauthorizedHttpException;
10
use Yii;
11
12
/**
13
 * Handles Authentication and Deauthentication of users
14
 * @class AuthenticationAction
15
 */
16
class AuthenticationAction extends RestAction
17
{
18
    /**
19
     * Authenticates a user using their username and password
20
     * @return mixed
21
     */
22
    public function post($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
    public function post(/** @scrutinizer ignore-unused */ $params)

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

Loading history...
23
    {
24
        $model = new Login;
25
        
26
        if ($model->load(['Login' => Yii::$app->request->post()])) {
27
            $token = $model->authenticate();
28
29
            if ($token === false) {
30
                throw new UnauthorizedHttpException('The credentials you provided are not valid', $model->exitStatus);
31
            } else {
32
                return $token->getAuthResponse();
33
            }
34
        }
35
            
36
        return false;
37
    }
38
39
    /**
40
     * Deauthenticates a user
41
     * @return mixed
42
     */
43
    public function delete($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public function delete(/** @scrutinizer ignore-unused */ $params)

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

Loading history...
44
    {
45
        $params = Authorization::extractParamsFromHeaderString(Yii::$app->request->getHeaders()->get(HMACSignatureAuth::AUTHORIZATION_HEADER));
0 ignored issues
show
Bug introduced by
It seems like Yii::app->request->getHe...::AUTHORIZATION_HEADER) can also be of type array; however, parameter $hmacHeader of ncryptf\Authorization::e...aramsFromHeaderString() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $params = Authorization::extractParamsFromHeaderString(/** @scrutinizer ignore-type */ Yii::$app->request->getHeaders()->get(HMACSignatureAuth::AUTHORIZATION_HEADER));
Loading history...
46
        if ($params) {
47
            if ($token = $this->getTokenFromAccessToken($params['access_token'])) {
48
                return (bool)$token->delete();
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
    * Retrieves a Token object from an access token string
57
    * @param string $accessToken
58
    * @return \yrc\models\redis\Token
59
    */
60
    private function getTokenFromAccessToken(string $accessToken)
61
    {
62
        try {
63
            $tokenClass = (Yii::$app->user->identityClass::TOKEN_CLASS);
64
            $token = $tokenClass::find()
65
                ->where(['access_token' => $accessToken])
66
                ->one();
67
        } catch (\Exception $e) {
68
            return null;
69
        }
70
71
        if ($token === null || $token->isExpired()) {
72
            return null;
73
        }
74
75
        return $token;
76
    }
77
}
78