package middlewares import ( "net/http" "strings" "github.com/spf13/viper" "github.com/uptrace/bunrouter" ) func (self *Handlers) CheckPresharedKey( next bunrouter.HandlerFunc, ) bunrouter.HandlerFunc { return func(w http.ResponseWriter, req bunrouter.Request) error { authHeader := strings.Split(req.Header.Get("Authorization"), " ") if len(authHeader) != 2 || authHeader[0] != "Bearer" { return HTTPError{ StatusCode: http.StatusUnauthorized, Message: "missing preshared key", } } if authHeader[1] != viper.GetString("preshared-key") { return HTTPError{ StatusCode: http.StatusUnauthorized, Message: "preshared key mismatched", } } return next(w, req) } }