mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
refactor: extract token retrieval logic into GetAPITokenFromContext utility function
This commit is contained in:
@@ -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)
|
||||
|
||||
14
core/utils/token.go
Normal file
14
core/utils/token.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user