| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
|
|
|
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 226 |
liveuser |
6 |
Copyright (C) 2014~2025 MIN ZHI, CHEN
|
| 3 |
liveuser |
7 |
|
|
|
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
namespace qbpwcf;
|
|
|
25 |
|
|
|
26 |
/*
|
|
|
27 |
類別說明:
|
|
|
28 |
jwt應用的類別.
|
|
|
29 |
備註:
|
|
|
30 |
無.
|
|
|
31 |
*/
|
|
|
32 |
class jwt{
|
|
|
33 |
|
|
|
34 |
/*
|
|
|
35 |
#函式說明:
|
|
|
36 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
37 |
#回傳結果:
|
|
|
38 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
39 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
40 |
#$result["function"],當前執行的函式名稱.
|
|
|
41 |
#必填參數:
|
|
|
42 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
43 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
44 |
#參考資料:
|
|
|
45 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
46 |
*/
|
|
|
47 |
public function __call($method,$arguments){
|
| 226 |
liveuser |
48 |
|
| 3 |
liveuser |
49 |
#取得當前執行的函式
|
|
|
50 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
51 |
|
| 3 |
liveuser |
52 |
#設置執行不正常
|
|
|
53 |
$result["status"]="false";
|
| 226 |
liveuser |
54 |
|
| 3 |
liveuser |
55 |
#設置執行錯誤
|
|
|
56 |
$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
|
| 226 |
liveuser |
57 |
|
| 3 |
liveuser |
58 |
#設置所丟入的參數
|
|
|
59 |
$result["error"][]=$arguments;
|
| 226 |
liveuser |
60 |
|
| 3 |
liveuser |
61 |
#回傳結果
|
|
|
62 |
return $result;
|
| 226 |
liveuser |
63 |
|
| 3 |
liveuser |
64 |
}#function __call end
|
| 226 |
liveuser |
65 |
|
| 3 |
liveuser |
66 |
/*
|
|
|
67 |
#函式說明:
|
|
|
68 |
#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
|
|
|
69 |
#回傳結果:
|
|
|
70 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
71 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
72 |
#$result["function"],當前執行的函式名稱.
|
|
|
73 |
#必填參數:
|
|
|
74 |
#$method,物件,為物件實體或類別名稱,會自動置入該參數.
|
|
|
75 |
#$arguments,陣列,為呼叫方法時所用的參數.
|
|
|
76 |
#參考資料:
|
|
|
77 |
#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
|
|
|
78 |
*/
|
|
|
79 |
public static function __callStatic($method,$arguments){
|
| 226 |
liveuser |
80 |
|
| 3 |
liveuser |
81 |
#取得當前執行的函式
|
|
|
82 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
83 |
|
| 3 |
liveuser |
84 |
#設置執行不正常
|
|
|
85 |
$result["status"]="false";
|
| 226 |
liveuser |
86 |
|
| 3 |
liveuser |
87 |
#設置執行錯誤
|
|
|
88 |
$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
|
| 226 |
liveuser |
89 |
|
| 3 |
liveuser |
90 |
#設置所丟入的參數
|
|
|
91 |
$result["error"][]=$arguments;
|
| 226 |
liveuser |
92 |
|
| 3 |
liveuser |
93 |
#回傳結果
|
|
|
94 |
return $result;
|
| 226 |
liveuser |
95 |
|
| 3 |
liveuser |
96 |
}#function __callStatic end
|
| 226 |
liveuser |
97 |
|
| 3 |
liveuser |
98 |
/*
|
|
|
99 |
#函式說明:
|
|
|
100 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现.
|
|
|
101 |
#回傳結果:
|
|
|
102 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
103 |
#$result["error"],錯誤訊息陣列
|
|
|
104 |
#$result["function"],當前函數執行的名稱
|
| 226 |
liveuser |
105 |
#$result["content"],base64UrlEncode编码後的內容。
|
| 3 |
liveuser |
106 |
#必填參數:
|
|
|
107 |
#$conf["input"],字串,需要編碼的字串.
|
|
|
108 |
$conf["input"]="";
|
|
|
109 |
#可省略參數:
|
|
|
110 |
#無
|
|
|
111 |
#參考資料:
|
|
|
112 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
113 |
#備註:
|
|
|
114 |
#無.
|
|
|
115 |
*/
|
|
|
116 |
public static function base64UrlEncode(&$conf){
|
| 226 |
liveuser |
117 |
|
| 3 |
liveuser |
118 |
#初始化要回傳的內容
|
|
|
119 |
$result=array();
|
| 226 |
liveuser |
120 |
|
| 3 |
liveuser |
121 |
#取得當前執行的函數名稱
|
|
|
122 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
123 |
|
| 3 |
liveuser |
124 |
#初始化要回傳的語法
|
|
|
125 |
$result["content"]="";
|
| 226 |
liveuser |
126 |
|
| 3 |
liveuser |
127 |
#如果 $conf 不為陣列
|
|
|
128 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
129 |
|
| 3 |
liveuser |
130 |
#設置執行失敗
|
|
|
131 |
$result["status"]="false";
|
| 226 |
liveuser |
132 |
|
| 3 |
liveuser |
133 |
#設置執行錯誤訊息
|
|
|
134 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
135 |
|
|
|
136 |
#如果傳入的參數為 null
|
|
|
137 |
if($conf==null){
|
| 226 |
liveuser |
138 |
|
| 3 |
liveuser |
139 |
#設置執行錯誤訊息
|
|
|
140 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
141 |
|
| 3 |
liveuser |
142 |
}#if end
|
|
|
143 |
|
|
|
144 |
#回傳結果
|
|
|
145 |
return $result;
|
| 226 |
liveuser |
146 |
|
| 3 |
liveuser |
147 |
}#if end
|
| 226 |
liveuser |
148 |
|
| 3 |
liveuser |
149 |
#檢查參數
|
|
|
150 |
#函式說明:
|
|
|
151 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
152 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
153 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
154 |
#$result["function"],當前執行的函式名稱.
|
|
|
155 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
156 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
157 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
158 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
159 |
#必填寫的參數:
|
|
|
160 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
161 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
162 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
163 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("input");
|
| 226 |
liveuser |
164 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
165 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
166 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
167 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
168 |
#可以省略的參數:
|
|
|
169 |
#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
170 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
171 |
#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
172 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("mimeType","compressType","delFile");
|
| 226 |
liveuser |
173 |
#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
174 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
|
|
|
175 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,「null」代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
176 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("null","base64","false");
|
|
|
177 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
178 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
179 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
180 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
181 |
|
| 3 |
liveuser |
182 |
#如果檢查參數失敗
|
|
|
183 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
184 |
|
| 3 |
liveuser |
185 |
#設置執行失敗
|
|
|
186 |
$result["status"]="false";
|
| 226 |
liveuser |
187 |
|
| 3 |
liveuser |
188 |
#設置錯誤訊息
|
|
|
189 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
190 |
|
| 3 |
liveuser |
191 |
#回傳結果
|
|
|
192 |
return $result;
|
| 226 |
liveuser |
193 |
|
| 3 |
liveuser |
194 |
}#if end
|
| 226 |
liveuser |
195 |
|
| 3 |
liveuser |
196 |
#如果檢查參數不通過
|
|
|
197 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
198 |
|
| 3 |
liveuser |
199 |
#設置執行失敗
|
|
|
200 |
$result["status"]="false";
|
| 226 |
liveuser |
201 |
|
| 3 |
liveuser |
202 |
#設置錯誤訊息
|
|
|
203 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
204 |
|
| 3 |
liveuser |
205 |
#回傳結果
|
|
|
206 |
return $result;
|
| 226 |
liveuser |
207 |
|
| 3 |
liveuser |
208 |
}#if end
|
| 226 |
liveuser |
209 |
|
| 3 |
liveuser |
210 |
#設置執行正常
|
|
|
211 |
$result["status"]="true";
|
| 226 |
liveuser |
212 |
|
| 3 |
liveuser |
213 |
#設置 base64UrlEncode 字串
|
|
|
214 |
$result["content"]=str_replace('=', '', strtr(base64_encode($conf["input"]), '+/', '-_'));
|
| 226 |
liveuser |
215 |
|
| 3 |
liveuser |
216 |
#回傳結果
|
|
|
217 |
return $result;
|
| 226 |
liveuser |
218 |
|
| 3 |
liveuser |
219 |
}#funcion base64UrlEncode end
|
| 226 |
liveuser |
220 |
|
| 3 |
liveuser |
221 |
/*
|
|
|
222 |
#函式說明:
|
|
|
223 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现
|
|
|
224 |
#回傳結果:
|
|
|
225 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
226 |
#$result["error"],錯誤訊息陣列.
|
|
|
227 |
#$result["function"],當前函數執行的名稱.
|
|
|
228 |
#$result["content"],base64UrlEncode解碼後的內容.
|
|
|
229 |
#必填參數:
|
|
|
230 |
#conf["input"],字串,需要解码的字符串.
|
|
|
231 |
$conf["input"]="";
|
|
|
232 |
#可省略參數:
|
|
|
233 |
#無
|
|
|
234 |
#參考資料:
|
|
|
235 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
236 |
#備註:
|
|
|
237 |
#無.
|
|
|
238 |
*/
|
|
|
239 |
public static function base64UrlDecode(&$conf){
|
| 226 |
liveuser |
240 |
|
| 3 |
liveuser |
241 |
#初始化要回傳的內容
|
|
|
242 |
$result=array();
|
| 226 |
liveuser |
243 |
|
| 3 |
liveuser |
244 |
#取得當前執行的函數名稱
|
|
|
245 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
246 |
|
| 3 |
liveuser |
247 |
#初始化要回傳的語法
|
|
|
248 |
$result["content"]="";
|
| 226 |
liveuser |
249 |
|
| 3 |
liveuser |
250 |
#如果 $conf 不為陣列
|
|
|
251 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
252 |
|
| 3 |
liveuser |
253 |
#設置執行失敗
|
|
|
254 |
$result["status"]="false";
|
| 226 |
liveuser |
255 |
|
| 3 |
liveuser |
256 |
#設置執行錯誤訊息
|
|
|
257 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
258 |
|
|
|
259 |
#如果傳入的參數為 null
|
|
|
260 |
if($conf==null){
|
| 226 |
liveuser |
261 |
|
| 3 |
liveuser |
262 |
#設置執行錯誤訊息
|
|
|
263 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
264 |
|
| 3 |
liveuser |
265 |
}#if end
|
|
|
266 |
|
|
|
267 |
#回傳結果
|
|
|
268 |
return $result;
|
| 226 |
liveuser |
269 |
|
| 3 |
liveuser |
270 |
}#if end
|
| 226 |
liveuser |
271 |
|
| 3 |
liveuser |
272 |
#檢查參數
|
|
|
273 |
#函式說明:
|
|
|
274 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
275 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
276 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
277 |
#$result["function"],當前執行的函式名稱.
|
|
|
278 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
279 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
280 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
281 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
282 |
#必填寫的參數:
|
|
|
283 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
284 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
285 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
286 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("input");
|
| 226 |
liveuser |
287 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
288 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
289 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
290 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
291 |
#可以省略的參數:
|
|
|
292 |
#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
293 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
294 |
#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
295 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("mimeType","compressType","delFile");
|
| 226 |
liveuser |
296 |
#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
297 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
|
|
|
298 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,「null」代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
299 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("null","base64","false");
|
|
|
300 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
301 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
302 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
303 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
304 |
|
| 3 |
liveuser |
305 |
#如果檢查參數失敗
|
|
|
306 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
307 |
|
| 3 |
liveuser |
308 |
#設置執行失敗
|
|
|
309 |
$result["status"]="false";
|
| 226 |
liveuser |
310 |
|
| 3 |
liveuser |
311 |
#設置錯誤訊息
|
|
|
312 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
313 |
|
| 3 |
liveuser |
314 |
#回傳結果
|
|
|
315 |
return $result;
|
| 226 |
liveuser |
316 |
|
| 3 |
liveuser |
317 |
}#if end
|
| 226 |
liveuser |
318 |
|
| 3 |
liveuser |
319 |
#如果檢查參數不通過
|
|
|
320 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
321 |
|
| 3 |
liveuser |
322 |
#設置執行失敗
|
|
|
323 |
$result["status"]="false";
|
| 226 |
liveuser |
324 |
|
| 3 |
liveuser |
325 |
#設置錯誤訊息
|
|
|
326 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
327 |
|
| 3 |
liveuser |
328 |
#回傳結果
|
|
|
329 |
return $result;
|
| 226 |
liveuser |
330 |
|
| 3 |
liveuser |
331 |
}#if end
|
| 226 |
liveuser |
332 |
|
| 3 |
liveuser |
333 |
$remainder = strlen($conf["input"]) % 4;
|
|
|
334 |
if($remainder){
|
| 226 |
liveuser |
335 |
|
| 3 |
liveuser |
336 |
$addlen = 4 - $remainder;
|
|
|
337 |
$conf["input"] .= str_repeat('=', $addlen);
|
| 226 |
liveuser |
338 |
|
| 3 |
liveuser |
339 |
}#if end
|
| 226 |
liveuser |
340 |
|
| 3 |
liveuser |
341 |
#設置執行正常
|
| 226 |
liveuser |
342 |
$result["status"]="true";
|
|
|
343 |
|
|
|
344 |
#設置 base64UrlDecode 的結果.
|
| 3 |
liveuser |
345 |
$result["content"]=base64_decode(strtr($conf["input"], '-_', '+/'));
|
| 226 |
liveuser |
346 |
|
| 3 |
liveuser |
347 |
#回傳結果
|
|
|
348 |
return $result;
|
| 226 |
liveuser |
349 |
|
| 3 |
liveuser |
350 |
}#function base64UrlDecode end
|
| 226 |
liveuser |
351 |
|
| 3 |
liveuser |
352 |
/*
|
|
|
353 |
#函式說明:
|
|
|
354 |
#產生 HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现
|
|
|
355 |
#回傳結果:
|
|
|
356 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
357 |
#$result["error"],錯誤訊息陣列.
|
|
|
358 |
#$result["function"],當前函數執行的名稱.
|
|
|
359 |
#$result["content"],base64UrlEncode(header).".".base64UrlEncode(payload)的簽章
|
|
|
360 |
#必填參數:
|
|
|
361 |
#$conf["input"],string,为base64UrlEncode(header).".".base64UrlEncode(payload)
|
|
|
362 |
$conf["input"]="";
|
|
|
363 |
#$conf["key"],字串,金鑰.
|
|
|
364 |
$conf["key"]="";
|
|
|
365 |
#可省略參數:
|
|
|
366 |
#無.
|
|
|
367 |
#參考資料:
|
|
|
368 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
369 |
#https://www.php.net/manual/en/function.hash-hmac.php
|
|
|
370 |
#備註:
|
|
|
371 |
#無.
|
|
|
372 |
*/
|
|
|
373 |
public static function signature(&$conf){
|
| 226 |
liveuser |
374 |
|
| 3 |
liveuser |
375 |
#初始化要回傳的內容
|
|
|
376 |
$result=array();
|
| 226 |
liveuser |
377 |
|
| 3 |
liveuser |
378 |
#取得當前執行的函數名稱
|
|
|
379 |
$result["function"]=__FUNCTION__;
|
| 226 |
liveuser |
380 |
|
| 3 |
liveuser |
381 |
#初始化要回傳的語法
|
|
|
382 |
$result["content"]="";
|
| 226 |
liveuser |
383 |
|
| 3 |
liveuser |
384 |
#如果 $conf 不為陣列
|
|
|
385 |
if(gettype($conf)!="array"){
|
| 226 |
liveuser |
386 |
|
| 3 |
liveuser |
387 |
#設置執行失敗
|
|
|
388 |
$result["status"]="false";
|
| 226 |
liveuser |
389 |
|
| 3 |
liveuser |
390 |
#設置執行錯誤訊息
|
|
|
391 |
$result["error"][]="\$conf變數須為陣列形態";
|
|
|
392 |
|
|
|
393 |
#如果傳入的參數為 null
|
|
|
394 |
if($conf==null){
|
| 226 |
liveuser |
395 |
|
| 3 |
liveuser |
396 |
#設置執行錯誤訊息
|
|
|
397 |
$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
|
| 226 |
liveuser |
398 |
|
| 3 |
liveuser |
399 |
}#if end
|
|
|
400 |
|
|
|
401 |
#回傳結果
|
|
|
402 |
return $result;
|
| 226 |
liveuser |
403 |
|
| 3 |
liveuser |
404 |
}#if end
|
| 226 |
liveuser |
405 |
|
| 3 |
liveuser |
406 |
#檢查參數
|
|
|
407 |
#函式說明:
|
|
|
408 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
409 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
410 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
411 |
#$result["function"],當前執行的函式名稱.
|
|
|
412 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
413 |
#$result[$shouldBtCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
414 |
#$result[$shouldBtCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
415 |
#$result[$shouldBtCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
416 |
#必填寫的參數:
|
|
|
417 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
418 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
419 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
420 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("input","key");
|
| 226 |
liveuser |
421 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列 例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
422 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
423 |
#$conf["variableCheck::checkArguments"]["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
424 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
425 |
#可以省略的參數:
|
|
|
426 |
#$conf["canBeEmptyString"],必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true"。
|
|
|
427 |
$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
|
|
|
428 |
#$conf["skipableVariableName"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
429 |
#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("mimeType","compressType","delFile");
|
| 226 |
liveuser |
430 |
#$conf["skipableVariableType"],爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
| 3 |
liveuser |
431 |
#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","string","string");
|
|
|
432 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,「null」代表不指定,若預設值是必填參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
433 |
#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("null","base64","false");
|
|
|
434 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
435 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
436 |
$checkResult=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
437 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
438 |
|
| 3 |
liveuser |
439 |
#如果檢查參數失敗
|
|
|
440 |
if($checkResult["status"]=="false"){
|
| 226 |
liveuser |
441 |
|
| 3 |
liveuser |
442 |
#設置執行失敗
|
|
|
443 |
$result["status"]="false";
|
| 226 |
liveuser |
444 |
|
| 3 |
liveuser |
445 |
#設置錯誤訊息
|
|
|
446 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
447 |
|
| 3 |
liveuser |
448 |
#回傳結果
|
|
|
449 |
return $result;
|
| 226 |
liveuser |
450 |
|
| 3 |
liveuser |
451 |
}#if end
|
| 226 |
liveuser |
452 |
|
| 3 |
liveuser |
453 |
#如果檢查參數不通過
|
|
|
454 |
if($checkResult["passed"]=="false"){
|
| 226 |
liveuser |
455 |
|
| 3 |
liveuser |
456 |
#設置執行失敗
|
|
|
457 |
$result["status"]="false";
|
| 226 |
liveuser |
458 |
|
| 3 |
liveuser |
459 |
#設置錯誤訊息
|
|
|
460 |
$result["error"]=$checkResult;
|
| 226 |
liveuser |
461 |
|
| 3 |
liveuser |
462 |
#回傳結果
|
|
|
463 |
return $result;
|
| 226 |
liveuser |
464 |
|
| 3 |
liveuser |
465 |
}#if end
|
| 226 |
liveuser |
466 |
|
| 3 |
liveuser |
467 |
#函式說明:
|
|
|
468 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现.
|
|
|
469 |
#回傳結果:
|
|
|
470 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
471 |
#$result["error"],錯誤訊息陣列
|
|
|
472 |
#$result["function"],當前函數執行的名稱
|
| 226 |
liveuser |
473 |
#$result["content"],base64UrlEncode编码後的內容。
|
| 3 |
liveuser |
474 |
#必填參數:
|
|
|
475 |
#$conf["input"],字串,需要編碼的字串.
|
|
|
476 |
$conf["jwt::base64UrlEncode"]["input"]=hash_hmac("sha256", $conf["input"], $conf["key"],true);
|
|
|
477 |
#可省略參數:
|
|
|
478 |
#無
|
|
|
479 |
#參考資料:
|
|
|
480 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
481 |
#備註:
|
|
|
482 |
#無.
|
|
|
483 |
$base64UrlEncode=jwt::base64UrlEncode($conf["jwt::base64UrlEncode"]);
|
|
|
484 |
unset($conf["jwt::base64UrlEncode"]);
|
| 226 |
liveuser |
485 |
|
| 3 |
liveuser |
486 |
#如果檢查參數失敗
|
|
|
487 |
if($base64UrlEncode["status"]=="false"){
|
| 226 |
liveuser |
488 |
|
| 3 |
liveuser |
489 |
#設置執行失敗
|
|
|
490 |
$result["status"]="false";
|
| 226 |
liveuser |
491 |
|
| 3 |
liveuser |
492 |
#設置錯誤訊息
|
|
|
493 |
$result["error"]=$base64UrlEncode;
|
| 226 |
liveuser |
494 |
|
| 3 |
liveuser |
495 |
#回傳結果
|
|
|
496 |
return $result;
|
| 226 |
liveuser |
497 |
|
| 3 |
liveuser |
498 |
}#if end
|
| 226 |
liveuser |
499 |
|
| 3 |
liveuser |
500 |
#取得加密後的內容
|
|
|
501 |
$result["content"]=$base64UrlEncode["content"];
|
| 226 |
liveuser |
502 |
|
| 3 |
liveuser |
503 |
#設置執行正常
|
|
|
504 |
$result["status"]="true";
|
| 226 |
liveuser |
505 |
|
| 3 |
liveuser |
506 |
#回傳結果
|
|
|
507 |
return $result;
|
| 226 |
liveuser |
508 |
|
| 3 |
liveuser |
509 |
}#function signature end
|
| 226 |
liveuser |
510 |
|
| 3 |
liveuser |
511 |
/*
|
|
|
512 |
#函式說明:
|
|
|
513 |
#建立 JWT 格式的內容
|
|
|
514 |
#回傳結果:
|
|
|
515 |
#$result["status"],狀態,"true"代表正常;"false"代表不正常.
|
|
|
516 |
#$result["error"],錯誤訊息陣列.
|
|
|
517 |
#$result["content"],jwt token.
|
|
|
518 |
#必填參數:
|
|
|
519 |
#$conf["key"],用來驗證簽章的加解密key.
|
|
|
520 |
$conf["key"]="";
|
|
|
521 |
#可省略參數:
|
|
|
522 |
#$conf["exp"],有效期限要多久,預設為當下加3600秒.
|
|
|
523 |
#$conf["exp"]=time()+3600;
|
|
|
524 |
#$conf["iat"],簽證時間,預設為當下.
|
|
|
525 |
#$conf["iat"]=time();
|
|
|
526 |
#$conf["otherBodies"],陣列,其他要放置的payload body內容,key為名稱,value為內容.
|
|
|
527 |
#$conf["otherBodies"]=array();
|
|
|
528 |
#$conf["base64encodeSig"],是否要base64加密金鑰,預設為"false"不加密,反之"true"代表要加密.
|
|
|
529 |
#$conf["base64encodeSig"]="false";
|
|
|
530 |
#參考資料:
|
|
|
531 |
#無.
|
|
|
532 |
#備註:
|
|
|
533 |
#無.
|
|
|
534 |
*/
|
|
|
535 |
public static function makeJWT(&$conf){
|
| 226 |
liveuser |
536 |
|
| 3 |
liveuser |
537 |
#函式說明:
|
|
|
538 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
539 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
540 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
541 |
#$result["function"],當前執行的函式名稱.
|
|
|
542 |
#$result["argu"],設置給予的參數.
|
|
|
543 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
544 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
545 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
546 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
547 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
548 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
549 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
550 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
551 |
#必填寫的參數:
|
|
|
552 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
553 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
554 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
555 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
556 |
#可以省略的參數:
|
|
|
557 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
558 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("key");
|
|
|
559 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
560 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
|
|
|
561 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
562 |
#$conf["canBeEmptyString"]="false";
|
|
|
563 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
564 |
#$conf["canNotBeEmpty"]=array();
|
|
|
565 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
566 |
#$conf["canBeEmpty"]=array();
|
|
|
567 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
568 |
$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("exp","iat","otherBodies","base64encodeSig");
|
|
|
569 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
570 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("exp","iat","otherBodies","base64encodeSig");
|
|
|
571 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
572 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("integer","integer","array","string");
|
|
|
573 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
574 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(time()+3600,time(),null,"false");
|
|
|
575 |
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
|
|
|
576 |
#$conf["disallowAllSkipableVarIsEmpty"]="";
|
|
|
577 |
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
|
|
|
578 |
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
|
|
|
579 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
580 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
581 |
#參考資料來源:
|
|
|
582 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
583 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
584 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
585 |
|
| 3 |
liveuser |
586 |
#如果檢查失敗
|
|
|
587 |
if($checkArguments["status"]==="false")
|
|
|
588 |
{
|
|
|
589 |
#設置執行失敗
|
|
|
590 |
$result["status"]="false";
|
| 226 |
liveuser |
591 |
|
| 3 |
liveuser |
592 |
#設置錯誤訊息
|
|
|
593 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
594 |
|
| 3 |
liveuser |
595 |
#回傳結果
|
|
|
596 |
return $result;
|
| 226 |
liveuser |
597 |
|
| 3 |
liveuser |
598 |
}#if end
|
| 226 |
liveuser |
599 |
|
| 3 |
liveuser |
600 |
#如果檢查不通過
|
|
|
601 |
if($checkArguments["passed"]==="false")
|
|
|
602 |
{
|
|
|
603 |
#設置執行失敗
|
|
|
604 |
$result["status"]="false";
|
| 226 |
liveuser |
605 |
|
| 3 |
liveuser |
606 |
#設置錯誤訊息
|
|
|
607 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
608 |
|
| 3 |
liveuser |
609 |
#回傳結果
|
|
|
610 |
return $result;
|
| 226 |
liveuser |
611 |
|
| 3 |
liveuser |
612 |
}#if end
|
| 226 |
liveuser |
613 |
|
| 3 |
liveuser |
614 |
#建立 header
|
| 226 |
liveuser |
615 |
$head=array();
|
| 3 |
liveuser |
616 |
$head["alg"]="HS256";
|
|
|
617 |
$head["typ"]="JWT";
|
| 226 |
liveuser |
618 |
$head=json_encode($head);
|
| 3 |
liveuser |
619 |
#函式說明:
|
|
|
620 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现.
|
|
|
621 |
#回傳結果:
|
|
|
622 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
623 |
#$result["error"],錯誤訊息陣列
|
|
|
624 |
#$result["function"],當前函數執行的名稱
|
| 226 |
liveuser |
625 |
#$result["content"],base64UrlEncode编码後的內容。
|
| 3 |
liveuser |
626 |
#必填參數:
|
|
|
627 |
#$conf["input"],字串,需要編碼的字串.
|
|
|
628 |
$conf["jwt::base64UrlEncode"]["input"]=$head;
|
|
|
629 |
#可省略參數:
|
|
|
630 |
#無
|
|
|
631 |
#參考資料:
|
|
|
632 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
633 |
#備註:
|
|
|
634 |
#無.
|
|
|
635 |
$base64UrlEncode=jwt::base64UrlEncode($conf["jwt::base64UrlEncode"]);
|
|
|
636 |
unset($conf["jwt::base64UrlEncode"]);
|
| 226 |
liveuser |
637 |
|
| 3 |
liveuser |
638 |
#如果加密失敗
|
|
|
639 |
if($base64UrlEncode["status"]==="false")
|
|
|
640 |
{
|
|
|
641 |
#設置執行失敗
|
|
|
642 |
$result["status"]="false";
|
| 226 |
liveuser |
643 |
|
| 3 |
liveuser |
644 |
#設置錯誤訊息
|
|
|
645 |
$result["error"]=$base64UrlEncode;
|
| 226 |
liveuser |
646 |
|
| 3 |
liveuser |
647 |
#回傳結果
|
|
|
648 |
return $result;
|
| 226 |
liveuser |
649 |
|
| 3 |
liveuser |
650 |
}#if end
|
| 226 |
liveuser |
651 |
|
| 3 |
liveuser |
652 |
#取得加密後的內容
|
|
|
653 |
$head=$base64UrlEncode["content"];
|
| 226 |
liveuser |
654 |
|
| 3 |
liveuser |
655 |
#初始化 body
|
|
|
656 |
$body=array();
|
| 226 |
liveuser |
657 |
|
| 3 |
liveuser |
658 |
#設置到期時間
|
|
|
659 |
$body["exp"]=$conf["exp"];
|
| 226 |
liveuser |
660 |
|
| 3 |
liveuser |
661 |
#設置簽章時間
|
|
|
662 |
$body["iat"]=$conf["iat"];
|
| 226 |
liveuser |
663 |
|
| 3 |
liveuser |
664 |
#如果有 $conf["otherBodies"]
|
|
|
665 |
if(isset($conf["otherBodies"])){
|
| 226 |
liveuser |
666 |
|
| 3 |
liveuser |
667 |
#針對每個 body
|
|
|
668 |
foreach($conf["otherBodies"] as $key=>$value){
|
| 226 |
liveuser |
669 |
|
| 3 |
liveuser |
670 |
#加上 body
|
|
|
671 |
$body[$key]=$value;
|
| 226 |
liveuser |
672 |
|
| 3 |
liveuser |
673 |
}#foreach end
|
| 226 |
liveuser |
674 |
|
| 3 |
liveuser |
675 |
}#if end
|
| 226 |
liveuser |
676 |
|
| 3 |
liveuser |
677 |
$body=json_encode($body);
|
|
|
678 |
#函式說明:
|
|
|
679 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现.
|
|
|
680 |
#回傳結果:
|
|
|
681 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
682 |
#$result["error"],錯誤訊息陣列
|
|
|
683 |
#$result["function"],當前函數執行的名稱
|
| 226 |
liveuser |
684 |
#$result["content"],base64UrlEncode编码後的內容。
|
| 3 |
liveuser |
685 |
#必填參數:
|
|
|
686 |
#$conf["input"],字串,需要編碼的字串.
|
|
|
687 |
$conf["jwt::base64UrlEncode"]["input"]=$body;
|
|
|
688 |
#可省略參數:
|
|
|
689 |
#無
|
|
|
690 |
#參考資料:
|
|
|
691 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
692 |
#備註:
|
|
|
693 |
#無.
|
|
|
694 |
$base64UrlEncode=jwt::base64UrlEncode($conf["jwt::base64UrlEncode"]);
|
|
|
695 |
unset($conf["jwt::base64UrlEncode"]);
|
| 226 |
liveuser |
696 |
|
| 3 |
liveuser |
697 |
#如果加密失敗
|
|
|
698 |
if($base64UrlEncode["status"]==="false")
|
|
|
699 |
{
|
|
|
700 |
#設置執行失敗
|
|
|
701 |
$result["status"]="false";
|
| 226 |
liveuser |
702 |
|
| 3 |
liveuser |
703 |
#設置錯誤訊息
|
|
|
704 |
$result["error"]=$base64UrlEncode;
|
| 226 |
liveuser |
705 |
|
| 3 |
liveuser |
706 |
#回傳結果
|
|
|
707 |
return $result;
|
| 226 |
liveuser |
708 |
|
| 3 |
liveuser |
709 |
}#if end
|
| 226 |
liveuser |
710 |
|
| 3 |
liveuser |
711 |
#取得加密後的內容
|
|
|
712 |
$body=$base64UrlEncode["content"];
|
| 226 |
liveuser |
713 |
|
| 3 |
liveuser |
714 |
#產生簽章
|
|
|
715 |
#函式說明:
|
|
|
716 |
#產生 HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现
|
|
|
717 |
#回傳結果:
|
|
|
718 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
719 |
#$result["error"],錯誤訊息陣列.
|
|
|
720 |
#$result["function"],當前函數執行的名稱.
|
|
|
721 |
#$result["content"],base64UrlEncode(header).".".base64UrlEncode(payload)的簽章
|
|
|
722 |
#必填參數:
|
|
|
723 |
#$conf["input"],string,为base64UrlEncode(header).".".base64UrlEncode(payload)
|
|
|
724 |
$conf["jwt::signature"]["input"]=$head.".".$body;
|
|
|
725 |
#$conf["key"],字串,金鑰.
|
|
|
726 |
$conf["jwt::signature"]["key"]=$conf["key"];
|
|
|
727 |
#參考資料:
|
|
|
728 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
729 |
#https://www.php.net/manual/en/function.hash-hmac.php
|
|
|
730 |
#備註:
|
|
|
731 |
#無.
|
|
|
732 |
$signature=jwt::signature($conf["jwt::signature"]);
|
|
|
733 |
unset($conf["jwt::signature"]);
|
| 226 |
liveuser |
734 |
|
| 3 |
liveuser |
735 |
#如果產生signature失敗
|
|
|
736 |
if($signature["status"]==="false")
|
|
|
737 |
{
|
|
|
738 |
#設置執行失敗
|
|
|
739 |
$result["status"]="false";
|
| 226 |
liveuser |
740 |
|
| 3 |
liveuser |
741 |
#設置錯誤訊息
|
|
|
742 |
$result["error"]=$signature;
|
| 226 |
liveuser |
743 |
|
| 3 |
liveuser |
744 |
#回傳結果
|
|
|
745 |
return $result;
|
| 226 |
liveuser |
746 |
|
| 3 |
liveuser |
747 |
}#if end
|
| 226 |
liveuser |
748 |
|
| 3 |
liveuser |
749 |
#取得簽章
|
|
|
750 |
$sig=$signature["content"];
|
| 226 |
liveuser |
751 |
|
|
|
752 |
#如果有要加密
|
| 3 |
liveuser |
753 |
if($conf["base64encodeSig"]==="true"){
|
| 226 |
liveuser |
754 |
|
| 3 |
liveuser |
755 |
#函式說明:
|
|
|
756 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现.
|
|
|
757 |
#回傳結果:
|
|
|
758 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
759 |
#$result["error"],錯誤訊息陣列
|
|
|
760 |
#$result["function"],當前函數執行的名稱
|
| 226 |
liveuser |
761 |
#$result["content"],base64UrlEncode编码後的內容。
|
| 3 |
liveuser |
762 |
#必填參數:
|
|
|
763 |
#$conf["input"],字串,需要編碼的字串.
|
|
|
764 |
$conf["jwt::base64UrlEncode"]["input"]=$sig;
|
|
|
765 |
#可省略參數:
|
|
|
766 |
#無
|
|
|
767 |
#參考資料:
|
|
|
768 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
769 |
#備註:
|
|
|
770 |
#無.
|
|
|
771 |
$base64UrlEncode=jwt::base64UrlEncode($conf["jwt::base64UrlEncode"]);
|
|
|
772 |
unset($conf["jwt::base64UrlEncode"]);
|
| 226 |
liveuser |
773 |
|
| 3 |
liveuser |
774 |
#如果加密失敗
|
|
|
775 |
if($base64UrlEncode["status"]==="false")
|
|
|
776 |
{
|
|
|
777 |
#設置執行失敗
|
|
|
778 |
$result["status"]="false";
|
| 226 |
liveuser |
779 |
|
| 3 |
liveuser |
780 |
#設置錯誤訊息
|
|
|
781 |
$result["error"]=$base64UrlEncode;
|
| 226 |
liveuser |
782 |
|
| 3 |
liveuser |
783 |
#回傳結果
|
|
|
784 |
return $result;
|
| 226 |
liveuser |
785 |
|
| 3 |
liveuser |
786 |
}#if end
|
| 226 |
liveuser |
787 |
|
| 3 |
liveuser |
788 |
#取得加密後的sig
|
|
|
789 |
$sig=$base64UrlEncode["content"];
|
| 226 |
liveuser |
790 |
|
| 3 |
liveuser |
791 |
}#if end
|
| 226 |
liveuser |
792 |
|
| 3 |
liveuser |
793 |
#回傳 JWT token
|
|
|
794 |
$result["content"]=$head.".".$body.".".$sig;
|
| 226 |
liveuser |
795 |
|
| 3 |
liveuser |
796 |
#設置執行正常
|
| 226 |
liveuser |
797 |
$result["status"]="true";
|
|
|
798 |
|
| 3 |
liveuser |
799 |
#回傳結果
|
| 226 |
liveuser |
800 |
return $result;
|
|
|
801 |
|
| 3 |
liveuser |
802 |
}#funcion makeJWT end
|
| 226 |
liveuser |
803 |
|
| 3 |
liveuser |
804 |
/*
|
|
|
805 |
#函式說明:
|
|
|
806 |
#檢查 JWT token 是否合法
|
|
|
807 |
#回傳結果:
|
|
|
808 |
#$result["status"],"true"代表為正確得JWT token,反之"false"代表不正確得JWT token.
|
|
|
809 |
#$result["content"],陣列
|
|
|
810 |
#$result["content"]["head"],jwt的head物件.
|
|
|
811 |
#$result["content"]["body"],jwt的body物件.
|
|
|
812 |
#$result["error"],錯誤訊息陣列.
|
|
|
813 |
#必填參數:
|
|
|
814 |
#$conf["token"],要驗證的Authorization header內容,亦即"Bearer jwt".
|
|
|
815 |
$conf["token"]="";
|
|
|
816 |
#$conf["key"],用來驗證簽章的加解密key.
|
|
|
817 |
$conf["key"]="";
|
|
|
818 |
#可省略參數:
|
|
|
819 |
#$conf["debug"],"false"代表不debug;"true"代表要debug.
|
|
|
820 |
#$conf["debug"]="true";
|
|
|
821 |
#參考資料:
|
|
|
822 |
#無.
|
|
|
823 |
#備註:
|
|
|
824 |
#無.
|
|
|
825 |
*/
|
|
|
826 |
public static function verifyJWT(&$conf){
|
| 226 |
liveuser |
827 |
|
| 3 |
liveuser |
828 |
#初始化要回傳的結果
|
|
|
829 |
$result=array();
|
| 226 |
liveuser |
830 |
|
| 3 |
liveuser |
831 |
#參數
|
|
|
832 |
$result["argu"]=$conf;
|
| 226 |
liveuser |
833 |
|
| 3 |
liveuser |
834 |
#檢查參數
|
|
|
835 |
#函式說明:
|
|
|
836 |
#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
|
|
|
837 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
838 |
#$reuslt["error"],執行不正常結束的錯訊息陣列.
|
|
|
839 |
#$result["function"],當前執行的函式名稱.
|
|
|
840 |
#$result["argu"],設置給予的參數.
|
|
|
841 |
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
|
|
|
842 |
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
|
|
|
843 |
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
|
|
|
844 |
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
|
|
|
845 |
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
|
|
|
846 |
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
|
|
|
847 |
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
|
|
|
848 |
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
|
|
|
849 |
#必填寫的參數:
|
|
|
850 |
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
|
|
|
851 |
$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
|
|
|
852 |
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
|
|
|
853 |
$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
|
|
|
854 |
#可以省略的參數:
|
|
|
855 |
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
|
|
|
856 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("token","key");
|
|
|
857 |
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
|
|
|
858 |
$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string","string");
|
|
|
859 |
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
|
|
|
860 |
#$conf["canBeEmptyString"]="false";
|
|
|
861 |
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
|
|
|
862 |
#$conf["canNotBeEmpty"]=array();
|
|
|
863 |
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
|
|
|
864 |
#$conf["canBeEmpty"]=array();
|
|
|
865 |
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
|
|
|
866 |
$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("debug");
|
|
|
867 |
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
|
|
|
868 |
$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("debug");
|
|
|
869 |
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
|
|
|
870 |
$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
|
|
|
871 |
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
|
|
|
872 |
$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("false");
|
|
|
873 |
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
|
|
|
874 |
#$conf["disallowAllSkipableVarIsEmpty"]="";
|
|
|
875 |
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
|
|
|
876 |
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
|
|
|
877 |
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
|
|
|
878 |
#$conf["arrayCountEqualCheck"][]=array();
|
|
|
879 |
#參考資料來源:
|
|
|
880 |
#array_keys=>http://php.net/manual/en/function.array-keys.php
|
|
|
881 |
$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
|
|
|
882 |
unset($conf["variableCheck::checkArguments"]);
|
| 226 |
liveuser |
883 |
|
| 3 |
liveuser |
884 |
#如果檢查參數失敗
|
|
|
885 |
if($checkArguments["status"]==="false"){
|
| 226 |
liveuser |
886 |
|
| 3 |
liveuser |
887 |
#設置執行失敗
|
|
|
888 |
$result["status"]="false";
|
| 226 |
liveuser |
889 |
|
| 3 |
liveuser |
890 |
#設置錯誤訊息
|
|
|
891 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
892 |
|
| 3 |
liveuser |
893 |
#回傳結果
|
|
|
894 |
return $result;
|
| 226 |
liveuser |
895 |
|
| 3 |
liveuser |
896 |
}#if end
|
| 226 |
liveuser |
897 |
|
| 3 |
liveuser |
898 |
#如果檢查參數不通過
|
|
|
899 |
if($checkArguments["passed"]==="false"){
|
| 226 |
liveuser |
900 |
|
| 3 |
liveuser |
901 |
#設置執行失敗
|
|
|
902 |
$result["status"]="false";
|
| 226 |
liveuser |
903 |
|
| 3 |
liveuser |
904 |
#設置錯誤訊息
|
|
|
905 |
$result["error"]=$checkArguments;
|
| 226 |
liveuser |
906 |
|
| 3 |
liveuser |
907 |
#回傳結果
|
|
|
908 |
return $result;
|
| 226 |
liveuser |
909 |
|
| 3 |
liveuser |
910 |
}#if end
|
| 226 |
liveuser |
911 |
|
| 3 |
liveuser |
912 |
#取得 token
|
|
|
913 |
$token=$conf["token"];
|
| 226 |
liveuser |
914 |
|
| 3 |
liveuser |
915 |
#取得 key
|
|
|
916 |
$key=$conf["key"];
|
| 226 |
liveuser |
917 |
|
| 3 |
liveuser |
918 |
#檢查是否為 JWT token
|
|
|
919 |
$isJWT=strpos($token,"Bearer ");
|
| 226 |
liveuser |
920 |
|
| 3 |
liveuser |
921 |
#如果是 JWT token
|
|
|
922 |
if($isJWT!==0){
|
| 226 |
liveuser |
923 |
|
| 3 |
liveuser |
924 |
#debug
|
|
|
925 |
if($conf["debug"]==="true"){
|
| 226 |
liveuser |
926 |
|
| 3 |
liveuser |
927 |
#設置錯誤訊息
|
|
|
928 |
$result["error"][]="只允許 JWT token";
|
| 226 |
liveuser |
929 |
|
| 3 |
liveuser |
930 |
}#if end
|
| 226 |
liveuser |
931 |
|
| 3 |
liveuser |
932 |
#設置檢查不通過
|
|
|
933 |
$result["status"]="false";
|
| 226 |
liveuser |
934 |
|
| 3 |
liveuser |
935 |
#只允許 JWT token
|
|
|
936 |
return $result;
|
| 226 |
liveuser |
937 |
|
| 3 |
liveuser |
938 |
}#if end
|
| 226 |
liveuser |
939 |
|
| 3 |
liveuser |
940 |
#剔除 "Bearer "
|
|
|
941 |
$token=substr($token,7);
|
| 226 |
liveuser |
942 |
|
| 3 |
liveuser |
943 |
#涵式說明:
|
|
|
944 |
#將固定格式的字串分開,並回傳分開的結果。
|
|
|
945 |
#回傳結果:
|
|
|
946 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
947 |
#$result["error"],錯誤訊息陣列
|
|
|
948 |
#$result["function"],當前執行的函數名稱.
|
|
|
949 |
#$result["argu"],使用的參數.
|
|
|
950 |
#$result["oriStr"],要分割的原始字串內容
|
|
|
951 |
#$result["dataArray"],爲分割好字串的陣列內容,$result["dataArray"][$i]爲第($i+1)段的內容。
|
|
|
952 |
#$result["dataCounts"],爲總共分成幾段
|
|
|
953 |
#$result["found"],是否有在$conf["stringIn"]找到$conf["spiltSymbol"],"true"代表有找到,"false"代表沒有找到.
|
|
|
954 |
#必填的參數:
|
|
|
955 |
#$conf["stringIn"],字串,要處理的字串.
|
|
|
956 |
$conf["stringProcess::spiltString"]["stringIn"]=$token;
|
|
|
957 |
#$conf["spiltSymbol"],字串,爲以哪個符號作爲分割.
|
|
|
958 |
$conf["stringProcess::spiltString"]["spiltSymbol"]=".";
|
|
|
959 |
#可省略參數:
|
|
|
960 |
#$conf["allowEmptyStr"],是否允許分割出來空字串,預設為"false"不允許;"true"代表允許.
|
|
|
961 |
$conf["stringProcess::spiltString"]["allowEmptyStr"]="false";
|
|
|
962 |
$spiltString=stringProcess::spiltString($conf["stringProcess::spiltString"]);
|
|
|
963 |
unset($conf["stringProcess::spiltString"]);
|
| 226 |
liveuser |
964 |
|
| 3 |
liveuser |
965 |
#如果分割字串失敗
|
|
|
966 |
if($spiltString["status"]==="false"){
|
| 226 |
liveuser |
967 |
|
| 3 |
liveuser |
968 |
#設置錯誤訊息
|
|
|
969 |
$result["error"]=$spiltString;
|
| 226 |
liveuser |
970 |
|
| 3 |
liveuser |
971 |
#設置檢查不通過
|
|
|
972 |
$result["status"]="false";
|
| 226 |
liveuser |
973 |
|
| 3 |
liveuser |
974 |
#只允許 JWT token
|
|
|
975 |
return $result;
|
| 226 |
liveuser |
976 |
|
| 3 |
liveuser |
977 |
}#if end
|
| 226 |
liveuser |
978 |
|
| 3 |
liveuser |
979 |
#如果沒有解成3段
|
|
|
980 |
if($spiltString["dataCounts"]!==3){
|
| 226 |
liveuser |
981 |
|
| 3 |
liveuser |
982 |
#設置錯誤訊息
|
|
|
983 |
$result["error"][]="JWT token 應該要透過 . 分成三段";
|
| 226 |
liveuser |
984 |
|
| 3 |
liveuser |
985 |
#設置檢查不通過
|
|
|
986 |
$result["status"]="false";
|
| 226 |
liveuser |
987 |
|
| 3 |
liveuser |
988 |
#只允許 JWT token
|
|
|
989 |
return $result;
|
| 226 |
liveuser |
990 |
|
| 3 |
liveuser |
991 |
}#if end
|
| 226 |
liveuser |
992 |
|
| 3 |
liveuser |
993 |
#取得header
|
|
|
994 |
$head=$spiltString["dataArray"][0];
|
| 226 |
liveuser |
995 |
|
| 3 |
liveuser |
996 |
#函式說明:
|
|
|
997 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现
|
|
|
998 |
#回傳結果:
|
|
|
999 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1000 |
#$result["error"],錯誤訊息陣列.
|
|
|
1001 |
#$result["function"],當前函數執行的名稱.
|
|
|
1002 |
#$result["content"],base64UrlEncode解碼後的內容.
|
|
|
1003 |
#必填參數:
|
|
|
1004 |
#conf["input"],字串,需要解码的字符串.
|
|
|
1005 |
$conf["jwt::base64UrlDecode"]["input"]=$head;
|
|
|
1006 |
#可省略參數:
|
|
|
1007 |
#無
|
|
|
1008 |
#參考資料:
|
|
|
1009 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
1010 |
#備註:
|
|
|
1011 |
#無.
|
|
|
1012 |
$base64UrlDecode=jwt::base64UrlDecode($conf["jwt::base64UrlDecode"]);
|
|
|
1013 |
unset($conf["jwt::base64UrlDecode"]);
|
| 226 |
liveuser |
1014 |
|
| 3 |
liveuser |
1015 |
#如果解密失敗失敗
|
|
|
1016 |
if($base64UrlDecode["status"]==="false"){
|
| 226 |
liveuser |
1017 |
|
| 3 |
liveuser |
1018 |
#設置錯誤訊息
|
|
|
1019 |
$result["error"]=$base64UrlDecode;
|
| 226 |
liveuser |
1020 |
|
| 3 |
liveuser |
1021 |
#設置檢查不通過
|
|
|
1022 |
$result["status"]="false";
|
| 226 |
liveuser |
1023 |
|
| 3 |
liveuser |
1024 |
#回傳結果
|
|
|
1025 |
return $result;
|
| 226 |
liveuser |
1026 |
|
| 3 |
liveuser |
1027 |
}#if end
|
| 226 |
liveuser |
1028 |
|
| 3 |
liveuser |
1029 |
#取得header物件
|
|
|
1030 |
$headObj=json_decode($base64UrlDecode["content"]);
|
| 226 |
liveuser |
1031 |
|
| 3 |
liveuser |
1032 |
#取得內文
|
|
|
1033 |
$body=$spiltString["dataArray"][1];
|
| 226 |
liveuser |
1034 |
|
| 3 |
liveuser |
1035 |
#函式說明:
|
|
|
1036 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现
|
|
|
1037 |
#回傳結果:
|
|
|
1038 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1039 |
#$result["error"],錯誤訊息陣列.
|
|
|
1040 |
#$result["function"],當前函數執行的名稱.
|
|
|
1041 |
#$result["content"],base64UrlEncode解碼後的內容.
|
|
|
1042 |
#必填參數:
|
|
|
1043 |
#conf["input"],字串,需要解码的字符串.
|
|
|
1044 |
$conf["jwt::base64UrlDecode"]["input"]=$body;
|
|
|
1045 |
#可省略參數:
|
|
|
1046 |
#無
|
|
|
1047 |
#參考資料:
|
|
|
1048 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
1049 |
#備註:
|
|
|
1050 |
#無.
|
|
|
1051 |
$base64UrlDecode=jwt::base64UrlDecode($conf["jwt::base64UrlDecode"]);
|
|
|
1052 |
unset($conf["jwt::base64UrlDecode"]);
|
| 226 |
liveuser |
1053 |
|
| 3 |
liveuser |
1054 |
#如果解密失敗失敗
|
|
|
1055 |
if($base64UrlDecode["status"]==="false"){
|
| 226 |
liveuser |
1056 |
|
| 3 |
liveuser |
1057 |
#設置錯誤訊息
|
|
|
1058 |
$result["error"]=$base64UrlDecode;
|
| 226 |
liveuser |
1059 |
|
| 3 |
liveuser |
1060 |
#設置檢查不通過
|
|
|
1061 |
$result["status"]="false";
|
| 226 |
liveuser |
1062 |
|
| 3 |
liveuser |
1063 |
#回傳結果
|
|
|
1064 |
return $result;
|
| 226 |
liveuser |
1065 |
|
| 3 |
liveuser |
1066 |
}#if end
|
| 226 |
liveuser |
1067 |
|
|
|
1068 |
#取得body物件
|
| 3 |
liveuser |
1069 |
$bodyObj=json_decode($base64UrlDecode["content"]);
|
| 226 |
liveuser |
1070 |
|
| 3 |
liveuser |
1071 |
#取得簽章
|
|
|
1072 |
$sign=$spiltString["dataArray"][2];
|
| 226 |
liveuser |
1073 |
|
| 3 |
liveuser |
1074 |
#驗證簽章
|
|
|
1075 |
#函式說明:
|
|
|
1076 |
#產生 HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现
|
|
|
1077 |
#回傳結果:
|
|
|
1078 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1079 |
#$result["error"],錯誤訊息陣列.
|
|
|
1080 |
#$result["function"],當前函數執行的名稱.
|
|
|
1081 |
#$result["content"],base64UrlEncode(header).".".base64UrlEncode(payload)的簽章
|
|
|
1082 |
#必填參數:
|
|
|
1083 |
#$conf["input"],string,为base64UrlEncode(header).".".base64UrlEncode(payload)
|
|
|
1084 |
$conf["jwt::signature"]["input"]=$head.".".$body;
|
|
|
1085 |
#$conf["key"],字串,金鑰.
|
|
|
1086 |
$conf["jwt::signature"]["key"]=$key;
|
|
|
1087 |
#參考資料:
|
|
|
1088 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
1089 |
#https://www.php.net/manual/en/function.hash-hmac.php
|
|
|
1090 |
#備註:
|
|
|
1091 |
#無.
|
|
|
1092 |
$signature=jwt::signature($conf["jwt::signature"]);
|
|
|
1093 |
unset($conf["jwt::signature"]);
|
| 226 |
liveuser |
1094 |
|
| 3 |
liveuser |
1095 |
#如果產生signature失敗
|
|
|
1096 |
if($signature["status"]==="false"){
|
| 226 |
liveuser |
1097 |
|
| 3 |
liveuser |
1098 |
#設置錯誤訊息
|
|
|
1099 |
$result["error"]=$signature;
|
| 226 |
liveuser |
1100 |
|
| 3 |
liveuser |
1101 |
#設置檢查不通過
|
|
|
1102 |
$result["status"]="false";
|
| 226 |
liveuser |
1103 |
|
| 3 |
liveuser |
1104 |
#回傳結果
|
|
|
1105 |
return $result;
|
| 226 |
liveuser |
1106 |
|
|
|
1107 |
}#if end
|
|
|
1108 |
|
| 3 |
liveuser |
1109 |
#取得簽章
|
|
|
1110 |
$expectedSign=$signature["content"];
|
| 226 |
liveuser |
1111 |
|
| 3 |
liveuser |
1112 |
#簽章要base64Url加密
|
|
|
1113 |
#函式說明:
|
|
|
1114 |
#base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现.
|
|
|
1115 |
#回傳結果:
|
|
|
1116 |
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
|
|
|
1117 |
#$result["error"],錯誤訊息陣列
|
|
|
1118 |
#$result["function"],當前函數執行的名稱
|
| 226 |
liveuser |
1119 |
#$result["content"],base64UrlEncode编码後的內容。
|
| 3 |
liveuser |
1120 |
#必填參數:
|
|
|
1121 |
#$conf["input"],字串,需要編碼的字串.
|
|
|
1122 |
$conf["jwt::base64UrlEncode"]["input"]=$expectedSign;
|
|
|
1123 |
#可省略參數:
|
|
|
1124 |
#無
|
|
|
1125 |
#參考資料:
|
|
|
1126 |
#https://zhuanlan.zhihu.com/p/94747540
|
|
|
1127 |
#備註:
|
|
|
1128 |
#無.
|
|
|
1129 |
$base64UrlEncode=jwt::base64UrlEncode($conf["jwt::base64UrlEncode"]);
|
| 226 |
liveuser |
1130 |
|
| 3 |
liveuser |
1131 |
#如加密失敗
|
|
|
1132 |
if($base64UrlEncode["status"]==="false"){
|
| 226 |
liveuser |
1133 |
|
| 3 |
liveuser |
1134 |
#設置錯誤訊息
|
|
|
1135 |
$result["error"]=$base64UrlEncode;
|
| 226 |
liveuser |
1136 |
|
| 3 |
liveuser |
1137 |
#設置檢查不通過
|
|
|
1138 |
$result["status"]="false";
|
| 226 |
liveuser |
1139 |
|
| 3 |
liveuser |
1140 |
#回傳結果
|
|
|
1141 |
return $result;
|
| 226 |
liveuser |
1142 |
|
|
|
1143 |
}#if end
|
|
|
1144 |
|
| 3 |
liveuser |
1145 |
#取得加密後的簽章
|
| 226 |
liveuser |
1146 |
$expectedSign=$base64UrlEncode["content"];
|
|
|
1147 |
|
| 3 |
liveuser |
1148 |
#預設未通過驗證
|
|
|
1149 |
$passed=false;
|
| 226 |
liveuser |
1150 |
|
| 3 |
liveuser |
1151 |
#如果簽證一致
|
|
|
1152 |
if($sign===$expectedSign){
|
| 226 |
liveuser |
1153 |
|
| 3 |
liveuser |
1154 |
#設置通過驗證
|
|
|
1155 |
$passed=true;
|
| 226 |
liveuser |
1156 |
|
| 3 |
liveuser |
1157 |
}#if end
|
| 226 |
liveuser |
1158 |
|
| 3 |
liveuser |
1159 |
#資料來源可信
|
| 226 |
liveuser |
1160 |
|
| 3 |
liveuser |
1161 |
#預設有限期為現今時間加一天
|
|
|
1162 |
$exp=time()+86400;
|
| 226 |
liveuser |
1163 |
|
| 3 |
liveuser |
1164 |
#如果有有效期限
|
|
|
1165 |
if(isset($bodyObj->exp)){
|
| 226 |
liveuser |
1166 |
|
| 3 |
liveuser |
1167 |
#有效期限
|
|
|
1168 |
$exp=$bodyObj->exp;
|
| 226 |
liveuser |
1169 |
|
| 3 |
liveuser |
1170 |
}#if end
|
| 226 |
liveuser |
1171 |
|
| 3 |
liveuser |
1172 |
#預設發證時間為現在.
|
|
|
1173 |
$iat=time();
|
| 226 |
liveuser |
1174 |
|
| 3 |
liveuser |
1175 |
#如果有發證的時間
|
|
|
1176 |
if(isset($bodyObj->iat)){
|
| 226 |
liveuser |
1177 |
|
| 3 |
liveuser |
1178 |
#發證的時間
|
|
|
1179 |
$iat=$bodyObj->iat;
|
| 226 |
liveuser |
1180 |
|
| 3 |
liveuser |
1181 |
}#if end
|
| 226 |
liveuser |
1182 |
|
| 3 |
liveuser |
1183 |
#如果現在時間大於過期時間
|
|
|
1184 |
if(time()>$exp){
|
| 226 |
liveuser |
1185 |
|
| 3 |
liveuser |
1186 |
#debug
|
|
|
1187 |
if($conf["debug"]==="true"){
|
| 226 |
liveuser |
1188 |
|
| 3 |
liveuser |
1189 |
#設置錯誤訊息
|
|
|
1190 |
$result["error"][]="有效期限過期";
|
| 226 |
liveuser |
1191 |
|
| 3 |
liveuser |
1192 |
}#if end
|
| 226 |
liveuser |
1193 |
|
| 3 |
liveuser |
1194 |
#設置檢查不通過
|
|
|
1195 |
$result["status"]="false";
|
| 226 |
liveuser |
1196 |
|
| 3 |
liveuser |
1197 |
#只允許 JWT token
|
|
|
1198 |
return $result;
|
| 226 |
liveuser |
1199 |
|
| 3 |
liveuser |
1200 |
}#if end
|
| 226 |
liveuser |
1201 |
|
| 3 |
liveuser |
1202 |
#如果過期
|
|
|
1203 |
if($exp<$iat){
|
| 226 |
liveuser |
1204 |
|
| 3 |
liveuser |
1205 |
#debug
|
|
|
1206 |
if($conf["debug"]==="true"){
|
| 226 |
liveuser |
1207 |
|
| 3 |
liveuser |
1208 |
#設置錯誤訊息
|
|
|
1209 |
$result["error"][]="簽章過期";
|
| 226 |
liveuser |
1210 |
|
| 3 |
liveuser |
1211 |
}#if end
|
| 226 |
liveuser |
1212 |
|
| 3 |
liveuser |
1213 |
#設置檢查不通過
|
|
|
1214 |
$result["status"]="false";
|
| 226 |
liveuser |
1215 |
|
| 3 |
liveuser |
1216 |
#只允許 JWT token
|
|
|
1217 |
return $result;
|
| 226 |
liveuser |
1218 |
|
| 3 |
liveuser |
1219 |
}#if end
|
| 226 |
liveuser |
1220 |
|
| 3 |
liveuser |
1221 |
#設置通過驗證
|
|
|
1222 |
$result["status"]="true";
|
| 226 |
liveuser |
1223 |
|
| 3 |
liveuser |
1224 |
#設置要回傳的內容
|
|
|
1225 |
$result["content"]=array("head"=>$headObj,"body"=>$bodyObj);
|
| 226 |
liveuser |
1226 |
|
| 3 |
liveuser |
1227 |
#回傳結果
|
|
|
1228 |
return $result;
|
| 226 |
liveuser |
1229 |
|
| 3 |
liveuser |
1230 |
}#function verifyJWT end
|
|
|
1231 |
|
|
|
1232 |
}#class jwt end
|
|
|
1233 |
|
|
|
1234 |
?>
|