From 321d23cb4da629cd5eb965af9e100802e195fab9 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Thu, 5 Jul 2018 10:06:11 +0300 Subject: [PATCH] Fix failing test & check formatting (#10279) --- agents/go-agents/core/auth/auth.go | 22 +++++++++++----------- agents/go-agents/core/auth/auth_test.go | 2 +- agents/go-agents/core/auth/sign_key.go | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/agents/go-agents/core/auth/auth.go b/agents/go-agents/core/auth/auth.go index 284df2dfdd..fe51bbe291 100644 --- a/agents/go-agents/core/auth/auth.go +++ b/agents/go-agents/core/auth/auth.go @@ -18,15 +18,15 @@ import ( "regexp" "fmt" - "strings" "github.com/dgrijalva/jwt-go" "os" + "strings" ) const ( TokenKind = "machine_token" WorkspaceIdEnv = "CHE_WORKSPACE_ID" - BearerPrefix = "bearer " + BearerPrefix = "bearer " ) var ( @@ -114,10 +114,10 @@ func (handler handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } token := req.URL.Query().Get("token") if token == "" { - header := req.Header.Get("Authorization") - if header != "" && strings.HasPrefix(strings.ToLower(header), BearerPrefix) { - token = header[len(BearerPrefix):] - } + header := req.Header.Get("Authorization") + if header != "" && strings.HasPrefix(strings.ToLower(header), BearerPrefix) { + token = header[len(BearerPrefix):] + } } if err := authenticate(token); err == nil { handler.delegate.ServeHTTP(w, req) @@ -134,10 +134,10 @@ func (handler cachingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request } token := req.URL.Query().Get("token") if token == "" { - header := req.Header.Get("Authorization") - if header != "" && strings.HasPrefix(strings.ToLower(header), BearerPrefix) { - token = header[len(BearerPrefix):] - } + header := req.Header.Get("Authorization") + if header != "" && strings.HasPrefix(strings.ToLower(header), BearerPrefix) { + token = header[len(BearerPrefix):] + } } if handler.cache.Contains(token) { handler.delegate.ServeHTTP(w, req) @@ -151,7 +151,7 @@ func (handler cachingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request func authenticate(token string) error { if token == "" { - return errors.New("Authentication failed because: missing authentication token") + return errors.New("Authentication failed because: missing authentication token in 'Authorization' header or 'token' query param") } claims := &JWTClaims{} diff --git a/agents/go-agents/core/auth/auth_test.go b/agents/go-agents/core/auth/auth_test.go index 649e23b096..5d58e0d103 100644 --- a/agents/go-agents/core/auth/auth_test.go +++ b/agents/go-agents/core/auth/auth_test.go @@ -74,7 +74,7 @@ func TestAuthenticationFailedWhenEmptyTokenProvided(t *testing.T) { if err == nil { t.Fatal("Expecting error when no empty token provided") } - expectedError := "Authentication failed because: missing 'token' query parameter" + expectedError := "Authentication failed because: missing authentication token in 'Authorization' header or 'token' query param" if err.Error() != expectedError { t.Fatalf("Expecting error message: '%v'", expectedError) } diff --git a/agents/go-agents/core/auth/sign_key.go b/agents/go-agents/core/auth/sign_key.go index 2e2d522a46..72101bf241 100644 --- a/agents/go-agents/core/auth/sign_key.go +++ b/agents/go-agents/core/auth/sign_key.go @@ -12,10 +12,10 @@ package auth import ( - "encoding/base64" - "os" - "errors" "crypto/x509" + "encoding/base64" + "errors" + "os" ) const SignatureKeyEnv = "CHE_MACHINE_AUTH_SIGNATURE__PUBLIC__KEY"