Skip to content

Commit 924bb3a

Browse files
Merge pull request #1309 from planetscale/cursor/auth-login-help-secrets-e3f0
Omit OAuth client credentials from auth help text
2 parents 8adefea + 6f7cdf3 commit 924bb3a

5 files changed

Lines changed: 105 additions & 10 deletions

File tree

internal/cmd/auth/check.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package auth
22

33
import (
4-
psauth "github.com/planetscale/cli/internal/auth"
54
"github.com/planetscale/cli/internal/cmdutil"
65
"github.com/planetscale/cli/internal/printer"
76

87
"github.com/spf13/cobra"
98
)
109

1110
func CheckCmd(ch *cmdutil.Helper) *cobra.Command {
12-
var clientID string
13-
var clientSecret string
14-
1511
cmd := &cobra.Command{
1612
Use: "check",
1713
Args: cobra.NoArgs,
@@ -49,7 +45,5 @@ func CheckCmd(ch *cmdutil.Helper) *cobra.Command {
4945
},
5046
}
5147

52-
cmd.Flags().StringVar(&clientID, "client-id", psauth.OAuthClientID, "The client ID for the PlanetScale CLI application.")
53-
cmd.Flags().StringVar(&clientSecret, "client-secret", psauth.OAuthClientSecret, "The client ID for the PlanetScale CLI application")
5448
return cmd
5549
}

internal/cmd/auth/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func LoginCmd(ch *cmdutil.Helper) *cobra.Command {
3333
return errors.New("the 'login' command requires an interactive shell (use --format json; browser opens when possible, then polls until approved)")
3434
}
3535

36+
clientID, clientSecret = resolveOAuthClient(clientID, clientSecret)
3637
authenticator, err := psauth.New(cleanhttp.DefaultClient(), clientID, clientSecret, psauth.SetBaseURL(authURL))
3738
if err != nil {
3839
if jsonMode {
@@ -126,8 +127,7 @@ func LoginCmd(ch *cmdutil.Helper) *cobra.Command {
126127
},
127128
}
128129

129-
cmd.Flags().StringVar(&clientID, "client-id", psauth.OAuthClientID, "The client ID for the PlanetScale CLI application.")
130-
cmd.Flags().StringVar(&clientSecret, "client-secret", psauth.OAuthClientSecret, "The client ID for the PlanetScale CLI application")
130+
addOAuthClientFlags(cmd, &clientID, &clientSecret)
131131
cmd.Flags().StringVar(&authURL, "api-url", psauth.DefaultBaseURL, "The PlanetScale Auth API base URL.")
132132

133133
return cmd

internal/cmd/auth/logout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func LogoutCmd(ch *cmdutil.Helper) *cobra.Command {
3333
_ = waitForEnter(cmd.InOrStdin())
3434
}
3535

36+
clientID, clientSecret = resolveOAuthClient(clientID, clientSecret)
3637
authenticator, err := auth.New(cleanhttp.DefaultClient(), clientID, clientSecret, auth.SetBaseURL(apiURL))
3738
if err != nil {
3839
return err
@@ -56,8 +57,7 @@ func LogoutCmd(ch *cmdutil.Helper) *cobra.Command {
5657
},
5758
}
5859

59-
cmd.Flags().StringVar(&clientID, "client-id", auth.OAuthClientID, "The client ID for the PlanetScale CLI application.")
60-
cmd.Flags().StringVar(&clientSecret, "client-secret", auth.OAuthClientSecret, "The client ID for the PlanetScale CLI application")
60+
addOAuthClientFlags(cmd, &clientID, &clientSecret)
6161
cmd.Flags().StringVar(&apiURL, "api-url", auth.DefaultBaseURL, "The PlanetScale base API URL.")
6262
return cmd
6363
}

