Feat: add preshared key check
This commit is contained in:
32
middlewares/checkPresharedKey.go
Normal file
32
middlewares/checkPresharedKey.go
Normal file
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user