diff --git a/core/middlewares/auth.go b/core/middlewares/auth.go index f7621afc..bb4876ef 100644 --- a/core/middlewares/auth.go +++ b/core/middlewares/auth.go @@ -9,7 +9,6 @@ import ( "github.com/crawlab-team/crawlab/core/utils" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" - "strings" ) func AuthorizationMiddleware() gin.HandlerFunc { @@ -28,10 +27,7 @@ func AuthorizationMiddleware() gin.HandlerFunc { } // token string - tokenStr := c.GetHeader("Authorization") - if strings.HasPrefix(tokenStr, "Bearer ") { - tokenStr = strings.Replace(tokenStr, "Bearer ", "", 1) - } + tokenStr := utils.GetAPITokenFromContext(c) // validate token u, err := userSvc.CheckToken(tokenStr) diff --git a/core/utils/token.go b/core/utils/token.go new file mode 100644 index 00000000..77a2d839 --- /dev/null +++ b/core/utils/token.go @@ -0,0 +1,14 @@ +package utils + +import ( + "github.com/gin-gonic/gin" + "strings" +) + +func GetAPITokenFromContext(c *gin.Context) string { + tokenStr := c.GetHeader("Authorization") + if strings.HasPrefix(tokenStr, "Bearer ") { + tokenStr = strings.Replace(tokenStr, "Bearer ", "", 1) + } + return tokenStr +}