From f13011be77b8e13a6bc46083547767cebef778b2 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Tue, 6 Sep 2022 14:57:09 +0800 Subject: [PATCH] fix(client): skip resource check in `GetAccessToken` when resource is empty string --- client/client.go | 7 ++++--- client/client_test.go | 12 +++--------- client/handle_sign_in_callback_test.go | 4 +--- client/helper_test.go | 9 ++------- 4 files changed, 10 insertions(+), 22 deletions(-) diff --git a/client/client.go b/client/client.go index 5e1b132..d54828b 100644 --- a/client/client.go +++ b/client/client.go @@ -87,9 +87,10 @@ func (logtoClient *LogtoClient) GetAccessToken(resource string) (AccessToken, er return AccessToken{}, ErrNotAuthenticated } - // TODO: do not check granted resource if resource is empty - if !slices.Contains(logtoClient.logtoConfig.Resources, resource) { - return AccessToken{}, ErrUnacknowledgedResourceFound + if resource != "" { + if !slices.Contains(logtoClient.logtoConfig.Resources, resource) { + return AccessToken{}, ErrUnacknowledgedResourceFound + } } accessTokenKey := buildAccessTokenKey([]string{}, resource) diff --git a/client/client_test.go b/client/client_test.go index 75104fe..15ba7b0 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -19,9 +19,7 @@ func TestGetAccessTokenShouldReturnAccessTokenAccessTokenInTokenMap(t *testing.T } logtoClient := NewLogtoClient( - &LogtoConfig{ - Resources: []string{""}, - }, + &LogtoConfig{}, &TestStorage{ data: map[string]string{ StorageKeyIdToken: "id token", @@ -39,9 +37,7 @@ func TestGetAccessTokenShouldReturnAccessTokenAccessTokenInTokenMap(t *testing.T func TestGetAccessTokenShouldReturnNotAuthenticatedErrIfNoIdTokenAvailable(t *testing.T) { logtoClient := NewLogtoClient( - &LogtoConfig{ - Resources: []string{""}, - }, + &LogtoConfig{}, &TestStorage{ data: map[string]string{ StorageKeyIdToken: "", @@ -95,9 +91,7 @@ func TestGetAccessTokenShouldReturnFetchedAccessTokenAndUpdateLocalAccessTokenIf defer patchesForVerifyIdToken.Reset() logtoClient := NewLogtoClient( - &LogtoConfig{ - Resources: []string{""}, - }, + &LogtoConfig{}, &TestStorage{ data: map[string]string{ StorageKeyIdToken: "id token", diff --git a/client/handle_sign_in_callback_test.go b/client/handle_sign_in_callback_test.go index 3a48935..8a2e840 100644 --- a/client/handle_sign_in_callback_test.go +++ b/client/handle_sign_in_callback_test.go @@ -64,9 +64,7 @@ func TestHandleSignInCallbackShouldHandleCallbackCorrectly(t *testing.T) { }, } - logtoClient := NewLogtoClient(&LogtoConfig{ - Resources: []string{""}, - }, storage) + logtoClient := NewLogtoClient(&LogtoConfig{}, storage) signInCallbackRequest, createSignInCallbackRequestErr := http.NewRequest("GET", "https://example.com/sign-in-callback", nil) assert.Nil(t, createSignInCallbackRequestErr) diff --git a/client/helper_test.go b/client/helper_test.go index a116d9d..9a3af1a 100644 --- a/client/helper_test.go +++ b/client/helper_test.go @@ -66,9 +66,7 @@ func TestFetchOidcConfigShouldReturnExpectedOidcConfig(t *testing.T) { func TestLoadAccessTokenMapShouldLoadAccessTokenMapFromStorage(t *testing.T) { expiresAt := time.Now().Unix() + 60 - logtoConfig := &LogtoConfig{ - Resources: []string{"testResource"}, - } + logtoConfig := &LogtoConfig{} testStorage := &TestStorage{ data: map[string]string{ @@ -152,10 +150,7 @@ func TestVerifyAndSaveTokenResponseShouldSaveToken(t *testing.T) { defer patchesForVerifyIdToken.Reset() - logtoClient := NewLogtoClient(&LogtoConfig{ - // TODO: do not check granted resource if resource is empty - Resources: []string{""}, - }, &TestStorage{ + logtoClient := NewLogtoClient(&LogtoConfig{}, &TestStorage{ data: map[string]string{}, })