Skip to content

Commit

Permalink
return error from Middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Ilario <filario@redhat.com>
  • Loading branch information
filariow committed Aug 29, 2024
1 parent 1ed6846 commit d7393c5
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
gocontext "context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -414,27 +413,20 @@ func (p *Proxy) ensureUserIsNotBanned() echo.MiddlewareFunc {
return next(ctx)
}

errorResponse := func(err *crterrors.Error) error {
ctx.Logger().Error(errs.Wrap(err, "user ban status check error"))
ctx.Response().Writer.Header().Set("Content-Type", "application/json")
ctx.Response().Writer.WriteHeader(int(err.Code))
return json.NewEncoder(ctx.Response().Writer).Encode(err.Status)
}

email := ctx.Get(context.EmailKey).(string)
if email == "" {
return errorResponse(crterrors.NewUnauthorizedError("unauthenticated request", "anonymous access is not allowed"))
return crterrors.NewUnauthorizedError("unauthenticated request", "anonymous access is not allowed")
}

// retrieve banned users
uu, err := p.app.InformerService().ListBannedUsersByEmail(email)
if err != nil {
return errorResponse(crterrors.NewInternalError(errs.New("unable to retrieve user"), "could not define ban status"))
return crterrors.NewInternalError(errs.New("unable to retrieve user"), "could not define ban status")
}

// if a matching Banned user is found, then user is banned
if len(uu) > 0 {
return errorResponse(crterrors.NewForbiddenError("user is banned", "user is banned"))
return crterrors.NewForbiddenError("user is banned", "user is banned")
}

// user is not banned
Expand Down

0 comments on commit d7393c5

Please sign in to comment.