12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package middware
- import (
- "adm-data-gateway/errors"
- "crypto/md5"
- "encoding/hex"
- "net/http"
- "github.com/gin-gonic/gin"
- "google.golang.org/grpc/status"
- )
- func Sign() gin.HandlerFunc {
- return func(ctx *gin.Context) {
- appKey := ctx.GetHeader("appkey")
- s := status.New(10001, "内部服务错误")
- if appKey == "" {
- if v, ok := status.FromError(errors.AppKeyNotExistError); ok {
- s = v
- }
- ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
- ctx.Abort()
- return
- }
- sign := ctx.GetHeader("sign")
- if sign == "" {
- if v, ok := status.FromError(errors.SignNotExistError); ok {
- s = v
- }
- ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
- ctx.Abort()
- return
- }
- ts := ctx.GetHeader("ts")
- if ts == "" {
- if v, ok := status.FromError(errors.TimeStampNotExistError); ok {
- s = v
- }
- ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
- ctx.Abort()
- return
- }
- h := md5.New()
- h.Write([]byte(appKey + ts))
- if sign != hex.EncodeToString(h.Sum(nil)) {
- if v, ok := status.FromError(errors.SignNotCorrectError); ok {
- s = v
- }
- ctx.JSON(http.StatusOK, Response{s.Code(), s.Message()})
- ctx.Abort()
- return
- }
- ctx.Next()
- }
- }
|