@@ -106,6 +106,12 @@ func setAuthFieldWithProfile(profile string, key authFieldKey, value string) err
106106 if errFallback != nil {
107107 return fmt .Errorf ("write to keyring failed (%w), try writing to encoded text file: %w" , err , errFallback )
108108 }
109+ // If writing to the text file succeeded as a fallback, clean up any stale key in the keyring
110+ // so GetAuthField won't return outdated credentials.
111+ _ = deleteAuthFieldInKeyring (profile , key )
112+ } else {
113+ // If writing to the keyring succeeded, clean up any old entry from the encoded text file.
114+ _ = deleteAuthFieldInEncodedTextFile (profile , key )
109115 }
110116
111117 return nil
@@ -130,31 +136,34 @@ func DeleteAuthField(key authFieldKey) error {
130136}
131137
132138func deleteAuthFieldWithProfile (profile string , key authFieldKey ) error {
139+ var errKeyring , errFile error
140+
133141 err := deleteAuthFieldInKeyring (profile , key )
142+ if err != nil && ! errors .Is (err , keyring .ErrNotFound ) {
143+ errKeyring = err
144+ }
145+
146+ err = deleteAuthFieldInEncodedTextFile (profile , key )
134147 if err != nil {
135- // if the key is not found, we can ignore the error
136- if ! errors .Is (err , keyring .ErrNotFound ) {
137- errFallback := deleteAuthFieldInEncodedTextFile (profile , key )
138- if errFallback != nil {
139- return fmt .Errorf ("delete from keyring failed (%w), try deleting from encoded text file: %w" , err , errFallback )
140- }
141- }
148+ errFile = err
149+ }
150+
151+ if errKeyring != nil && errFile != nil {
152+ return fmt .Errorf ("delete from keyring: %w, delete from encoded text file: %w" , errKeyring , errFile )
142153 }
143154
144155 return nil
145156}
146157
147158func deleteAuthFieldInEncodedTextFile (activeProfile string , key authFieldKey ) error {
148- err := createEncodedTextFile (activeProfile )
149- if err != nil {
150- return err
151- }
152-
153159 textFileDir := config .GetProfileFolderPath (activeProfile )
154160 textFilePath := filepath .Join (textFileDir , textFileName )
155161
156162 contentEncoded , err := os .ReadFile (textFilePath )
157163 if err != nil {
164+ if os .IsNotExist (err ) {
165+ return nil
166+ }
158167 return fmt .Errorf ("read file: %w" , err )
159168 }
160169
@@ -170,8 +179,21 @@ func deleteAuthFieldInEncodedTextFile(activeProfile string, key authFieldKey) er
170179 return fmt .Errorf ("unmarshal file: %w" , err )
171180 }
172181
182+ if _ , ok := content [key ]; ! ok {
183+ return nil
184+ }
185+
173186 delete (content , key )
174187
188+ if len (content ) == 0 {
189+ err = os .Remove (textFilePath )
190+ if err != nil && ! os .IsNotExist (err ) {
191+ return fmt .Errorf ("remove empty encoded text file: %w" , err )
192+ }
193+
194+ return nil
195+ }
196+
175197 contentBytes , err = json .Marshal (content )
176198 if err != nil {
177199 return fmt .Errorf ("marshal file: %w" , err )
@@ -291,11 +313,6 @@ func getAuthFieldFromKeyring(activeProfile string, key authFieldKey) (string, er
291313}
292314
293315func getAuthFieldFromEncodedTextFile (activeProfile string , key authFieldKey ) (string , error ) {
294- err := createEncodedTextFile (activeProfile )
295- if err != nil {
296- return "" , err
297- }
298-
299316 textFileDir := config .GetProfileFolderPath (activeProfile )
300317 textFilePath := filepath .Join (textFileDir , textFileName )
301318
@@ -403,6 +420,17 @@ func GetAuthEmail() (string, error) {
403420}
404421
405422func LoginUser (email , accessToken , refreshToken , sessionExpiresAtUnix string ) error {
423+ // Clean up any service account credentials that might be present
424+ saFields := []authFieldKey {
425+ SERVICE_ACCOUNT_KEY ,
426+ PRIVATE_KEY ,
427+ SERVICE_ACCOUNT_TOKEN ,
428+ SERVICE_ACCOUNT_EMAIL ,
429+ }
430+ for _ , key := range saFields {
431+ _ = DeleteAuthField (key )
432+ }
433+
406434 authFields := map [authFieldKey ]string {
407435 SESSION_EXPIRES_AT_UNIX : sessionExpiresAtUnix ,
408436 ACCESS_TOKEN : accessToken ,
@@ -419,13 +447,26 @@ func LoginUser(email, accessToken, refreshToken, sessionExpiresAtUnix string) er
419447}
420448
421449func LogoutUser () error {
422- for _ , key := range loginAuthFieldKeys {
450+ for _ , key := range authFieldKeys {
423451 err := DeleteAuthField (key )
424452 if err != nil {
425453 return fmt .Errorf ("delete auth field \" %s\" : %w" , key , err )
426454 }
427455 }
428456
457+ activeProfile , err := config .GetProfile ()
458+ if err != nil {
459+ return fmt .Errorf ("get profile: %w" , err )
460+ }
461+
462+ textFileDir := config .GetProfileFolderPath (activeProfile )
463+ textFilePath := filepath .Join (textFileDir , textFileName )
464+
465+ err = os .Remove (textFilePath )
466+ if err != nil && ! os .IsNotExist (err ) {
467+ return fmt .Errorf ("remove auth storage file: %w" , err )
468+ }
469+
429470 return nil
430471}
431472
@@ -446,5 +487,13 @@ func DeleteProfileAuth(profile string) error {
446487 }
447488 }
448489
490+ textFileDir := config .GetProfileFolderPath (profile )
491+ textFilePath := filepath .Join (textFileDir , textFileName )
492+
493+ err = os .Remove (textFilePath )
494+ if err != nil && ! os .IsNotExist (err ) {
495+ return fmt .Errorf ("remove auth storage file: %w" , err )
496+ }
497+
449498 return nil
450499}
0 commit comments