Skip to content

Commit 36f2682

Browse files
authored
chore(loo-4744): verify api key when running auth status (#14)
1 parent 37726b8 commit 36f2682

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

cmd/auth_status.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5+
"strings"
56

7+
"github.com/loops-so/cli/internal/api"
68
"github.com/loops-so/cli/internal/config"
79
"github.com/spf13/cobra"
810
)
@@ -11,23 +13,45 @@ var statusCmd = &cobra.Command{
1113
Use: "status",
1214
Short: "Print the resolved configuration",
1315
RunE: func(cmd *cobra.Command, args []string) error {
14-
cfg, err := runAuthStatus()
16+
cfg, keyResp, err := runAuthStatus()
1517
if err != nil {
1618
return err
1719
}
1820

21+
masked := maskKey(cfg.APIKey)
22+
1923
if isJSONOutput() {
20-
return printJSON(cmd.OutOrStdout(), cfg)
24+
return printJSON(cmd.OutOrStdout(), struct {
25+
APIKey string `json:"apiKey"`
26+
EndpointURL string `json:"endpointUrl"`
27+
TeamName string `json:"teamName"`
28+
}{masked, cfg.EndpointURL, keyResp.TeamName})
2129
}
2230

23-
fmt.Fprintf(cmd.OutOrStdout(), "API Key: %s\n", cfg.APIKey)
31+
fmt.Fprintf(cmd.OutOrStdout(), "API Key: %s\n", masked)
2432
fmt.Fprintf(cmd.OutOrStdout(), "Endpoint: %s\n", cfg.EndpointURL)
33+
fmt.Fprintf(cmd.OutOrStdout(), "Team: %s\n", keyResp.TeamName)
2534
return nil
2635
},
2736
}
2837

29-
func runAuthStatus() (*config.Config, error) {
30-
return config.Load()
38+
func runAuthStatus() (*config.Config, *api.APIKeyResponse, error) {
39+
cfg, err := config.Load()
40+
if err != nil {
41+
return nil, nil, err
42+
}
43+
keyResp, err := api.NewClient(cfg.EndpointURL, cfg.APIKey).GetAPIKey()
44+
if err != nil {
45+
return nil, nil, fmt.Errorf("API key verification failed: %w", err)
46+
}
47+
return cfg, keyResp, nil
48+
}
49+
50+
func maskKey(key string) string {
51+
if len(key) <= 4 {
52+
return "****"
53+
}
54+
return fmt.Sprintf("%s%s", strings.Repeat("*", len(key)-4), key[len(key)-4:])
3155
}
3256

3357
func init() {

cmd/auth_status_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
)
99

1010
func TestRunAuthStatus(t *testing.T) {
11-
t.Run("returns config", func(t *testing.T) {
12-
serveJSON(t, http.StatusOK, `{}`)
13-
cfg, err := runAuthStatus()
11+
t.Run("returns config and team name", func(t *testing.T) {
12+
serveJSON(t, http.StatusOK, `{"teamName":"Acme"}`)
13+
cfg, keyResp, err := runAuthStatus()
1414
if err != nil {
1515
t.Fatalf("unexpected error: %v", err)
1616
}
@@ -20,11 +20,22 @@ func TestRunAuthStatus(t *testing.T) {
2020
if cfg.EndpointURL == "" {
2121
t.Error("expected EndpointURL to be set")
2222
}
23+
if keyResp.TeamName != "Acme" {
24+
t.Errorf("got team %q, want %q", keyResp.TeamName, "Acme")
25+
}
2326
})
2427

2528
t.Run("returns error when no key set", func(t *testing.T) {
2629
keyring.MockInit()
27-
_, err := runAuthStatus()
30+
_, _, err := runAuthStatus()
31+
if err == nil {
32+
t.Fatal("expected error, got nil")
33+
}
34+
})
35+
36+
t.Run("returns error on api failure", func(t *testing.T) {
37+
serveJSON(t, http.StatusUnauthorized, `{"error":"Invalid API key"}`)
38+
_, _, err := runAuthStatus()
2839
if err == nil {
2940
t.Fatal("expected error, got nil")
3041
}

0 commit comments

Comments
 (0)