Files
crawlab/core/middlewares/filer_auth.go
2024-06-14 15:42:50 +08:00

35 lines
710 B
Go

package middlewares
import (
"github.com/crawlab-team/crawlab/core/errors"
"github.com/crawlab-team/crawlab/core/utils"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func FilerAuthorizationMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// auth key
authKey := c.GetHeader("Authorization")
// server auth key
svrAuthKey := viper.GetString("fs.filer.authKey")
// skip to next if no server auth key is provided
if svrAuthKey == "" {
c.Next()
return
}
// validate
if authKey != svrAuthKey {
// validation failed, return error response
utils.HandleErrorUnauthorized(c, errors.ErrorHttpUnauthorized)
return
}
// validation success
c.Next()
}
}