internal/cmd/auth/oauth_flags.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package auth
2+
3+
import (
4+
psauth "github.com/planetscale/cli/internal/auth"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// addOAuthClientFlags registers --client-id and --client-secret without exposing
10+
// the built-in OAuth credentials as help-text defaults.
11+
func addOAuthClientFlags(cmd *cobra.Command, clientID, clientSecret *string) {
12+
cmd.Flags().StringVar(clientID, "client-id", "", "The client ID for the PlanetScale CLI application.")
13+
cmd.Flags().StringVar(clientSecret, "client-secret", "", "The client secret for the PlanetScale CLI application.")
14+
}
15+
16+
// resolveOAuthClient returns the flag values, falling back to the built-in
17+
// PlanetScale CLI OAuth application credentials when unset.
18+
func resolveOAuthClient(clientID, clientSecret string) (string, string) {
19+
if clientID == "" {
20+
clientID = psauth.OAuthClientID
21+
}
22+
if clientSecret == "" {
23+
clientSecret = psauth.OAuthClientSecret
24+
}
25+
return clientID, clientSecret
26+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package auth
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
psauth "github.com/planetscale/cli/internal/auth"
9+
"github.com/planetscale/cli/internal/cmdutil"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func TestAuthLoginHelpOmitsOAuthSecrets(t *testing.T) {
14+
assertHelpOmitsOAuthSecrets(t, LoginCmd(&cmdutil.Helper{}))
15+
}
16+
17+
func TestAuthLogoutHelpOmitsOAuthSecrets(t *testing.T) {
18+
assertHelpOmitsOAuthSecrets(t, LogoutCmd(&cmdutil.Helper{}))
19+
}
20+
21+
func TestAuthCheckHasNoOAuthClientFlags(t *testing.T) {
22+
cmd := CheckCmd(&cmdutil.Helper{})
23+
if cmd.Flags().Lookup("client-id") != nil {
24+
t.Fatal("auth check must not define --client-id")
25+
}
26+
if cmd.Flags().Lookup("client-secret") != nil {
27+
t.Fatal("auth check must not define --client-secret")
28+
}
29+
}
30+
31+
func assertHelpOmitsOAuthSecrets(t *testing.T, cmd *cobra.Command) {
32+
t.Helper()
33+
34+
var out bytes.Buffer
35+
cmd.SetOut(&out)
36+
cmd.SetErr(&out)
37+
cmd.SetArgs([]string{"--help"})
38+
if err := cmd.Execute(); err != nil {
39+
t.Fatalf("help: %v", err)
40+
}
41+
42+
help := out.String()
43+
if strings.Contains(help, psauth.OAuthClientID) {
44+
t.Fatalf("help text must not include OAuth client ID; got:\n%s", help)
45+
}
46+
if strings.Contains(help, psauth.OAuthClientSecret) {
47+
t.Fatalf("help text must not include OAuth client secret; got:\n%s", help)
48+
}
49+
if !strings.Contains(help, "--client-id") {
50+
t.Fatalf("help text should still document --client-id; got:\n%s", help)
51+
}
52+
if !strings.Contains(help, "--client-secret") {
53+
t.Fatalf("help text should still document --client-secret; got:\n%s", help)
54+
}
55+
}
56+
57+
func TestResolveOAuthClientDefaults(t *testing.T) {
58+
id, secret := resolveOAuthClient("", "")
59+
if id != psauth.OAuthClientID {
60+
t.Fatalf("client ID = %q, want built-in default", id)
61+
}
62+
if secret != psauth.OAuthClientSecret {
63+
t.Fatalf("client secret = %q, want built-in default", secret)
64+
}
65+
}
66+
67+
func TestResolveOAuthClientOverrides(t *testing.T) {
68+
id, secret := resolveOAuthClient("custom-id", "custom-secret")
69+
if id != "custom-id" {
70+
t.Fatalf("client ID = %q, want custom-id", id)
71+
}
72+
if secret != "custom-secret" {
73+
t.Fatalf("client secret = %q, want custom-secret", secret)
74+
}
75+
}

0 commit comments

Comments
 (0)