auth.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package toolkit
  2. import (
  3. "errors"
  4. jwt "github.com/dgrijalva/jwt-go"
  5. )
  6. // AccessToken access token
  7. type AccessToken struct {
  8. ID string `json:"id"`
  9. Name string `json:"name"`
  10. Expires int64 `json:"expires_in"`
  11. }
  12. // CacheAccessToken cache access token
  13. type CacheAccessToken struct {
  14. ID int `json:"id"`
  15. Name string `json:"name"`
  16. Status int `json:"status"`
  17. Expires int64 `json:"expires_in"`
  18. Message string `json:"message"`
  19. }
  20. var (
  21. accessTokenKey []byte
  22. )
  23. // cacheKey cache token key
  24. func cacheKey(id string) string {
  25. return `user:user:` + id
  26. }
  27. // AccessTokenStorageCache storage CacheAccessToken to redis
  28. func AccessTokenStorageCache(id string, token CacheAccessToken) error {
  29. /*
  30. bytes, err := json.Marshal(token)
  31. if err != nil {
  32. return err
  33. }
  34. redis := NewRedisCache()
  35. return redis.Set(cacheKey(id), string(bytes), 0)
  36. // */
  37. return errors.New(`Not found cache class`)
  38. }
  39. // AccessTokenGetCache get CacheAccessToken from redis
  40. func AccessTokenGetCache(id string) (CacheAccessToken, error) {
  41. /*
  42. redis := NewRedisCache()
  43. data, err := redis.Get(cacheKey(id))
  44. var token CacheAccessToken
  45. if err != nil {
  46. return token, err
  47. }
  48. err = json.Unmarshal([]byte(data), &token)
  49. return token, err
  50. // */
  51. var token CacheAccessToken
  52. return token, errors.New(`Not found cache class`)
  53. }
  54. // SetAccessTokenKey set jwt key
  55. func SetAccessTokenKey(key string) {
  56. accessTokenKey = []byte(key)
  57. }
  58. // NewAccessToken new token
  59. func NewAccessToken(tok AccessToken) (string, error) {
  60. claims := jwt.MapClaims{
  61. "id": tok.ID,
  62. "name": tok.Name,
  63. "expires_in": tok.Expires}
  64. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  65. // Sign and get the complete encoded token as a string using the secret
  66. return token.SignedString(accessTokenKey)
  67. }
  68. // ParseAccessToken parse token
  69. func ParseAccessToken(accessToken string) (AccessToken, error) {
  70. var (
  71. tok AccessToken
  72. e error
  73. )
  74. token, err := jwt.Parse(
  75. accessToken,
  76. func(token *jwt.Token) (interface{}, error) {
  77. return accessTokenKey, nil
  78. })
  79. if token.Valid {
  80. claims := token.Claims.(jwt.MapClaims)
  81. if id, ok := claims["id"]; ok {
  82. tok.ID = id.(string)
  83. }
  84. if name, ok := claims["name"]; ok {
  85. tok.Name = name.(string)
  86. }
  87. if expires, ok := claims["expires_in"]; ok {
  88. tok.Expires = int64(expires.(float64))
  89. }
  90. e = nil
  91. } else if ve, ok := err.(*jwt.ValidationError); ok {
  92. if ve.Errors&jwt.ValidationErrorMalformed != 0 {
  93. //fmt.Println("That's not even a token")
  94. e = errors.New(`False authentication information`)
  95. } else if ve.Errors&
  96. (jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
  97. // Token is either expired or not active yet
  98. //fmt.Println("Timing is everything")
  99. e = errors.New(`Authentication information expired`)
  100. } else {
  101. e = errors.New(`Invalid authentication information`)
  102. //fmt.Println("Couldn't handle this token:", err)
  103. }
  104. }
  105. return tok, e
  106. }