Skip to content

Commit

Permalink
Unit test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 authored Jun 5, 2020
1 parent 0dac96e commit 4493655
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
69 changes: 35 additions & 34 deletions middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion security/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4493655

Please sign in to comment.