jwt.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "smart-enterprise-management-gateway/errors"
  6. "strconv"
  7. "github.com/tidwall/gjson"
  8. "github.com/dgrijalva/jwt-go"
  9. "github.com/gin-gonic/gin"
  10. "github.com/jaryhe/gopkgs/logger"
  11. "go.uber.org/zap"
  12. "github.com/jaryhe/gopkgs/jwtwrapper"
  13. "google.golang.org/grpc/status"
  14. )
  15. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  16. func GetJwtIdFromCtx(ctx *gin.Context) (int64, string, 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. userName := gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  36. return id, userName, nil
  37. }
  38. func GetSupper(ctx *gin.Context) (bool, error) {
  39. value, ok := ctx.Get("claims")
  40. if !ok {
  41. logger.Error("func",
  42. zap.String("call", "ctx.Get"),
  43. zap.String("error", "no claims data"))
  44. return false, errors.ParamsError
  45. }
  46. // 解析数据
  47. claims, ok := value.(*jwt.StandardClaims)
  48. if !ok {
  49. logger.Error("func",
  50. zap.String("call", "Claims assert"),
  51. zap.String("error", "NOT Claims type"))
  52. return false, errors.ParamsError
  53. }
  54. supper := gjson.GetBytes(StrToBytes(claims.Subject), "supper").Bool()
  55. return supper, 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. }