MSP-437 implement admin auth middleware
This commit is contained in:
1 parent
51d9cc0397
commit
e64a58e85a
1 file changed
+39
@@ -0,0 +1,39 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/httperrors"
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
const (
|
||||
adminAuthHeader = "x-auth-admin"
|
||||
)
|
||||
|
||||
type AdminAuthenticator interface {
|
||||
Authenticate(token string) (int, error)
|
||||
}
|
||||
|
||||
func AdminAuthenticate(auth AdminAuthenticator) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
adminToken := strings.TrimSpace(ctx.Request().Header.Get(adminAuthHeader))
|
||||
if adminToken == "" {
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
adminId, err := auth.Authenticate(adminToken)
|
||||
if err != nil {
|
||||
return httperrors.New(
|
||||
http.StatusUnauthorized,
|
||||
"invalid admin token",
|
||||
errors.WithMessage(err, "admin authenticate: authenticate"),
|
||||
)
|
||||
}
|
||||
ctx.AuthenticateAdmin(adminId, adminToken)
|
||||
return next.Handle(ctx)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user