jwt.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "fmt"
  6. "google.golang.org/grpc/status"
  7. "property-device-gateway/errors"
  8. "strconv"
  9. "github.com/tidwall/gjson"
  10. "github.com/dgrijalva/jwt-go"
  11. "github.com/gin-gonic/gin"
  12. "git.getensh.com/common/gopkgs/jwtwrapper"
  13. "git.getensh.com/common/gopkgs/logger"
  14. "go.uber.org/zap"
  15. )
  16. type TokenInfo struct {
  17. Uid int64
  18. User string
  19. }
  20. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  21. func GetJwtTokenFromCtx(ctx *gin.Context) (TokenInfo, error) {
  22. // 从上下文获取信息
  23. value, ok := ctx.Get("claims")
  24. ret := TokenInfo{}
  25. if !ok {
  26. logger.Error("func",
  27. zap.String("call", "ctx.Get"),
  28. zap.String("error", "no claims data"))
  29. return ret, errors.ParamsError
  30. }
  31. // 解析数据
  32. claims, ok := value.(*jwt.StandardClaims)
  33. if !ok {
  34. logger.Error("func",
  35. zap.String("call", "Claims assert"),
  36. zap.String("error", "NOT Claims type"))
  37. return ret, errors.ParamsError
  38. }
  39. // 类型转换
  40. ret.Uid, _ = strconv.ParseInt(claims.Id, 10, 64)
  41. ret.User = gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  42. return ret, nil
  43. }
  44. func GetSubjectValue(ctx *gin.Context) (bool, int64, error) {
  45. value, ok := ctx.Get("claims")
  46. if !ok {
  47. logger.Error("func",
  48. zap.String("call", "ctx.Get"),
  49. zap.String("error", "no claims data"))
  50. return false, 0, errors.ParamsError
  51. }
  52. // 解析数据
  53. claims, ok := value.(*jwt.StandardClaims)
  54. if !ok {
  55. logger.Error("func",
  56. zap.String("call", "Claims assert"),
  57. zap.String("error", "NOT Claims type"))
  58. return false, 0, errors.ParamsError
  59. }
  60. projectUser := gjson.GetBytes(StrToBytes(claims.Subject), "project_user").Bool()
  61. projectId := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int()
  62. fmt.Printf("projectid:%v\n", projectId)
  63. return projectUser, projectId, nil
  64. }
  65. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  66. func GetJwtProjectIdFromCtx(ctx *gin.Context) (int64, error) {
  67. // 从上下文获取信息
  68. value, ok := ctx.Get("claims")
  69. if !ok {
  70. logger.Error("func",
  71. zap.String("call", "ctx.Get"),
  72. zap.String("error", "no claims data"))
  73. return 0, errors.ParamsError
  74. }
  75. // 解析数据
  76. claims, ok := value.(*jwt.StandardClaims)
  77. if !ok {
  78. logger.Error("func",
  79. zap.String("call", "Claims assert"),
  80. zap.String("error", "NOT Claims type"))
  81. return 0, errors.ParamsError
  82. }
  83. // 类型转换
  84. id := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int()
  85. return id, nil
  86. }
  87. func TmpTokenVeriy(token string) (int64, error) {
  88. // 解析token
  89. claims, err := jwtwrapper.ParseToken(token)
  90. if err != nil {
  91. switch err.(*jwt.ValidationError).Errors {
  92. case jwt.ValidationErrorExpired:
  93. return 0, status.Error(10010, "登录token错误")
  94. default:
  95. return 0, status.Error(10010, "登录token错误")
  96. }
  97. return 0, status.Error(10010, "登录token错误")
  98. }
  99. isTmp := gjson.GetBytes([]byte(claims.Subject), "tmp_token").Bool()
  100. if !isTmp {
  101. return 0, status.Error(10010, "登录token错误")
  102. }
  103. projectId, _ := strconv.ParseInt(claims.Id, 10, 64)
  104. return projectId, nil
  105. }
  106. func EmailTokenVeriy(token string) (uid int64, projectId int64, name string, email string, err error) {
  107. // 解析token
  108. claims, err := jwtwrapper.ParseToken(token)
  109. if err != nil {
  110. switch err.(*jwt.ValidationError).Errors {
  111. case jwt.ValidationErrorExpired:
  112. return 0, 0, "", "", status.Error(10010, "凭据过期")
  113. default:
  114. return 0, 0, "", "", status.Error(10010, "凭据错误")
  115. }
  116. return 0, 0, "", "",status.Error(10010, "凭据错误")
  117. }
  118. name = gjson.GetBytes([]byte(claims.Subject), "user_name").String()
  119. email = gjson.GetBytes([]byte(claims.Subject), "email").String()
  120. if email == "" {
  121. return 0, 0, "", "", status.Error(10010, "邮箱为空")
  122. }
  123. uid, _ = strconv.ParseInt(claims.Id, 10, 64)
  124. projectId = gjson.GetBytes([]byte(claims.Subject), "project_id").Int()
  125. return uid, projectId, name, email, nil
  126. }