Test Failed
Push — master ( 2f15b8...6e5f5c )
by Julien
04:42
created

ClientController::authorizationUrlAction()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 1
nop 1
crap 6
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Modules\Oauth2\Controllers;
12
13
use League\OAuth2\Client\Grant\RefreshToken;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Grant\RefreshToken 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...
14
use League\OAuth2\Client\Provider\Client;
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Provider\Client 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...
15
16
/**
17
 * Class ClientController
18
 *
19
 * @property Client $oauth2Client
20
 * @package Zemit\Modules\Oauth2\Controllers
21
 */
22
class ClientController extends AbstractController
23
{
24
    const DEFAULT_SCOPE = null;
25
    
26
    public string $sessionKey = 'oauth2-client-state';
27
    
28
    /**
29
     * Redirect to Authorization Url
30
     *
31
     * @param null $scope
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $scope is correct as it would always require null to be passed?
Loading history...
32
     *
33
     * @return \Phalcon\Http\ResponseInterface
34
     */
35
    public function authorizationUrlAction($scope = null)
36
    {
37
        $redirectUrl = $this->oauth2Client->getAuthorizationUrl([
38
            'scope' => explode(',', $scope ?: $this->request->get('scope', 'string', self::DEFAULT_SCOPE))
0 ignored issues
show
introduced by
$scope is of type null, thus it always evaluated to false.
Loading history...
39
        ]);
40
        $this->session->set($this->sessionKey, $this->oauth2Client->getState());
41
        return $this->response->redirect($redirectUrl);
42
    }
43
    
44
    /**
45
     * Validate State
46
     *
47
     * @param null $state
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $state is correct as it would always require null to be passed?
Loading history...
48
     *
49
     * @return bool
50
     */
51
    public function validateState($state = null)
52
    {
53
        $state ??= $this->request->get('state', 'string');
54
        
55
        if (empty($state) || !$this->session->has($this->sessionKey)) {
56
            return false;
57
        }
58
        
59
        return $state === $this->session->get($this->sessionKey);
60
    }
61
    
62
    /**
63
     * Get Access Token
64
     *
65
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
66
     *
67
     * @return mixed
68
     */
69
    public function getAccessToken($code = null)
70
    {
71
        $code ??= $this->request->get('code', 'string');
72
        return $this->oauth2Client->getAccessToken('authorization_code', ['code' => $code]);
73
    }
74
    
75
    /**
76
     * Refresh Token
77
     *
78
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
79
     *
80
     * @return mixed
81
     */
82
    public function refreshToken($refreshToken = null)
83
    {
84
        $refreshToken ??= $this->request->get('refreshToken', 'string');
85
        return $this->oauth2Client->getAccessToken(new RefreshToken(), ['code' => $refreshToken]);
86
    }
87
    
88
    /**
89
     * Use this to interact with an API on the users behalf
90
     *
91
     * @param $token
92
     *
93
     * @return mixed
94
     */
95
    public function getToken($token)
96
    {
97
        return $token->getToken();
98
    }
99
    
100
    /**
101
     * Use this to get a new access token if the old one expires
102
     *
103
     * @param $token
104
     *
105
     * @return mixed
106
     */
107
    public function getRefreshToken($token)
108
    {
109
        return $token->getRefreshToken();
110
    }
111
    
112
    /**
113
     * Unix timestamp at which the access token expires
114
     *
115
     * @param $token
116
     *
117
     * @return mixed
118
     */
119
    public function getExpires($token)
120
    {
121
        return $token->getExpires();
122
    }
123
    
124
    /**
125
     * @param null $token
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $token is correct as it would always require null to be passed?
Loading history...
126
     *
127
     * @return mixed
128
     */
129
    public function getResourceOwner($token = null)
130
    {
131
        $token ??= $this->getAccessToken();
132
        return $this->oauth2Client->getResourceOwner($token);
133
    }
134
}
135