123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package controllers
- import (
- "crypto/md5"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- "hongze/hongze_open_api/models/custom"
- "hongze/hongze_open_api/models/tables/open_api_user"
- "hongze/hongze_open_api/utils"
- "math"
- "reflect"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- type BaseAuth struct {
- BaseCommon
- AdminWx *custom.AdminWx `description:"管理员信息"`
- Token string `description:"用户token"`
- StartSize int `description:"开始数量"`
- StartPage int `description:"开始页码"`
- PageSize int `description:"每页数量"`
- }
- func (c *BaseAuth) Prepare() {
-
- signData := make(map[string]string)
- method := c.Ctx.Input.Method()
- var pageSize, currentIndex int
- switch method {
- case "GET":
-
- params := c.Ctx.Request.URL.Query()
- signData = convertParam(params)
- pageSize, _ = c.GetInt("_page_size")
- currentIndex, _ = c.GetInt("_page")
- 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 {
- c.FailWithMessage(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 {
- c.FailWithMessage(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 {
-
-
- c.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err))
- return
- }
-
- signData = convertParamInterface(params)
-
-
-
- default:
- err := c.Ctx.Request.ParseForm()
- if err != nil {
- c.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err))
- return
- }
- params := c.Ctx.Request.Form
- signData = convertParam(params)
- }
- }
-
-
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
-
- if pageSize > utils.PageMaxSize {
- pageSize = utils.PageMaxSize
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- c.StartSize = startSize
- c.PageSize = pageSize
- c.StartPage = currentIndex
- ip := c.Ctx.Input.IP()
-
-
-
- err := checkSign(signData, ip)
- if err != nil {
- c.SignError(fmt.Sprintf("签名校验失败,%v", err))
- return
- }
- uri := c.Ctx.Input.URI()
- utils.FileLog.Info(fmt.Sprintf("URI:%s", uri))
- }
- 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 checkSign(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 := open_api_user.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.ApiLog.Println(fmt.Sprintf("签名校验异常,签名字符串:%v;服务端签名值:%v", signStr, sign))
- return errors.New("签名校验异常,请核实签名")
- }
- return nil
- }
|