jwt.go 3.8 KB

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