jwt.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package middleware
  2. import (
  3. "gd_management_gateway/errors"
  4. "fmt"
  5. "gd_management_gateway/common.in/jsonrpc2"
  6. dsignal "gd_management_gateway/common.in/signal"
  7. "gd_management_gateway/common.in/task"
  8. "gd_management_gateway/common.in/utils"
  9. "github.com/astaxie/beego/context"
  10. )
  11. type (
  12. JwtRequest struct {
  13. Token string `json:"token" required:"true" in:"header"`
  14. Uid string `json:"uid" required:"true" in:"header"`
  15. }
  16. )
  17. func JwtAuth(ctx *context.Context) {
  18. var req JwtRequest
  19. getParamsTask := func() error {
  20. if dsignal.ExitRecieved == true {
  21. return jsonrpc2.NewJsonError(10003, "服务停止中")
  22. }
  23. req.Uid = ctx.Input.Header("uid")
  24. req.Token = ctx.Input.Header("token")
  25. if req.Uid == "" || req.Token == "" {
  26. return errors.ArgsError
  27. }
  28. return nil
  29. }
  30. handleTokenCheck := func() error {
  31. uid, _, err := utils.ParseToken(req.Token)
  32. if err != nil {
  33. return err
  34. }
  35. if fmt.Sprintf("%d", uid) != req.Uid {
  36. return errors.UserTokenillegality
  37. }
  38. return nil
  39. }
  40. // 执行以上定义的任务
  41. task.Do(&task.PanicRecover{
  42. Ctx: ctx,
  43. LogID: ""}, ctx,
  44. getParamsTask, handleTokenCheck,
  45. )
  46. }