mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-25 17:42:25 +01:00
29 lines
616 B
Go
29 lines
616 B
Go
package middlewares
|
|
|
|
import (
|
|
"crawlab/routes"
|
|
"crawlab/services"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
func AuthorizationMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
tokenStr := c.GetHeader("Authorization")
|
|
if c.Request.URL.Path == "/login" || (c.Request.URL.Path == "/users" && c.Request.Method == "PUT") {
|
|
c.Next()
|
|
} else {
|
|
_, err := services.CheckToken(tokenStr)
|
|
if err == nil {
|
|
c.Next()
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, routes.Response{
|
|
Status: "ok",
|
|
Message: "unauthorized",
|
|
Error: "unauthorized",
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|