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