diff --git a/assets/templates/authorized/authorized_api.html b/assets/templates/authorized/authorized_api.html index 31de8f3..a9024f2 100644 --- a/assets/templates/authorized/authorized_api.html +++ b/assets/templates/authorized/authorized_api.html @@ -79,10 +79,10 @@ const hash_id = {{ .HashID }} - $("input#request_api").maxlength({ - warningClass: "badge badge-info", - limitReachedClass: "badge badge-warning" - }); + $("input#request_api").maxlength({ + warningClass: "badge badge-info", + limitReachedClass: "badge badge-warning" + }); // 加载列表页数据 getListData(); @@ -91,12 +91,28 @@ $.get("/api/authorized_list", {id: hash_id}, function (data) { // 成功 if (data.list.length > 0) { + var badgeMethodClass = ""; + $.each(data.list, function (index, value) { + if (value.method === "GET") { + badgeMethodClass = "badge-primary"; + } else if (value.method === "POST") { + badgeMethodClass = "badge-success"; + } else if (value.method === "DELETE") { + badgeMethodClass = "badge-danger"; + } else if (value.method === "PUT") { + badgeMethodClass = "badge-yellow"; + } else if (value.method === "PATCH") { + badgeMethodClass = "badge-cyan"; + } else { + badgeMethodClass = "badge-dark"; + } + const p = '
\n' + '' + - '\n' + + '\n' + '\n' + - '' + value.method + '\n' + value.api + '' + value.method + '\n' + value.api ; $(".apis").append(p); diff --git a/assets/templates/authorized/authorized_demo.html b/assets/templates/authorized/authorized_demo.html new file mode 100644 index 0000000..6a63f70 --- /dev/null +++ b/assets/templates/authorized/authorized_demo.html @@ -0,0 +1,184 @@ + + +
+ + + + + + + + +1. 新增调用方,输入调用方标识、调用方对接人、备注等信息;
+2. 授权调用方可调用的接口;
+3. 查看详情,将调用方的 KEY、SECRET 发给调用方;
1. 基于 HTTP Header 中的两个参数 Authorization、Date 存储签名信息,Authorization 存储签名信息,格式:调用方 KEY + 空格分隔符 + 摘要(加密串),例如:
Authorization:blog MjJjMDE1MWFkZjMwOWFmYjFlNzViNDFjYjYwMWFlMmM=+
2. Date 存储时间信息,格式:GMT 格林尼治标准时间,使用 Asia/Shanghai 时区,例如;
Date:Sat, 03 Apr 2021 10:14:43 GMT+
+func New(key, secret string, ttl time.Duration) Signature {
+ return &signature{
+ key: key,
+ secret: secret,
+ ttl: ttl,
+ }
+}
+
+// Generate
+// path 请求的路径 (不附带 querystring)
+func (s *signature) Generate(path string, method string, params url.Values) (authorization, date string, err error) {
+ if path == "" {
+ err = errors.New("path required")
+ return
+ }
+
+ if method == "" {
+ err = errors.New("method required")
+ return
+ }
+
+ methodName := strings.ToUpper(method)
+ if !methods[methodName] {
+ err = errors.New("method param error")
+ return
+ }
+
+ // Date
+ date = time_parse.GMTLayoutString()
+
+ // Encode() 方法中自带 sorted by key
+ sortParamsEncode := params.Encode()
+
+ // 加密字符串规则
+ buffer := bytes.NewBuffer(nil)
+ buffer.WriteString(path)
+ buffer.WriteString(delimiter)
+ buffer.WriteString(methodName)
+ buffer.WriteString(delimiter)
+ buffer.WriteString(sortParamsEncode)
+ buffer.WriteString(delimiter)
+ buffer.WriteString(date)
+
+ // 对数据进行 hmac 加密,并进行 base64 encode
+ hash := hmac.New(sha256.New, []byte(s.secret))
+ hash.Write(buffer.Bytes())
+ digest := base64.StdEncoding.EncodeToString(hash.Sum(nil))
+
+ authorization = fmt.Sprintf("%s %s", s.key, digest)
+ return
+}
+
+// 模拟数据
+const (
+ key = "blog"
+ secret = "i1ydX9RtHyuJTrw7frcu"
+ ttl = time.Minute * 10
+)
+
+func TestSignature_Generate(t *testing.T) {
+ path := "/echo"
+ method := "POST"
+
+ params := url.Values{}
+ params.Add("a", "a1")
+ params.Add("d", "d1")
+ params.Add("c", "c1")
+
+ authorization, date, err := New(key, secret, ttl).Generate(path, method, params)
+ t.Log("authorization:", authorization)
+ t.Log("date:", date)
+ t.Log("err:", err)
+}
+
+
+// 模拟数据
+$key = "blog";
+$secret = "i1ydX9RtHyuJTrw7frcu";
+
+$path = "/echo";
+$method = "POST";
+
+$params['a'] = "a1";
+$params['d'] = "d1";
+$params['c'] = "c1";
+
+// 对 params key 进行排序
+ksort($params);
+
+// 对 sortParams 进行 Encode
+$sortParamsEncode = http_build_query($params);
+
+// GMT 格林尼治标准时间,使用 Asia/Shanghai 时区
+$date = gmdate('D, d M Y H:i:s T',time() + 8*3600);
+
+// 加密字符串规则
+$encryptStr = $path."|".strtoupper($method)."|".$sortParamsEncode."|".$date;
+
+// 对数据进行 sha256 加密,并进行 base64 encode
+$digest = base64_encode(hash_hmac("sha256", $encryptStr, $secret, true));
+
+$authorization = $key." ".$digest;
+
+echo "authorization:{$authorization}";
+echo "---";
+echo "date:{$date}";
+
+ 推荐使用: ELK 组件 ,本功能仅仅是读取文本进行展示。
仅展示最新的 100 条日志。{{$value.Msg}}
+
+ {{else}}
+
+ {{$value.Level}}
+ {{$value.Time}}
+ {{$value.Method}}
+ {{$value.HTTPCode}}
+ {{$value.TraceID}}
+ {{$value.Path}}
+
+ {{end}}
+
+
+
+ {{$value.Content}}
+