Skip to content

Commit f2108c6

Browse files
fix(auth): satisfy lint for manual auth flow
1 parent 3f6375a commit f2108c6

5 files changed

Lines changed: 178 additions & 94 deletions

File tree

internal/cmd/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,15 @@ func (c *AuthAddCmd) Run(ctx context.Context) error {
549549
if authURL != "" || authCode != "" {
550550
return usage("remote step 1 does not accept --auth-url or --auth-code")
551551
}
552-
result, err := manualAuthURL(ctx, googleauth.AuthorizeOptions{
552+
result, manualErr := manualAuthURL(ctx, googleauth.AuthorizeOptions{
553553
Services: services,
554554
Scopes: scopes,
555555
Manual: true,
556556
ForceConsent: c.ForceConsent,
557557
Client: client,
558558
})
559-
if err != nil {
560-
return err
559+
if manualErr != nil {
560+
return manualErr
561561
}
562562
if outfmt.IsJSON(ctx) {
563563
return outfmt.WriteJSON(os.Stdout, map[string]any{

internal/googleauth/manual_state.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
func 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
}

internal/googleauth/manual_state_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ func TestManualAuthURL_ReusesState(t *testing.T) {
1212
origRead := readClientCredentials
1313
origEndpoint := oauthEndpoint
1414
origState := randomStateFn
15+
1516
t.Cleanup(func() {
1617
readClientCredentials = origRead
1718
oauthEndpoint = origEndpoint
1819
randomStateFn = origState
1920
})
21+
2022
useTempManualStatePath(t)
2123

2224
readClientCredentials = func(string) (config.ClientCredentials, error) {
@@ -29,6 +31,7 @@ func TestManualAuthURL_ReusesState(t *testing.T) {
2931
if stateCalls == 1 {
3032
return "state1", nil
3133
}
34+
3235
return "state2", nil
3336
}
3437

@@ -39,6 +42,7 @@ func TestManualAuthURL_ReusesState(t *testing.T) {
3942
if err != nil {
4043
t.Fatalf("ManualAuthURL: %v", err)
4144
}
45+
4246
res2, err := ManualAuthURL(context.Background(), AuthorizeOptions{
4347
Scopes: []string{"s1"},
4448
Manual: true,
@@ -48,23 +52,28 @@ func TestManualAuthURL_ReusesState(t *testing.T) {
4852
}
4953

5054
state1 := authURLState(t, res1.URL)
55+
5156
state2 := authURLState(t, res2.URL)
5257
if state1 != "state1" || state2 != "state1" {
5358
t.Fatalf("expected reused state, got state1=%q state2=%q", state1, state2)
5459
}
60+
5561
if !res2.StateReused {
5662
t.Fatalf("expected state_reused true on second call")
5763
}
64+
5865
if stateCalls != 1 {
5966
t.Fatalf("expected randomStateFn called once, got %d", stateCalls)
6067
}
6168
}
6269

6370
func authURLState(t *testing.T, rawURL string) string {
6471
t.Helper()
72+
6573
parsed, err := url.Parse(rawURL)
6674
if err != nil {
6775
t.Fatalf("parse auth URL: %v", err)
6876
}
77+
6978
return parsed.Query().Get("state")
7079
}

0 commit comments

Comments
 (0)