jwt.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2020 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package utils
  4. import (
  5. "google.golang.org/grpc/status"
  6. "smart-supplier-management-gateway/errors"
  7. "strconv"
  8. "github.com/tidwall/gjson"
  9. "github.com/dgrijalva/jwt-go"
  10. "github.com/gin-gonic/gin"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "go.uber.org/zap"
  13. "github.com/jaryhe/gopkgs/jwtwrapper"
  14. )
  15. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  16. func GetJwtIdFromCtx(ctx *gin.Context) (int64, error) {
  17. // 从上下文获取信息
  18. value, ok := ctx.Get("claims")
  19. if !ok {
  20. logger.Error("func",
  21. zap.String("call", "ctx.Get"),
  22. zap.String("error", "no claims data"))
  23. return 0, errors.ParamsError
  24. }
  25. // 解析数据
  26. claims, ok := value.(*jwt.StandardClaims)
  27. if !ok {
  28. logger.Error("func",
  29. zap.String("call", "Claims assert"),
  30. zap.String("error", "NOT Claims type"))
  31. return 0, errors.ParamsError
  32. }
  33. // 类型转换
  34. id, _ := strconv.ParseInt(claims.Id, 10, 64)
  35. return id, nil
  36. }
  37. func GetTokenInfo(ctx *gin.Context) (string, int64, error) {
  38. value, ok := ctx.Get("claims")
  39. if !ok {
  40. logger.Error("func",
  41. zap.String("call", "ctx.Get"),
  42. zap.String("error", "no claims data"))
  43. return "", 0, errors.ParamsError
  44. }
  45. // 解析数据
  46. claims, ok := value.(*jwt.StandardClaims)
  47. if !ok {
  48. logger.Error("func",
  49. zap.String("call", "Claims assert"),
  50. zap.String("error", "NOT Claims type"))
  51. return "", 0, errors.ParamsError
  52. }
  53. id, _ := strconv.ParseInt(claims.Id, 10, 64)
  54. user := gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  55. return user, id, nil
  56. }
  57. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  58. func GetJwtProjectIdFromCtx(ctx *gin.Context) (int64, error) {
  59. // 从上下文获取信息
  60. value, ok := ctx.Get("claims")
  61. if !ok {
  62. logger.Error("func",
  63. zap.String("call", "ctx.Get"),
  64. zap.String("error", "no claims data"))
  65. return 0, errors.ParamsError
  66. }
  67. // 解析数据
  68. claims, ok := value.(*jwt.StandardClaims)
  69. if !ok {
  70. logger.Error("func",
  71. zap.String("call", "Claims assert"),
  72. zap.String("error", "NOT Claims type"))
  73. return 0, errors.ParamsError
  74. }
  75. // 类型转换
  76. id := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int()
  77. return id, nil
  78. }
  79. func EmailTokenVeriy(token string) (uid int64, projectId int64, name string, email string, err error) {
  80. // 解析token
  81. claims, err := jwtwrapper.ParseToken(token)
  82. if err != nil {
  83. switch err.(*jwt.ValidationError).Errors {
  84. case jwt.ValidationErrorExpired:
  85. return 0, 0, "", "", status.Error(10010, "凭据过期")
  86. default:
  87. return 0, 0, "", "", status.Error(10010, "凭据错误")
  88. }
  89. return 0, 0, "", "",status.Error(10010, "凭据错误")
  90. }
  91. name = gjson.GetBytes([]byte(claims.Subject), "user_name").String()
  92. email = gjson.GetBytes([]byte(claims.Subject), "email").String()
  93. if email == "" {
  94. return 0, 0, "", "", status.Error(10010, "邮箱为空")
  95. }
  96. uid, _ = strconv.ParseInt(claims.Id, 10, 64)
  97. projectId = gjson.GetBytes([]byte(claims.Subject), "project_id").Int()
  98. return uid, projectId, name, email, nil
  99. }