Passed
Push — master ( 149115...a32ae4 )
by Charles
03:54
created

AuthenticationAction::getTokenFromAccessToken()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type ncryptf\Authorization 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...
7
use yrc\filters\auth\HMACSignatureAuth;
8
use yrc\rest\Action as RestAction;
9
use yii\web\UnauthorizedHttpException;
0 ignored issues
show
Bug introduced by
The type yii\web\UnauthorizedHttpException 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...
10
use Yii;
0 ignored issues
show
Bug introduced by
The type Yii 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...
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)
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)
44
    {
45
        $params = Authorization::extractParamsFromHeaderString(Yii::$app->request->getHeaders()->get(HMACSignatureAuth::AUTHORIZATION_HEADER));
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