jwt.go 5.1 KB

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