jwt.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "google.golang.org/grpc/status"
  6. "cp-organization-management-gateway/errors"
  7. "strconv"
  8. "github.com/tidwall/gjson"
  9. "github.com/dgrijalva/jwt-go"
  10. "github.com/gin-gonic/gin"
  11. "github.com/jaryhe/gopkgs/jwtwrapper"
  12. "github.com/jaryhe/gopkgs/logger"
  13. "go.uber.org/zap"
  14. )
  15. type TokenInfo struct {
  16. Uid int64
  17. Username string
  18. OrganizationCode string
  19. IsSuper bool
  20. }
  21. func GetTokeInfo(ctx *gin.Context)(TokenInfo, error) {
  22. // 从上下文获取信息
  23. ret := TokenInfo{}
  24. value, ok := ctx.Get("claims")
  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.IsSuper = gjson.GetBytes(StrToBytes(claims.Subject), "is_super").Bool()
  42. ret.Username = gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  43. ret.OrganizationCode = gjson.GetBytes(StrToBytes(claims.Subject), "organization_code").String()
  44. return ret, nil
  45. }
  46. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  47. func GetJwtIdFromCtx(ctx *gin.Context) (int64, string, error) {
  48. // 从上下文获取信息
  49. value, ok := ctx.Get("claims")
  50. if !ok {
  51. logger.Error("func",
  52. zap.String("call", "ctx.Get"),
  53. zap.String("error", "no claims data"))
  54. return 0, "", errors.ParamsError
  55. }
  56. // 解析数据
  57. claims, ok := value.(*jwt.StandardClaims)
  58. if !ok {
  59. logger.Error("func",
  60. zap.String("call", "Claims assert"),
  61. zap.String("error", "NOT Claims type"))
  62. return 0, "", errors.ParamsError
  63. }
  64. // 类型转换
  65. id, _ := strconv.ParseInt(claims.Id, 10, 64)
  66. userName := gjson.GetBytes(StrToBytes(claims.Subject), "user_name").String()
  67. return id, userName, nil
  68. }
  69. func GetOrganizationCode(ctx *gin.Context) (string, error) {
  70. value, ok := ctx.Get("claims")
  71. if !ok {
  72. logger.Error("func",
  73. zap.String("call", "ctx.Get"),
  74. zap.String("error", "no claims data"))
  75. return "", errors.ParamsError
  76. }
  77. // 解析数据
  78. claims, ok := value.(*jwt.StandardClaims)
  79. if !ok {
  80. logger.Error("func",
  81. zap.String("call", "Claims assert"),
  82. zap.String("error", "NOT Claims type"))
  83. return "", errors.ParamsError
  84. }
  85. code := gjson.GetBytes(StrToBytes(claims.Subject), "organization_code").String()
  86. return code, nil
  87. }
  88. // GetJwtIdFromCtx 从 gin 上下文中获取 id
  89. func GetJwtProjectIdFromCtx(ctx *gin.Context) (int64, error) {
  90. // 从上下文获取信息
  91. value, ok := ctx.Get("claims")
  92. if !ok {
  93. logger.Error("func",
  94. zap.String("call", "ctx.Get"),
  95. zap.String("error", "no claims data"))
  96. return 0, errors.ParamsError
  97. }
  98. // 解析数据
  99. claims, ok := value.(*jwt.StandardClaims)
  100. if !ok {
  101. logger.Error("func",
  102. zap.String("call", "Claims assert"),
  103. zap.String("error", "NOT Claims type"))
  104. return 0, errors.ParamsError
  105. }
  106. // 类型转换
  107. id := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int()
  108. return id, nil
  109. }
  110. func TmpTokenVeriy(token string) (int64, error) {
  111. // 解析token
  112. claims, err := jwtwrapper.ParseToken(token)
  113. if err != nil {
  114. switch err.(*jwt.ValidationError).Errors {
  115. case jwt.ValidationErrorExpired:
  116. return 0, status.Error(10010, "登录token错误")
  117. default:
  118. return 0, status.Error(10010, "登录token错误")
  119. }
  120. return 0, status.Error(10010, "登录token错误")
  121. }
  122. isTmp := gjson.GetBytes([]byte(claims.Subject), "tmp_token").Bool()
  123. if !isTmp {
  124. return 0, status.Error(10010, "登录token错误")
  125. }
  126. projectId, _ := strconv.ParseInt(claims.Id, 10, 64)
  127. return projectId, nil
  128. }
  129. func EmailTokenVeriy(token string) (uid int64, projectId int64, name string, email string, err error) {
  130. // 解析token
  131. claims, err := jwtwrapper.ParseToken(token)
  132. if err != nil {
  133. switch err.(*jwt.ValidationError).Errors {
  134. case jwt.ValidationErrorExpired:
  135. return 0, 0, "", "", status.Error(10010, "凭据过期")
  136. default:
  137. return 0, 0, "", "", status.Error(10010, "凭据错误")
  138. }
  139. return 0, 0, "", "",status.Error(10010, "凭据错误")
  140. }
  141. name = gjson.GetBytes([]byte(claims.Subject), "user_name").String()
  142. email = gjson.GetBytes([]byte(claims.Subject), "email").String()
  143. if email == "" {
  144. return 0, 0, "", "", status.Error(10010, "邮箱为空")
  145. }
  146. uid, _ = strconv.ParseInt(claims.Id, 10, 64)
  147. projectId = gjson.GetBytes([]byte(claims.Subject), "project_id").Int()
  148. return uid, projectId, name, email, nil
  149. }