3333func manualStatePath () (string , error ) {
3434 dir , err := config .EnsureDir ()
3535 if err != nil {
36- return "" , err
36+ return "" , fmt . Errorf ( "ensure config dir: %w" , err )
3737 }
3838
3939 return filepath .Join (dir , manualStateFilename ), nil
@@ -48,24 +48,35 @@ func loadManualState(client string, scopes []string, forceConsent bool) (string,
4848 data , err := os .ReadFile (path ) //nolint:gosec // config path
4949 if err != nil {
5050 if os .IsNotExist (err ) {
51- return "" , false , nil
51+ err = nil
52+ return "" , false , err
5253 }
54+
5355 return "" , false , fmt .Errorf ("read manual auth state: %w" , err )
5456 }
5557
5658 var st manualState
57- if err := json .Unmarshal (data , & st ); err != nil {
59+
60+ unmarshalErr := json .Unmarshal (data , & st )
61+ if unmarshalErr != nil {
5862 _ = os .Remove (path )
59- return "" , false , nil
63+ unmarshalErr = nil
64+
65+ return "" , false , unmarshalErr //nolint:nilerr // invalid state should be treated as a cache miss
6066 }
67+
6168 if st .State == "" {
6269 _ = os .Remove (path )
70+
6371 return "" , false , nil
6472 }
73+
6574 if manualStateNowFn ().Sub (st .CreatedAt ) > manualStateTTL {
6675 _ = os .Remove (path )
76+
6777 return "" , false , nil
6878 }
79+
6980 if st .Client != client || st .ForceConsent != forceConsent || ! scopesEqual (st .Scopes , scopes ) {
7081 return "" , false , nil
7182 }
@@ -84,22 +95,29 @@ func loadManualStateStrict(client string, scopes []string, forceConsent bool) (s
8495 if os .IsNotExist (err ) {
8596 return "" , errManualStateMissing
8697 }
98+
8799 return "" , fmt .Errorf ("read manual auth state: %w" , err )
88100 }
89101
90102 var st manualState
91103 if err := json .Unmarshal (data , & st ); err != nil {
92104 _ = os .Remove (path )
105+
93106 return "" , errManualStateMissing
94107 }
108+
95109 if st .State == "" {
96110 _ = os .Remove (path )
111+
97112 return "" , errManualStateMissing
98113 }
114+
99115 if manualStateNowFn ().Sub (st .CreatedAt ) > manualStateTTL {
100116 _ = os .Remove (path )
117+
101118 return "" , errManualStateMissing
102119 }
120+
103121 if st .Client != client || st .ForceConsent != forceConsent || ! scopesEqual (st .Scopes , scopes ) {
104122 return "" , errManualStateMismatch
105123 }
@@ -125,12 +143,14 @@ func saveManualState(client string, scopes []string, forceConsent bool, state st
125143 if err != nil {
126144 return fmt .Errorf ("encode manual auth state: %w" , err )
127145 }
146+
128147 data = append (data , '\n' )
129148
130149 tmp := path + ".tmp"
131150 if err := os .WriteFile (tmp , data , 0o600 ); err != nil {
132151 return fmt .Errorf ("write manual auth state: %w" , err )
133152 }
153+
134154 if err := os .Rename (tmp , path ); err != nil {
135155 return fmt .Errorf ("commit manual auth state: %w" , err )
136156 }
@@ -148,6 +168,7 @@ func clearManualState() error {
148168 if os .IsNotExist (err ) {
149169 return nil
150170 }
171+
151172 return fmt .Errorf ("remove manual auth state: %w" , err )
152173 }
153174
@@ -161,6 +182,7 @@ func normalizeScopes(scopes []string) []string {
161182
162183 out := append ([]string (nil ), scopes ... )
163184 sort .Strings (out )
185+
164186 return out
165187}
166188
@@ -170,10 +192,12 @@ func scopesEqual(a, b []string) bool {
170192 }
171193 na := normalizeScopes (a )
172194 nb := normalizeScopes (b )
195+
173196 for i := range na {
174197 if na [i ] != nb [i ] {
175198 return false
176199 }
177200 }
201+
178202 return true
179203}
0 commit comments