MSP-437 implement authz middleware
This commit is contained in:
1 parent
e64a58e85a
commit
14995e91dd
2 files changed
+49
-27
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/integration-system/isp-kit/log"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/httperrors"
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
type Authorizer interface {
|
||||
Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error)
|
||||
}
|
||||
|
||||
func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
authData, err := ctx.GetAuthData()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "authorize: get auth data")
|
||||
}
|
||||
|
||||
ok, err := authorizer.Authorize(ctx.Context(), authData.ApplicationId, ctx.Endpoint())
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "authorize: authorize")
|
||||
}
|
||||
if !ok && ctx.IsAdminAuthenticated() {
|
||||
logger.Info(
|
||||
ctx.Context(),
|
||||
"bypassing unauthorized request because it has admin privilege",
|
||||
log.Int("applicationId", authData.ApplicationId),
|
||||
log.Int("adminId", ctx.AdminId()),
|
||||
)
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
if !ok {
|
||||
return httperrors.New(
|
||||
http.StatusForbidden,
|
||||
"endpoint is not allowed",
|
||||
errors.Errorf("authorize: endpoint '%s' is not allowed for application '%d'", ctx.Endpoint(), authData.ApplicationId),
|
||||
)
|
||||
}
|
||||
|
||||
return next.Handle(ctx)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type AdminAuthorize interface {
|
||||
Authorize(id int, path string) error
|
||||
}
|
||||
|
||||
func Authorize(adminAuthorize AdminAuthorize) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *Context) error {
|
||||
if !ctx.authenticated {
|
||||
return errors.WithMessage(domain.ErrAuthorize, "authentication is required")
|
||||
}
|
||||
|
||||
err := adminAuthorize.Authorize(ctx.AdminId, ctx.Path)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(domain.ErrAuthorize, "authorize admin: %v", err)
|
||||
}
|
||||
|
||||
return next.Handle(ctx)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user