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; |
|
|
|
|
14
|
|
|
use League\OAuth2\Client\Provider\Client; |
|
|
|
|
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 |
|
|
|
|
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)) |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths