jwt.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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-system-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. const SYSTEMPHONELOGINKEY = "system_phone_login_"
  17. func GetKey(prefix, tail string) string {
  18. return fmt.Sprintf("%s%s", prefix, tail)
  19. }
  20. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  21. func GetJwtIdFromCtx(ctx *gin.Context) (int64, string, error) {
  22. // 从上下文获取信息
  23. value, ok := ctx.Get("claims")
  24. if !ok {
  25. logger.Error("func",
  26. zap.String("call", "ctx.Get"),
  27. zap.String("error", "no claims data"))
  28. return 0, "", errors.ParamsError
  29. }
  30. // 解析数据
  31. claims, ok := value.(*jwt.StandardClaims)
  32. if !ok {
  33. logger.Error("func",
  34. zap.String("call", "Claims assert"),
  35. zap.String("error", "NOT Claims type"))
  36. return 0, "", errors.ParamsError
  37. }
  38. // 类型转换
  39. id, _ := strconv.ParseInt(claims.Id, 10, 64)
  40. userName := gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  41. return id, userName, nil
  42. }
  43. type TokenInfo struct {
  44. Uid int64
  45. Cid int64
  46. GardenId int64
  47. UserName string
  48. GardenName string
  49. ShouldChooseGarden bool
  50. Phone string
  51. // 公司登录小区成功后,为true
  52. ByCompany bool
  53. // 公司登录小区验证接口,该字段为true
  54. IsCompanyEnter bool
  55. Super bool
  56. Codes []string
  57. SingleSignTime string
  58. GPermissionTime string
  59. UPermissionTime string
  60. }
  61. func GetSubjectValue(ctx *gin.Context) (info TokenInfo, err error) {
  62. ret := TokenInfo{}
  63. value, ok := ctx.Get("claims")
  64. if !ok {
  65. logger.Error("func",
  66. zap.String("call", "ctx.Get"),
  67. zap.String("error", "no claims data"))
  68. return ret, errors.ParamsError
  69. }
  70. // 解析数据
  71. claims, ok := value.(*jwt.StandardClaims)
  72. if !ok {
  73. logger.Error("func",
  74. zap.String("call", "Claims assert"),
  75. zap.String("error", "NOT Claims type"))
  76. return ret, errors.ParamsError
  77. }
  78. ret.Uid, _ = strconv.ParseInt(claims.Id, 10, 64)
  79. ret.Cid = gjson.GetBytes(StrToBytes(claims.Subject), "cid").Int()
  80. ret.GardenId = gjson.GetBytes(StrToBytes(claims.Subject), "garden_id").Int()
  81. ret.UserName = gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  82. ret.GardenName = gjson.GetBytes(StrToBytes(claims.Subject), "garden_name").String()
  83. ret.Phone = gjson.GetBytes(StrToBytes(claims.Subject), "phone").String()
  84. ret.IsCompanyEnter = gjson.GetBytes(StrToBytes(claims.Subject), "is_company_enter").Bool()
  85. ret.ByCompany = gjson.GetBytes(StrToBytes(claims.Subject), "by_company").Bool()
  86. ret.ShouldChooseGarden = gjson.GetBytes(StrToBytes(claims.Subject), "shood_choose_garden").Bool()
  87. ret.Super = gjson.GetBytes(StrToBytes(claims.Subject), "is_super_group").Bool()
  88. codes := gjson.GetBytes(StrToBytes(claims.Subject), "codes").Map()
  89. if len(codes) > 0 {
  90. for k, _ := range codes {
  91. ret.Codes = append(ret.Codes, k)
  92. }
  93. }
  94. return ret, nil
  95. }
  96. func EmailTokenVeriy(token string) (uid int64, projectId int64, name string, email string, err error) {
  97. // 解析token
  98. claims, err := jwtwrapper.ParseToken(token)
  99. if err != nil {
  100. switch err.(*jwt.ValidationError).Errors {
  101. case jwt.ValidationErrorExpired:
  102. return 0, 0, "", "", status.Error(10010, "凭据过期")
  103. default:
  104. return 0, 0, "", "", status.Error(10010, "凭据错误")
  105. }
  106. return 0, 0, "", "",status.Error(10010, "凭据错误")
  107. }
  108. name = gjson.GetBytes([]byte(claims.Subject), "user_name").String()
  109. email = gjson.GetBytes([]byte(claims.Subject), "email").String()
  110. if email == "" {
  111. return 0, 0, "", "", status.Error(10010, "邮箱为空")
  112. }
  113. uid, _ = strconv.ParseInt(claims.Id, 10, 64)
  114. projectId = gjson.GetBytes([]byte(claims.Subject), "project_id").Int()
  115. return uid, projectId, name, email, nil
  116. }