Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions internal/cmd/auth/check.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package auth

import (
psauth "github.com/planetscale/cli/internal/auth"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"

"github.com/spf13/cobra"
)

func CheckCmd(ch *cmdutil.Helper) *cobra.Command {
var clientID string
var clientSecret string

cmd := &cobra.Command{
Use: "check",
Args: cobra.NoArgs,
Expand Down Expand Up @@ -49,7 +45,5 @@ func CheckCmd(ch *cmdutil.Helper) *cobra.Command {
},
}

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flags are unused by this command, can be removed.

return cmd
}
4 changes: 2 additions & 2 deletions internal/cmd/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func LoginCmd(ch *cmdutil.Helper) *cobra.Command {
return errors.New("the 'login' command requires an interactive shell (use --format json; browser opens when possible, then polls until approved)")
}

clientID, clientSecret = resolveOAuthClient(clientID, clientSecret)
authenticator, err := psauth.New(cleanhttp.DefaultClient(), clientID, clientSecret, psauth.SetBaseURL(authURL))
if err != nil {
if jsonMode {
Expand Down Expand Up @@ -126,8 +127,7 @@ func LoginCmd(ch *cmdutil.Helper) *cobra.Command {
},
}

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

return cmd
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func LogoutCmd(ch *cmdutil.Helper) *cobra.Command {
_ = waitForEnter(cmd.InOrStdin())
}

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

cmd.Flags().StringVar(&clientID, "client-id", auth.OAuthClientID, "The client ID for the PlanetScale CLI application.")
cmd.Flags().StringVar(&clientSecret, "client-secret", auth.OAuthClientSecret, "The client ID for the PlanetScale CLI application")
addOAuthClientFlags(cmd, &clientID, &clientSecret)
cmd.Flags().StringVar(&apiURL, "api-url", auth.DefaultBaseURL, "The PlanetScale base API URL.")
return cmd
}
Expand Down
26 changes: 26 additions & 0 deletions internal/cmd/auth/oauth_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package auth

import (
psauth "github.com/planetscale/cli/internal/auth"

"github.com/spf13/cobra"
)

// addOAuthClientFlags registers --client-id and --client-secret without exposing
// the built-in OAuth credentials as help-text defaults.
func addOAuthClientFlags(cmd *cobra.Command, clientID, clientSecret *string) {
cmd.Flags().StringVar(clientID, "client-id", "", "The client ID for the PlanetScale CLI application.")
cmd.Flags().StringVar(clientSecret, "client-secret", "", "The client secret for the PlanetScale CLI application.")
}

// resolveOAuthClient returns the flag values, falling back to the built-in
// PlanetScale CLI OAuth application credentials when unset.
func resolveOAuthClient(clientID, clientSecret string) (string, string) {
if clientID == "" {
clientID = psauth.OAuthClientID
}
if clientSecret == "" {
clientSecret = psauth.OAuthClientSecret
}
return clientID, clientSecret
}
75 changes: 75 additions & 0 deletions internal/cmd/auth/oauth_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package auth

import (
"bytes"
"strings"
"testing"

psauth "github.com/planetscale/cli/internal/auth"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/spf13/cobra"
)

func TestAuthLoginHelpOmitsOAuthSecrets(t *testing.T) {
assertHelpOmitsOAuthSecrets(t, LoginCmd(&cmdutil.Helper{}))
}

func TestAuthLogoutHelpOmitsOAuthSecrets(t *testing.T) {
assertHelpOmitsOAuthSecrets(t, LogoutCmd(&cmdutil.Helper{}))
}

func TestAuthCheckHasNoOAuthClientFlags(t *testing.T) {
cmd := CheckCmd(&cmdutil.Helper{})
if cmd.Flags().Lookup("client-id") != nil {
t.Fatal("auth check must not define --client-id")
}
if cmd.Flags().Lookup("client-secret") != nil {
t.Fatal("auth check must not define --client-secret")
}
}

func assertHelpOmitsOAuthSecrets(t *testing.T, cmd *cobra.Command) {
t.Helper()

var out bytes.Buffer
cmd.SetOut(&out)
cmd.SetErr(&out)
cmd.SetArgs([]string{"--help"})
if err := cmd.Execute(); err != nil {
t.Fatalf("help: %v", err)
}

help := out.String()
if strings.Contains(help, psauth.OAuthClientID) {
t.Fatalf("help text must not include OAuth client ID; got:\n%s", help)
}
if strings.Contains(help, psauth.OAuthClientSecret) {
t.Fatalf("help text must not include OAuth client secret; got:\n%s", help)
}
if !strings.Contains(help, "--client-id") {
t.Fatalf("help text should still document --client-id; got:\n%s", help)
}
if !strings.Contains(help, "--client-secret") {
t.Fatalf("help text should still document --client-secret; got:\n%s", help)
}
}

func TestResolveOAuthClientDefaults(t *testing.T) {
id, secret := resolveOAuthClient("", "")
if id != psauth.OAuthClientID {
t.Fatalf("client ID = %q, want built-in default", id)
}
if secret != psauth.OAuthClientSecret {
t.Fatalf("client secret = %q, want built-in default", secret)
}
}

func TestResolveOAuthClientOverrides(t *testing.T) {
id, secret := resolveOAuthClient("custom-id", "custom-secret")
if id != "custom-id" {
t.Fatalf("client ID = %q, want custom-id", id)
}
if secret != "custom-secret" {
t.Fatalf("client secret = %q, want custom-secret", secret)
}
}