diff --git a/http/handlers_test.go b/http/handlers_test.go index f1192fe..b4fdaf5 100644 --- a/http/handlers_test.go +++ b/http/handlers_test.go @@ -361,8 +361,8 @@ func TestErrorHandler(t *testing.T) { mockRespWriter.EXPECT().Write([]byte(err.Message)).Times(1) ErrorHandlerNoLog()(context.Background(), err, mockRespWriter) }) - t.Run("ClientError", func(t *testing.T) { - var mockError = mock.NewClientError(mockCtrl) + t.Run("DetailedError", func(t *testing.T) { + var mockError = mock.NewDetailedError(mockCtrl) var status = 403 var message = "error.message" mockError.EXPECT().Status().Return(status) diff --git a/middleware/authentication.go b/middleware/authentication.go index 0dcbc17..125138a 100644 --- a/middleware/authentication.go +++ b/middleware/authentication.go @@ -22,45 +22,13 @@ import ( func MakeHTTPBasicAuthenticationMW(passwordToMatch string, logger log.Logger) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - var authorizationHeader = req.Header.Get("Authorization") var ctx = context.TODO() - - if authorizationHeader == "" { - logger.Info(ctx, "msg", "Authorization error: Missing Authorization header") - httpErrorHandler(ctx, http.StatusForbidden, errors.New(errorhandler.MsgErrMissingParam+"."+errorhandler.AuthHeader), w) - return - } - - var regexpBasicAuth = `^[Bb]asic (.+)$` - var r = regexp.MustCompile(regexpBasicAuth) - var match = r.FindStringSubmatch(authorizationHeader) - if match == nil { - logger.Info(ctx, "msg", "Authorization error: Missing basic token") - httpErrorHandler(ctx, http.StatusForbidden, errors.New(errorhandler.MsgErrMissingParam+"."+errorhandler.BasicToken), w) - return - } - - // Decode base 64 - decodedToken, err := base64.StdEncoding.DecodeString(match[1]) - + var username, password, err = extractBasicCredentials(ctx, req.Header.Get("Authorization"), logger) if err != nil { - logger.Info(ctx, "msg", "Authorization error: Invalid base64 token") - httpErrorHandler(ctx, http.StatusForbidden, errors.New(errorhandler.MsgErrInvalidParam+"."+errorhandler.Token), w) + httpErrorHandler(ctx, http.StatusForbidden, err, w) return } - // Extract username & password values - var tokenSubparts = strings.Split(string(decodedToken), ":") - - if len(tokenSubparts) != 2 { - logger.Info(ctx, "msg", "Authorization error: Invalid token format (username:password)") - httpErrorHandler(ctx, http.StatusForbidden, errors.New(errorhandler.MsgErrInvalidParam+"."+errorhandler.Token), w) - return - } - - var username = tokenSubparts[0] - var password = tokenSubparts[1] - ctx = context.WithValue(req.Context(), cs.CtContextUsername, username) // Check password match @@ -75,6 +43,39 @@ func MakeHTTPBasicAuthenticationMW(passwordToMatch string, logger log.Logger) fu } } +func extractBasicCredentials(ctx context.Context, authorizationHeader string, logger log.Logger) (string, string, error) { + if authorizationHeader == "" { + logger.Info(ctx, "msg", "Authorization error: Missing Authorization header") + return "", "", errors.New(errorhandler.MsgErrMissingParam + "." + errorhandler.AuthHeader) + } + + var regexpBasicAuth = `^[Bb]asic (.+)$` + var r = regexp.MustCompile(regexpBasicAuth) + var match = r.FindStringSubmatch(authorizationHeader) + if match == nil { + logger.Info(ctx, "msg", "Authorization error: Missing basic token") + return "", "", errors.New(errorhandler.MsgErrMissingParam + "." + errorhandler.BasicToken) + } + + // Decode base 64 + decodedToken, err := base64.StdEncoding.DecodeString(match[1]) + + if err != nil { + logger.Info(ctx, "msg", "Authorization error: Invalid base64 token") + return "", "", errors.New(errorhandler.MsgErrInvalidParam + "." + errorhandler.Token) + } + + // Extract username & password values + var tokenSubparts = strings.Split(string(decodedToken), ":") + + if len(tokenSubparts) != 2 { + logger.Info(ctx, "msg", "Authorization error: Invalid token format (username:password)") + return "", "", errors.New(errorhandler.MsgErrInvalidParam + "." + errorhandler.Token) + } + + return tokenSubparts[0], tokenSubparts[1], nil +} + // KeycloakClient is the interface of the keycloak client. type KeycloakClient interface { VerifyToken(ctx context.Context, realmName string, accessToken string) error diff --git a/security/encryption.go b/security/encryption.go index ca3b300..2ade5cd 100644 --- a/security/encryption.go +++ b/security/encryption.go @@ -67,7 +67,7 @@ func (cd *aesGcmCrypting) Encrypt(value []byte, additional []byte) ([]byte, erro } var iv = make([]byte, 12) - rand.Read(iv) + _, _ = rand.Read(iv) var aesgcm cipher.AEAD aesgcm, err = cipher.NewGCMWithTagSize(block, cd.tagSize)