From 29429a1854116ab9aa663076a12ea9548e66d7a0 Mon Sep 17 00:00:00 2001 From: Toan Vo Date: Wed, 1 Jun 2022 23:45:01 +0700 Subject: [PATCH] Fix basic auth parser --- src/auth/api/http/auth.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/auth/api/http/auth.go b/src/auth/api/http/auth.go index a30487a6e..ec8265f77 100644 --- a/src/auth/api/http/auth.go +++ b/src/auth/api/http/auth.go @@ -1,7 +1,6 @@ package http import ( - "encoding/base64" "net/http" "strings" @@ -51,19 +50,19 @@ func (m *Auth) Middleware(next http.Handler) http.Handler { next.ServeHTTP(rw, r.WithContext(WithUserInfo(ctx, userInfo))) return case BasicSchema: - apiKey, err := base64.StdEncoding.DecodeString(authValue) - if err != nil { - httpinfra.WriteHTTPErrorResponse(rw, errors.InvalidFormatError(err.Error())) - return - } + _, apiKey, ok := r.BasicAuth() + if ok { + userInfo, err := m.authenticator.AuthenticateAPIKey(r.Context(), []byte(apiKey)) + if err != nil { + httpinfra.WriteHTTPErrorResponse(rw, err) + return + } - userInfo, err := m.authenticator.AuthenticateAPIKey(r.Context(), apiKey) - if err != nil { - httpinfra.WriteHTTPErrorResponse(rw, err) + next.ServeHTTP(rw, r.WithContext(WithUserInfo(ctx, userInfo))) return } - next.ServeHTTP(rw, r.WithContext(WithUserInfo(ctx, userInfo))) + httpinfra.WriteHTTPErrorResponse(rw, errors.InvalidFormatError("error parsing basic auth")) return default: httpinfra.WriteHTTPErrorResponse(rw, errors.InvalidFormatError("unsupported authorization schema %s", authSchema))