jwt.go 4.2 KB

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