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\oauth |
11
|
|
|
* |
12
|
|
|
* @author Vince scarpa <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
namespace responsible\core\auth; |
16
|
|
|
|
17
|
|
|
use responsible\core\auth; |
18
|
|
|
use responsible\core\configuration; |
19
|
|
|
use responsible\core\user; |
20
|
|
|
use responsible\core\server; |
21
|
|
|
|
22
|
|
|
class authorise extends server |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* [$user] |
26
|
|
|
* @var object |
27
|
|
|
*/ |
28
|
|
|
public $user; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* [__construct Inherit Responsible API options] |
32
|
|
|
*/ |
33
|
|
|
public function __construct($options) |
34
|
|
|
{ |
35
|
|
|
$this->setOptions($options); |
36
|
|
|
$this->config = new configuration\config; |
37
|
|
|
$this->config->responsibleDefault($options); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* [auth] |
42
|
|
|
* @return boolean|object |
43
|
|
|
*/ |
44
|
|
|
public function authorise() |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* Ignore if debug mode is initiated in Responsible API options |
48
|
|
|
*/ |
49
|
|
|
if ($this->getRequestType() == 'debug') { |
50
|
|
|
$this->grantAccess = true; |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if a custom scope is set |
56
|
|
|
*/ |
57
|
|
|
if( isset($this->header->getMethod()->data['scope']) && |
58
|
|
|
($this->header->getMethod()->data['scope'] == 'anonymous') |
59
|
|
|
) { |
60
|
|
|
$this->grantAccess = true; |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (isset($this->getOptions()['systemUser']) && !empty($this->getOptions()['systemUser'])) { |
65
|
|
|
$this->header |
66
|
|
|
->setHeader('Authorization', array( |
67
|
|
|
'Bearer', $this->getOptions()['systemUser']['token'], |
68
|
|
|
), "", ""); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Scan for a header Authorization Bearer Json Web Token |
73
|
|
|
* -- If not set header will return an unauthorised message |
74
|
|
|
*/ |
75
|
|
|
$token = $this->header->authorizationHeaders(); |
76
|
|
|
|
77
|
|
|
if (isset($token['client_access_request']) && !empty($token['client_access_request'])) { |
78
|
|
|
$this->user = (object) $token['client_access_request']; |
79
|
|
|
$this->grantAccess = true; |
80
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* [$jwt Decode the JWT] |
85
|
|
|
* @var auth\jwt |
86
|
|
|
*/ |
87
|
|
|
$jwt = new auth\jwt; |
88
|
|
|
$decoded = $jwt |
89
|
|
|
->setOptions($this->getOptions()) |
90
|
|
|
->token($token) |
91
|
|
|
->key('payloadOnly') |
92
|
|
|
->decode() |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
if( isset($decoded['sub']) && !empty($decoded['sub']) ) { |
96
|
|
|
|
97
|
|
|
$this->user = (object) (new user\user) |
98
|
|
|
->setOptions($this->getOptions()) |
99
|
|
|
->load($decoded['sub'], ['refreshToken' => true]) |
100
|
|
|
; |
101
|
|
|
|
102
|
|
|
if ( !empty($this->user) ) { |
103
|
|
|
$jwt = new auth\jwt; |
104
|
|
|
$decoded = $jwt |
105
|
|
|
->setOptions($this->getOptions()) |
106
|
|
|
->token($token) |
107
|
|
|
->key($this->user->secret) |
108
|
|
|
->decode() |
109
|
|
|
; |
110
|
|
|
} |
111
|
|
|
}else{ |
112
|
|
|
|
113
|
|
|
$this->header->unauthorised(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* [$user Check user account] |
119
|
|
|
* @var [object] |
120
|
|
|
*/ |
121
|
|
|
if ( (isset($decoded['sub']) && !empty($decoded['sub'])) && !$this->user ) { |
122
|
|
|
$this->user = (object) (new user\user) |
123
|
|
|
->setOptions($this->getOptions()) |
124
|
|
|
->load($decoded['sub'], ['refreshToken' => true]) |
125
|
|
|
; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Account not found / doesn't exist |
130
|
|
|
*/ |
131
|
|
|
if (empty($this->user)) { |
132
|
|
|
$this->header->unauthorised(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* [user] |
138
|
|
|
* @return object |
139
|
|
|
*/ |
140
|
|
|
public function user() |
141
|
|
|
{ |
142
|
|
|
if( $this->isGrantType() ) { |
143
|
|
|
return (object) [ |
144
|
|
|
'uid' => -1, |
145
|
|
|
'account_id' => 0, |
146
|
|
|
'scope' => 'anonymous', |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
return $this->user; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* [isGrantType If grant type is set then allow system scope override] |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
|
|
public function isGrantType() |
157
|
|
|
{ |
158
|
|
|
return $this->grantAccess; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* [getJWTToken Get the user JWT refresh object] |
163
|
|
|
* @return boolean|null |
164
|
|
|
*/ |
165
|
|
|
public function getJWTObject($objectKey, $array = null) |
166
|
|
|
{ |
167
|
|
|
if ($this->getRequestType() == 'debug') { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if( isset($this->header->getMethod()->data['scope']) && |
172
|
|
|
($this->header->getMethod()->data['scope'] == 'anonymous') |
173
|
|
|
) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (is_null($this->user)) { |
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$haystack = (is_null($array)) ? $this->user->refreshToken : $array; |
182
|
|
|
|
183
|
|
|
if (isset($haystack[$objectKey])) { |
184
|
|
|
return $haystack[$objectKey]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (is_array($haystack)) { |
188
|
|
|
foreach ($haystack as $key => $value) { |
189
|
|
|
if (is_array($value)) { |
190
|
|
|
return $this->getJWTObject($objectKey, $value); |
191
|
|
|
} |
192
|
|
|
if (false !== stripos($key, $objectKey)) { |
193
|
|
|
return $haystack[$key]; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|