Skip to content

Commit

Permalink
Added helper error function
Browse files Browse the repository at this point in the history
  • Loading branch information
kristinapathak committed May 8, 2019
1 parent 010ac65 commit 55e7140
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions bascule/basculehttp/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
authorization := request.Header.Get(c.headerName)
if len(authorization) == 0 {
err := errors.New("no authorization header")
logger.Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error())
c.onErrorResponse(MissingHeader, err)
c.error(logger, MissingHeader, "", err)
response.WriteHeader(http.StatusForbidden)
return
}

i := strings.IndexByte(authorization, ' ')
if i < 1 {
err := errors.New("unexpected authorization header value")
logger.Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error(),
"auth", authorization)
c.onErrorResponse(InvalidHeader, err)
c.error(logger, InvalidHeader, authorization, err)
response.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -55,19 +52,15 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
tf, supported := c.authorizations[key]
if !supported {
err := fmt.Errorf("key not supported: [%v]", key)
logger.Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error(),
"auth", authorization[i+1:])
c.onErrorResponse(KeyNotSupported, err)
c.error(logger, KeyNotSupported, authorization, err)
response.WriteHeader(http.StatusForbidden)
return
}

ctx := request.Context()
token, err := tf.ParseAndValidate(ctx, request, key, authorization[i+1:])
if err != nil {
logger.Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error(), "key", key,
"auth", authorization[i+1:])
c.onErrorResponse(ParseFailed, err)
c.error(logger, ParseFailed, authorization, err)
WriteResponse(response, http.StatusForbidden, err)
return
}
Expand All @@ -90,6 +83,11 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
})
}

func (c *constructor) error(logger bascule.Logger, e ErrorResponseReason, auth string, err error) {
logger.Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error(), "auth", auth)
c.onErrorResponse(e, err)
}

type COption func(*constructor)

func WithHeaderName(headerName string) COption {
Expand Down

0 comments on commit 55e7140

Please sign in to comment.