MSP-437 implement admin authz middleware

This commit is contained in:
d1slike committed 2022-08-04 21:49:14 +03:00
1 parent 14995e91dd
commit 0367fa8ac7
1 file changed
+32
+32
View File
@@ -0,0 +1,32 @@
package middleware
import (
"net/http"
"github.com/pkg/errors"
"isp-gate-service/httperrors"
"isp-gate-service/request"
)
type AdminMethodStore interface {
IsInnerMethod(path string) bool
}
func AdminAuthorize(store AdminMethodStore) Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx *request.Context) error {
isInner := store.IsInnerMethod(ctx.Endpoint())
if !isInner {
return next.Handle(ctx)
}
if ctx.IsAdminAuthenticated() {
return next.Handle(ctx)
}
return httperrors.New(
http.StatusForbidden,
"admin authentication required",
errors.Errorf("admin authorization: admin authentication required for '%s'", ctx.Endpoint()),
)
})
}
}