From 1060520fa77e3d3b7ce371d6736086d2c8f91b02 Mon Sep 17 00:00:00 2001 From: reesporte Date: Mon, 11 Apr 2022 16:16:46 -0500 Subject: [PATCH] add better error messaging for if the test is empty --- authn/authenticate.go | 4 ++++ authn/authenticate_internal_test.go | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/authn/authenticate.go b/authn/authenticate.go index 5cc1d64aa..5b8800e98 100644 --- a/authn/authenticate.go +++ b/authn/authenticate.go @@ -145,6 +145,10 @@ func (a *Auth) Authenticate(ctx context.Context, bearer string) (*UserInfo, erro a.tokenCache[bearer] = cachedToken{time.Now(), &t} } + if len(bearer) == 0 { + return nil, fmt.Errorf("bearer token is empty") + } + // NOTE: we are using ParseUnverified here because the IDP validates the // token's signature when we get the user's groups, we just need to make // sure it's not expired and is well-formed diff --git a/authn/authenticate_internal_test.go b/authn/authenticate_internal_test.go index 57d7dd846..41e841c8d 100644 --- a/authn/authenticate_internal_test.go +++ b/authn/authenticate_internal_test.go @@ -139,6 +139,7 @@ func TestAuthenticate(t *testing.T) { refresh bool errOnRefresh bool malformed bool + empty bool groups []Group err error }{ @@ -158,7 +159,11 @@ func TestAuthenticate(t *testing.T) { malformed: true, err: fmt.Errorf("parsing bearer token: token contains an invalid number of segments"), }, - + { + name: "Empty", + empty: true, + err: fmt.Errorf("bearer token is empty"), + }, { name: "ExpiredTokenNoRefresh", uid: "42", @@ -207,7 +212,7 @@ func TestAuthenticate(t *testing.T) { a := NewTestAuth(t) token := "" var err error - if !test.malformed { + if !test.malformed && !test.empty { tkn := jwt.New(jwt.SigningMethodHS256) claims := tkn.Claims.(jwt.MapClaims) claims["oid"] = test.uid @@ -219,7 +224,7 @@ func TestAuthenticate(t *testing.T) { if err != nil { t.Fatalf("unexpected error when signing token %v", err) } - } else { + } else if !test.empty { token = "asdfasdfasdfasdF" } if len(test.groups) > 0 {