另客网go项目公用的代码库

auth.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package auth
  2. import (
  3. "fmt"
  4. "github.com/dgrijalva/jwt-go"
  5. "github.com/gin-gonic/gin"
  6. "net/http"
  7. )
  8. const (
  9. CtxRequestHeaderUserId = "user_id"
  10. ctxRequestHeaderAuthorization = "Authorization"
  11. ctxRequestCookieAuthorization = "ak"
  12. ctxRequestTokenExpired = "expired"
  13. )
  14. func Auth(authKey string, session Session) gin.HandlerFunc {
  15. return func(ctx *gin.Context) {
  16. var tokenFromCookie, tokenFromHeader string
  17. tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
  18. if err == http.ErrNoCookie {
  19. tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
  20. }
  21. if tokenFromHeader == "" {
  22. tokenFromHeader = "Bearer " + tokenFromCookie
  23. }
  24. if len(tokenFromHeader) < 8 {
  25. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed"})
  26. return
  27. }
  28. token, err := jwt.Parse(tokenFromHeader[7:], func(token *jwt.Token) (interface{}, error) {
  29. if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
  30. return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
  31. }
  32. return []byte(authKey), nil
  33. })
  34. if err != nil || !token.Valid {
  35. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed"})
  36. return
  37. }
  38. if !session.IsExistsJwtToken(token.Signature) {
  39. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token expired by server"})
  40. return
  41. }
  42. if mapClaims, ok := token.Claims.(jwt.MapClaims); ok {
  43. if expired, ok := mapClaims[ctxRequestTokenExpired].(float64); ok {
  44. if expired == 0 && tokenFromCookie == "" {
  45. if session.DeleteJwtToken(token.Raw) {
  46. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token expired"})
  47. } else {
  48. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, delete server token failed"})
  49. }
  50. return
  51. }
  52. if uid, ok := mapClaims[CtxRequestHeaderUserId].(float64); ok {
  53. ctx.Set(CtxRequestHeaderUserId, int64(uid))
  54. } else {
  55. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[CtxRequestHeaderUserId].(float64) error"})
  56. return
  57. }
  58. } else {
  59. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[ctxRequestTokenExpired].(float64) error"})
  60. return
  61. }
  62. } else {
  63. ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token.Claims.(jwt.MapClaims) error"})
  64. return
  65. }
  66. }
  67. }