Skip to content

Commit 61aec9f

Browse files
committed
refactor(oauth): remove token cache deletion on invalid_grant
PutToken overwrites the cache file with O_TRUNC on re-auth, so explicitly deleting it is unnecessary. Simplify to log a warning and fall through to re-authentication. Rename test to reflect actual behavior: cache file is preserved, not deleted, when invalid_grant is encountered.
1 parent 3638fe6 commit 61aec9f

2 files changed

Lines changed: 8 additions & 13 deletions

File tree

oauth.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,6 @@ func BuildOAuthHTTPClient(ctx context.Context, scopes []string, oAuthPort int) (
282282
if err != nil {
283283
if IsInvalidGrant(err) {
284284
log.Printf("Cached token is invalid (refresh token revoked or expired), re-authenticating...")
285-
if removeErr := os.Remove(string(tokenCache)); removeErr != nil && !os.IsNotExist(removeErr) {
286-
log.Printf("Warning: failed to remove token cache: %v", removeErr)
287-
}
288285
} else {
289286
return nil, fmt.Errorf("error refreshing token: %w", err)
290287
}
@@ -394,4 +391,4 @@ func (f CacheFile) PutToken(tok *oauth2.Token) error {
394391
return fmt.Errorf("CacheFile.PutToken: %w", err)
395392
}
396393
return nil
397-
}
394+
}

test/oauth_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestCacheFile_RoundTrip(t *testing.T) {
9090
}
9191
}
9292

93-
func TestCacheFile_DeletedOnInvalidGrant(t *testing.T) {
93+
func TestCacheFile_ReauthOnInvalidGrant(t *testing.T) {
9494
tmpDir := t.TempDir()
9595
tokenPath := filepath.Join(tmpDir, "request.token")
9696

@@ -114,15 +114,13 @@ func TestCacheFile_DeletedOnInvalidGrant(t *testing.T) {
114114

115115
// Simulate invalid_grant error from token refresh
116116
refreshErr := &oauth2.RetrieveError{ErrorCode: "invalid_grant"}
117-
if yt.IsInvalidGrant(refreshErr) {
118-
if err := os.Remove(tokenPath); err != nil {
119-
t.Fatalf("failed to remove token cache: %v", err)
120-
}
117+
if !yt.IsInvalidGrant(refreshErr) {
118+
t.Fatal("expected IsInvalidGrant to return true for invalid_grant error")
121119
}
122120

123-
// Verify file is deleted
124-
if _, err := os.Stat(tokenPath); !os.IsNotExist(err) {
125-
t.Error("token cache file should have been deleted after invalid_grant")
121+
// Verify file is NOT deleted; PutToken will overwrite it when new token is written
122+
if _, err := os.Stat(tokenPath); os.IsNotExist(err) {
123+
t.Error("token cache file should NOT be deleted on invalid_grant; re-auth will overwrite it")
126124
}
127125
}
128126

@@ -156,4 +154,4 @@ func TestCacheFile_PutTokenOverwritesExisting(t *testing.T) {
156154
if got.AccessToken != "new-token" {
157155
t.Errorf("AccessToken = %q, want %q", got.AccessToken, "new-token")
158156
}
159-
}
157+
}

0 commit comments

Comments
 (0)