1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ================================== |
4
|
|
|
* Responsible PHP API |
5
|
|
|
* ================================== |
6
|
|
|
* |
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
8
|
|
|
* |
9
|
|
|
* @api Responible API |
10
|
|
|
* @package responsible\core\headers |
11
|
|
|
* |
12
|
|
|
* @author Vince scarpa <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
namespace responsible\core\headers; |
16
|
|
|
|
17
|
|
|
use responsible\core\encoder; |
18
|
|
|
use responsible\core\exception; |
19
|
|
|
use responsible\core\helpers\help as helper; |
20
|
|
|
use responsible\core\user; |
21
|
|
|
|
22
|
|
|
class headerAuth extends header |
23
|
|
|
{ |
24
|
|
|
use \responsible\core\traits\optionsTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* [__construct] |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* [authorizationHeaders Scan for "Authorization" header] |
34
|
|
|
* @return string|array [mixed: string / error] |
35
|
|
|
*/ |
36
|
|
|
public function authorizationHeaders($skipError = false) |
37
|
|
|
{ |
38
|
|
|
if ($grant = $this->isGrantRequest()) { |
39
|
|
|
return $grant; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($clientToken = $this->hasBearerToken()) { |
43
|
|
|
return $clientToken; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!$skipError) { |
47
|
|
|
$this->setUnauthorised(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* [hasBearerValue Check if Authorization headers has Bearer value] |
53
|
|
|
* @throws Exception |
54
|
|
|
* Unauthorised |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
|
|
private function hasBearerValue() |
58
|
|
|
{ |
59
|
|
|
$auth_headers = $this->getHeaders(); |
60
|
|
|
|
61
|
|
|
if (isset($auth_headers["Authorization"]) && !empty($auth_headers["Authorization"])) { |
62
|
|
|
|
63
|
|
|
list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); |
64
|
|
|
|
65
|
|
|
if (strcasecmp(trim($type), "Bearer") == 0) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* [hasBearerToken Check if bearer token is present] |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
|
|
public function hasBearerToken() |
78
|
|
|
{ |
79
|
|
|
$auth_headers = $this->getHeaders(); |
80
|
|
|
|
81
|
|
|
if ($this->hasBearerValue()) { |
82
|
|
|
|
83
|
|
|
list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); |
84
|
|
|
|
85
|
|
|
if (strcasecmp(trim($type), "Bearer") == 0 && !empty($clientToken)) { |
86
|
|
|
return $clientToken; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if the request is a token grant |
95
|
|
|
* @return array|boolean |
96
|
|
|
*/ |
97
|
|
|
public function isGrantRequest() |
98
|
|
|
{ |
99
|
|
|
$auth_headers = $this->getHeaders(); |
100
|
|
|
$helper = new helper; |
101
|
|
|
|
102
|
|
|
if (isset($auth_headers["Authorization"]) && !empty($auth_headers["Authorization"])) { |
103
|
|
|
if ($grantType = $helper->checkVal($_REQUEST, 'grant_type')) { |
104
|
|
|
|
105
|
|
|
$refreshToken = false; |
106
|
|
|
|
107
|
|
|
if ($grantType == 'client_credentials') { |
108
|
|
|
$refreshToken = $this->accessCredentialHeaders($auth_headers); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($grantType == 'refresh_token') { |
112
|
|
|
$refreshToken = $this->accessRefreshHeaders($auth_headers); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($refreshToken) { |
116
|
|
|
return [ |
117
|
|
|
'client_access_request' => $refreshToken, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* [accessRefreshHeaders description] |
128
|
|
|
* @return string|array [mixed: string / error] |
129
|
|
|
*/ |
130
|
|
|
private function accessRefreshHeaders($auth_headers) |
131
|
|
|
{ |
132
|
|
|
list($type, $clientToken) = explode(" ", $auth_headers["Authorization"], 2); |
133
|
|
|
|
134
|
|
|
if (strcasecmp($type, "Bearer") == 0 && !empty($clientToken)) { |
135
|
|
|
|
136
|
|
|
$user = new user\user; |
137
|
|
|
$account = $user |
138
|
|
|
->setOptions($this->options) |
139
|
|
|
->load( |
140
|
|
|
$clientToken, |
141
|
|
|
array( |
142
|
|
|
'loadBy' => 'refresh_token', |
143
|
|
|
'getJWT' => true, |
144
|
|
|
'authorizationRefresh' => true, |
145
|
|
|
) |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
if (empty($account)) { |
149
|
|
|
$this->setUnauthorised(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$tokens = [ |
153
|
|
|
'token' => $account['JWT'], |
154
|
|
|
'refresh_token' => $account['refreshToken']['token'], |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
$account['refreshToken'] = $tokens; |
158
|
|
|
|
159
|
|
|
return $account; |
160
|
|
|
|
161
|
|
|
} else { |
162
|
|
|
$this->setUnauthorised(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* [accessCredentialHeaders Check if the credentials are correct] |
168
|
|
|
* @param array $auth_headers |
169
|
|
|
* @return string|array [mixed: string / error] |
170
|
|
|
*/ |
171
|
|
|
private function accessCredentialHeaders($auth_headers) |
172
|
|
|
{ |
173
|
|
|
$cipher = new encoder\cipher; |
174
|
|
|
|
175
|
|
|
list($type, $clientCredentials) = explode(" ", $auth_headers["Authorization"], 2); |
176
|
|
|
|
177
|
|
|
if (strcasecmp($type, "Basic") == 0 && !empty($clientCredentials)) { |
178
|
|
|
$credentails = explode('/', $clientCredentials); |
179
|
|
|
if (!empty($credentails) && is_array($credentails)) { |
180
|
|
|
$credentails = explode(':', $cipher->decode($clientCredentials)); |
181
|
|
|
|
182
|
|
|
if (!empty($credentails) && is_array($credentails) && sizeof($credentails) == 2) { |
183
|
|
|
$user = new user\user; |
184
|
|
|
$user->setAccountID($credentails[0]); |
185
|
|
|
|
186
|
|
|
$account = $user |
187
|
|
|
->setOptions($this->options) |
188
|
|
|
->load( |
189
|
|
|
$credentails[0], |
190
|
|
|
array( |
191
|
|
|
'loadBy' => 'account_id', |
192
|
|
|
'getJWT' => true, |
193
|
|
|
'authorizationRefresh' => true, |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$tokens = [ |
198
|
|
|
'token' => $account['JWT'], |
199
|
|
|
'refresh_token' => $account['refreshToken']['token'], |
200
|
|
|
]; |
201
|
|
|
|
202
|
|
|
$account['refreshToken'] = $tokens; |
203
|
|
|
|
204
|
|
|
if (!empty($account)) { |
205
|
|
|
if (strcasecmp($account['secret'], $credentails[1]) == 0) { |
206
|
|
|
return $account; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
|
|
$this->setUnauthorised(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* [unauthorised Set an unauthorised header] |
218
|
|
|
* @throws Exception |
219
|
|
|
* UNAUTHORIZED 401 |
220
|
|
|
* @return void |
221
|
|
|
*/ |
222
|
|
|
public function setUnauthorised() |
223
|
|
|
{ |
224
|
|
|
$this->setHeaders(); |
225
|
|
|
|
226
|
|
|
$this->setHeader('HTTP/1.1', array( |
227
|
|
|
'Unauthorized', |
228
|
|
|
), 401); |
229
|
|
|
|
230
|
|
|
(new exception\errorException)->error('UNAUTHORIZED'); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|