auth.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package utils
  2. import (
  3. "gd_management_gateway/errors"
  4. "strconv"
  5. "time"
  6. "gd_management_gateway/common.in/cache"
  7. "gd_management_gateway/common.in/config"
  8. "github.com/dgrijalva/jwt-go"
  9. )
  10. type UtimesClaims struct {
  11. Jti string `json:"jti"`
  12. jwt.StandardClaims
  13. }
  14. type UserClaims struct {
  15. Uid int64 `json:"uid"`
  16. Level int64 `json:"level"`
  17. UserName string `json:"user_name"`
  18. jwt.StandardClaims
  19. }
  20. type UtimesIssuer int
  21. const (
  22. UserJWTKey = "be2bc479a23b6161c229b0a27359d868"
  23. )
  24. func ProductToken(uid, level int64, userName string) (string, error) {
  25. expired, _ := config.Conf.Cgi.Gateway.TokenExpired.Int64()
  26. claims := UserClaims{
  27. uid,
  28. level,
  29. userName,
  30. jwt.StandardClaims{
  31. ExpiresAt: time.Now().Unix() + expired, //3600*2, //过期时间
  32. Subject: strconv.FormatInt(uid, 10), //uid
  33. IssuedAt: time.Now().Unix(), //生成时间
  34. NotBefore: time.Now().Unix(),
  35. },
  36. }
  37. plain := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  38. return plain.SignedString([]byte(UserJWTKey))
  39. }
  40. func checkTokenFromRedisAndReproduct(tokenString string, token *jwt.Token) error {
  41. if clams, ok := token.Claims.(*UserClaims); ok {
  42. RedisSetEx(tokenString, "")
  43. err := RedisGet(tokenString)
  44. if err != nil {
  45. ProductToken(clams.Uid, clams.Level, clams.UserName)
  46. }
  47. return nil
  48. } else {
  49. return errors.UserTokenillegality
  50. }
  51. }
  52. func GetUserNameFromToken(tokenString string) string {
  53. parser := &jwt.Parser{SkipClaimsValidation:true}
  54. token, _ := parser.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
  55. return []byte(UserJWTKey), nil
  56. })
  57. //token, _ := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
  58. // return []byte(UserJWTKey), nil
  59. //})
  60. if clams, ok := token.Claims.(*UserClaims); ok {
  61. return clams.UserName
  62. } else {
  63. return ""
  64. }
  65. return ""
  66. }
  67. func ParseToken(tokenString string) (int64, int64, error) {
  68. token, err := jwt.ParseWithClaims(tokenString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
  69. return []byte(UserJWTKey), nil
  70. })
  71. rvalue := ""
  72. if !token.Valid {
  73. rvalue, _ = cache.Redis.Get(tokenString)
  74. if rvalue != tokenString {
  75. return 0, 0, errors.UserTokenExpire
  76. }
  77. }
  78. if err != nil && rvalue != tokenString {
  79. return 0, 0, errors.UserTokenillegality
  80. }
  81. if clams, ok := token.Claims.(*UserClaims); ok {
  82. expired, _ := config.Conf.Cgi.Gateway.TokenExpired.Int64()
  83. cache.Redis.SetEx(tokenString, expired, tokenString)
  84. return clams.Uid, clams.Level, nil
  85. } else {
  86. return 0, 0, errors.UserTokenillegality
  87. }
  88. }