|
@@ -1,12 +1,20 @@
|
|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
+ "crypto/md5"
|
|
|
"encoding/json"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
+ "github.com/beego/beego/v2/server/web"
|
|
|
+ "github.com/shopspring/decimal"
|
|
|
+ "math"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
-
|
|
|
- "github.com/beego/beego/v2/server/web"
|
|
|
+ "reflect"
|
|
|
+ "sort"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
|
|
|
"hongze/hz_data_api/models"
|
|
|
"hongze/hz_data_api/utils"
|
|
@@ -36,16 +44,9 @@ func (this *BaseAuthController) Prepare() {
|
|
|
fmt.Println("Url:", uri)
|
|
|
if method != "HEAD" {
|
|
|
if method == "POST" {
|
|
|
- authorization := this.Ctx.Input.Header("authorization")
|
|
|
- if authorization == "" {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
|
|
|
- this.StopRun()
|
|
|
- return
|
|
|
- }
|
|
|
- checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
|
|
|
- fmt.Println("checkAuthorization:", checkAuthorization)
|
|
|
- if authorization != checkAuthorization {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
|
|
|
+ ok, errMsg := checkSign(this)
|
|
|
+ if !ok {
|
|
|
+ this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: errMsg}, false, false)
|
|
|
this.StopRun()
|
|
|
return
|
|
|
}
|
|
@@ -61,6 +62,78 @@ func (this *BaseAuthController) Prepare() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func checkSign(c *BaseAuthController) (ok bool, errMsg string) {
|
|
|
+ method := c.Ctx.Input.Method()
|
|
|
+ signData := make(map[string]string)
|
|
|
+
|
|
|
+ switch method {
|
|
|
+ case "GET":
|
|
|
+
|
|
|
+ params := c.Ctx.Request.URL.Query()
|
|
|
+ signData = convertParam(params)
|
|
|
+ case "POST":
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ contentType := c.Ctx.Request.Header.Get("content-type")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ switch contentType {
|
|
|
+ case "multipart/form-data":
|
|
|
+
|
|
|
+ err := c.Ctx.Request.ParseMultipartForm(-int64(5 << 20))
|
|
|
+ if err != nil {
|
|
|
+ errMsg = fmt.Sprintf("获取参数失败,%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ params := c.Ctx.Request.Form
|
|
|
+ signData = convertParam(params)
|
|
|
+ case "application/x-www-form-urlencoded":
|
|
|
+ err := c.Ctx.Request.ParseForm()
|
|
|
+ if err != nil {
|
|
|
+ errMsg = fmt.Sprintf("获取参数失败,%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ params := c.Ctx.Request.Form
|
|
|
+ signData = convertParam(params)
|
|
|
+ case "application/json":
|
|
|
+
|
|
|
+ params := make(map[string]interface{})
|
|
|
+ err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶ms)
|
|
|
+ if err != nil {
|
|
|
+ errMsg = fmt.Sprintf("获取参数失败,%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ signData = convertParamInterface(params)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ default:
|
|
|
+ err := c.Ctx.Request.ParseForm()
|
|
|
+ if err != nil {
|
|
|
+ errMsg = fmt.Sprintf("获取参数失败,%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ params := c.Ctx.Request.Form
|
|
|
+ signData = convertParam(params)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ip := c.Ctx.Input.IP()
|
|
|
+ err := checkSignData(signData, ip)
|
|
|
+ if err != nil {
|
|
|
+ errMsg = fmt.Sprintf("签名校验失败,%v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ ok = true
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
func (c *BaseAuthController) ServeJSON(encoding ...bool) {
|
|
|
|
|
|
|
|
@@ -123,9 +196,139 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
|
|
|
if requestBody == "" {
|
|
|
requestBody = c.Ctx.Input.URI()
|
|
|
}
|
|
|
- apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
|
|
|
+ apiLog.Println("请求地址:", c.Ctx.Input.URI(), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
|
|
|
if coding {
|
|
|
content = []byte(utils.StringsToJSON(string(content)))
|
|
|
}
|
|
|
return c.Ctx.Output.Body(content)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+func convertParam(params map[string][]string) (signData map[string]string) {
|
|
|
+ signData = make(map[string]string)
|
|
|
+ for key := range params {
|
|
|
+ signData[key] = params[key][0]
|
|
|
+ }
|
|
|
+ return signData
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func convertParamInterface(params map[string]interface{}) (signData map[string]string) {
|
|
|
+ signData = make(map[string]string)
|
|
|
+ for key := range params {
|
|
|
+ val := ``
|
|
|
+
|
|
|
+
|
|
|
+ tmpVal := params[key]
|
|
|
+ switch reflect.TypeOf(tmpVal).Kind() {
|
|
|
+ case reflect.String:
|
|
|
+ val = fmt.Sprint(tmpVal)
|
|
|
+ case reflect.Int, reflect.Int16, reflect.Int64, reflect.Int32, reflect.Int8:
|
|
|
+ val = fmt.Sprint(tmpVal)
|
|
|
+ case reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint64:
|
|
|
+ val = fmt.Sprint(tmpVal)
|
|
|
+ case reflect.Bool:
|
|
|
+ val = fmt.Sprint(tmpVal)
|
|
|
+ case reflect.Float64:
|
|
|
+ decimalNum := decimal.NewFromFloat(tmpVal.(float64))
|
|
|
+ val = decimalNum.String()
|
|
|
+
|
|
|
+ case reflect.Float32:
|
|
|
+ decimalNum := decimal.NewFromFloat32(tmpVal.(float32))
|
|
|
+ val = decimalNum.String()
|
|
|
+ }
|
|
|
+ signData[key] = val
|
|
|
+ }
|
|
|
+ return signData
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func checkSignData(postData map[string]string, ip string) (err error) {
|
|
|
+ isSandbox := postData["is_sandbox"]
|
|
|
+
|
|
|
+ if utils.RunMode == "debug" && isSandbox != "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ appid := postData["appid"]
|
|
|
+ if appid == "" {
|
|
|
+ err = errors.New("参数异常,缺少appid")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ openApiUserInfo, tmpErr := models.GetByAppid(appid)
|
|
|
+ if tmpErr != nil {
|
|
|
+ if tmpErr.Error() == utils.ErrNoRow() {
|
|
|
+ err = errors.New("appid异常,请联系管理员")
|
|
|
+ } else {
|
|
|
+ err = errors.New("系统异常,请联系管理员")
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if openApiUserInfo == nil {
|
|
|
+ err = errors.New("系统异常,请联系管理员")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if openApiUserInfo.Ip != "" {
|
|
|
+ if !strings.Contains(openApiUserInfo.Ip, ip) {
|
|
|
+ err = errors.New(fmt.Sprintf("无权限访问该接口,ip:%v,请联系管理员", ip))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ownSign := postData["sign"]
|
|
|
+ if ownSign == "" {
|
|
|
+ err = errors.New("参数异常,缺少签名字符串")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if postData["nonce_str"] == "" {
|
|
|
+ err = errors.New("参数异常,缺少随机字符串")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if postData["timestamp"] == "" {
|
|
|
+ err = errors.New("参数异常,缺少时间戳")
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ timeUnix := time.Now().Unix()
|
|
|
+
|
|
|
+ timestamp, timeErr := strconv.ParseInt(postData["timestamp"], 10, 64)
|
|
|
+ if timeErr != nil {
|
|
|
+ err = errors.New("参数异常,时间戳格式异常")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if math.Abs(float64(timeUnix-timestamp)) > 300 {
|
|
|
+ err = errors.New("当前时间异常,请调整设备时间与北京时间一致")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var keys []string
|
|
|
+ for k := range postData {
|
|
|
+ if k != "sign" {
|
|
|
+ keys = append(keys, k)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ sort.Strings(keys)
|
|
|
+
|
|
|
+
|
|
|
+ var signStr string
|
|
|
+ for _, v := range keys {
|
|
|
+ signStr += v + "=" + postData[v] + "&"
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ sign := strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(signStr+"secret="+openApiUserInfo.Secret))))
|
|
|
+ if sign != ownSign {
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("签名校验异常,签名字符串:%v;服务端签名值:%v", signStr, sign))
|
|
|
+ return errors.New("签名校验异常,请核实签名")
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|