diff --git a/middleware/admin_authorize.go b/middleware/admin_authorize.go new file mode 100644 index 0000000..9c4ee53 --- /dev/null +++ b/middleware/admin_authorize.go @@ -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()), + ) + }) + } +}