You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.8 KiB

  1. package middleware
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/dgrijalva/jwt-go"
  6. "github.com/gin-gonic/gin"
  7. uuid "github.com/satori/go.uuid"
  8. "main/controller/servers"
  9. "main/init/qmsql"
  10. "time"
  11. )
  12. type SqlRes struct {
  13. Path string
  14. AuthorityId string
  15. ApiId uint
  16. Id uint
  17. }
  18. func JWTAuth() gin.HandlerFunc {
  19. return func(c *gin.Context) {
  20. // 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localSstorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
  21. token := c.Request.Header.Get("x-token")
  22. if token == "" {
  23. servers.ReportFormat(c, false, "未登录或非法访问", gin.H{
  24. "reload": true,
  25. })
  26. c.Abort()
  27. return
  28. }
  29. j := NewJWT()
  30. // parseToken 解析token包含的信息
  31. claims, err := j.ParseToken(token)
  32. if err != nil {
  33. if err == TokenExpired {
  34. servers.ReportFormat(c, false, "授权已过期", gin.H{
  35. "reload": true,
  36. })
  37. c.Abort()
  38. return
  39. }
  40. servers.ReportFormat(c, false, err.Error(), gin.H{
  41. "reload": true,
  42. })
  43. c.Abort()
  44. return
  45. }
  46. var sqlRes SqlRes
  47. row := qmsql.DEFAULTDB.Raw("SELECT apis.path,api_authorities.authority_id,api_authorities.api_id,apis.id FROM apis INNER JOIN api_authorities ON api_authorities.api_id = apis.id WHERE apis.path = ? AND api_authorities.authority_id = ?", c.Request.RequestURI, claims.AuthorityId)
  48. err = row.Scan(&sqlRes).Error
  49. if fmt.Sprintf("%v", err) == "record not found" {
  50. servers.ReportFormat(c, false, "没有Api操作权限", gin.H{})
  51. c.Abort()
  52. return
  53. }
  54. c.Set("claims", claims)
  55. }
  56. }
  57. type JWT struct {
  58. SigningKey []byte
  59. }
  60. var (
  61. TokenExpired error = errors.New("Token is expired")
  62. TokenNotValidYet error = errors.New("Token not active yet")
  63. TokenMalformed error = errors.New("That's not even a token")
  64. TokenInvalid error = errors.New("Couldn't handle this token:")
  65. SignKey string = "qmPlus"
  66. )
  67. type CustomClaims struct {
  68. UUID uuid.UUID
  69. ID uint
  70. NickName string
  71. AuthorityId string
  72. jwt.StandardClaims
  73. }
  74. func NewJWT() *JWT {
  75. return &JWT{
  76. []byte(GetSignKey()),
  77. }
  78. }
  79. //获取token
  80. func GetSignKey() string {
  81. return SignKey
  82. }
  83. // 这是SignKey
  84. func SetSignKey(key string) string {
  85. SignKey = key
  86. return SignKey
  87. }
  88. //创建一个token
  89. func (j *JWT) CreateToken(claims CustomClaims) (string, error) {
  90. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  91. return token.SignedString(j.SigningKey)
  92. }
  93. //解析 token
  94. func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error) {
  95. token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (i interface{}, e error) {
  96. return j.SigningKey, nil
  97. })
  98. if err != nil {
  99. if ve, ok := err.(*jwt.ValidationError); ok {
  100. if ve.Errors&jwt.ValidationErrorMalformed != 0 {
  101. return nil, TokenMalformed
  102. } else if ve.Errors&jwt.ValidationErrorExpired != 0 {
  103. // Token is expired
  104. return nil, TokenExpired
  105. } else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
  106. return nil, TokenNotValidYet
  107. } else {
  108. return nil, TokenInvalid
  109. }
  110. }
  111. }
  112. if token != nil {
  113. if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
  114. return claims, nil
  115. }
  116. return nil, TokenInvalid
  117. } else {
  118. return nil, TokenInvalid
  119. }
  120. }
  121. // 更新token
  122. func (j *JWT) RefreshToken(tokenString string) (string, error) {
  123. jwt.TimeFunc = func() time.Time {
  124. return time.Unix(0, 0)
  125. }
  126. token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
  127. return j.SigningKey, nil
  128. })
  129. if err != nil {
  130. return "", err
  131. }
  132. if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
  133. jwt.TimeFunc = time.Now
  134. claims.StandardClaims.ExpiresAt = time.Now().Add(1 * time.Hour).Unix()
  135. return j.CreateToken(*claims)
  136. }
  137. return "", TokenInvalid
  138. }