(role int64)
| 13 | ) |
| 14 | |
| 15 | func UserTokenAuth(role int64) func(c *gin.Context) { |
| 16 | return func(c *gin.Context) { |
| 17 | token := c.Request.Header.Get("Authorization") |
| 18 | token = strings.Replace(token, "Bearer ", "", 1) |
| 19 | if token == "" { |
| 20 | token = c.Query("access_token") |
| 21 | } |
| 22 | if token == "" { |
| 23 | c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToResponse(nil)) |
| 24 | c.Abort() |
| 25 | return |
| 26 | } |
| 27 | user, tokenEid, err := HandleAnyTokenAuth(token, role) |
| 28 | if err != nil { |
| 29 | switch err.Error() { |
| 30 | case "token is expired": |
| 31 | c.JSON(http.StatusUnauthorized, model.TokenExpiredError.ToResponse(nil)) |
| 32 | case "token has invalid claims", "forbidden access": |
| 33 | c.JSON(http.StatusUnauthorized, model.ForbiddenError.ToResponse(nil)) |
| 34 | default: |
| 35 | c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToResponse(nil)) |
| 36 | } |
| 37 | |
| 38 | c.Abort() |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | setUserSession(c, user, tokenEid) |
| 43 | c.Next() |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func HandleAnyTokenAuth(token string, role int64) (user *model.User, tokenEid int64, err error) { |
| 48 | user, tokenEid, err = HandleTokenAuth(token, role) |
nothing calls this directly
no test coverage detected