-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauth.go
More file actions
254 lines (207 loc) · 7.87 KB
/
auth.go
File metadata and controls
254 lines (207 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"golang.org/x/term"
"github.com/timescale/tiger-cli/internal/tiger/api"
"github.com/timescale/tiger-cli/internal/tiger/config"
"github.com/timescale/tiger-cli/internal/tiger/util"
)
// validateAPIKeyForLogin can be overridden for testing
var validateAPIKeyForLogin = api.ValidateAPIKey
// nextStepsMessage is the message shown after successful login
const nextStepsMessage = `
🎉 Next steps:
• Install MCP server for your favorite AI coding tool: tiger mcp install
• List existing services: tiger service list
• Create a new service: tiger service create
`
type credentials struct {
publicKey string
secretKey string
projectID string
}
func buildLoginCmd() *cobra.Command {
var flags credentials
cmd := &cobra.Command{
Use: "login",
Short: "Authenticate with TigerData API",
Long: `Authenticate with TigerData API using predefined keys or an interactive OAuth flow
By default, the command will launch an interactive OAuth flow in your browser to create new API keys.
The OAuth flow will:
- Open your browser for authentication
- Let you select a project (if you have multiple)
- Create API keys automatically for the selected project
The keys will be combined and stored securely in the system keyring or as a fallback file.
The project ID will be stored in the configuration file.
You may also provide API keys via flags or environment variables, in which case
they will be used directly. The CLI will prompt for any missing information.
You can find your API credentials and project ID at: https://console.cloud.timescale.com/dashboard/settings
Examples:
# Interactive login with OAuth (opens browser, creates API keys automatically)
tiger auth login
# Login with project ID (will prompt for keys if not provided)
tiger auth login --project-id your-project-id
# Login with keys and project ID
tiger auth login --public-key your-public-key --secret-key your-secret-key --project-id your-project-id
# Login using environment variables
export TIGER_PUBLIC_KEY="your-public-key"
export TIGER_SECRET_KEY="your-secret-key"
export TIGER_PROJECT_ID="proj-123"
tiger auth login`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
// Get config
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// TODO: It should be possible to get the projectID corresponding to the
// API keys programmatically, making the project-id flag/env var unnecessary
creds := credentials{
publicKey: flagOrEnvVar(flags.publicKey, "TIGER_PUBLIC_KEY"),
secretKey: flagOrEnvVar(flags.secretKey, "TIGER_SECRET_KEY"),
projectID: flagOrEnvVar(flags.projectID, "TIGER_PROJECT_ID"),
}
if creds.publicKey == "" && creds.secretKey == "" && creds.projectID == "" {
// If no credentials were provided, start interactive OAuth login flow
l := &oauthLogin{
authURL: cfg.ConsoleURL + "/oauth/authorize",
tokenURL: cfg.GatewayURL + "/idp/external/cli/token",
successURL: cfg.ConsoleURL + "/oauth/code/success",
graphql: &GraphQLClient{
URL: cfg.GatewayURL + "/query",
},
out: cmd.OutOrStdout(),
}
creds, err = l.loginWithOAuth()
if err != nil {
return err
}
} else if creds.publicKey == "" || creds.secretKey == "" || creds.projectID == "" {
// If some credentials were provided, prompt for missing ones
creds, err = promptForCredentials(cfg.ConsoleURL, creds)
if err != nil {
return fmt.Errorf("failed to get credentials: %w", err)
}
if creds.publicKey == "" || creds.secretKey == "" {
return fmt.Errorf("both public key and secret key are required")
}
if creds.projectID == "" {
return fmt.Errorf("project ID is required")
}
}
// Combine the keys in the format "public:secret" for storage
apiKey := fmt.Sprintf("%s:%s", creds.publicKey, creds.secretKey)
// Validate the API key by making a test API call
fmt.Fprintln(cmd.OutOrStdout(), "Validating API key...")
if err := validateAPIKeyForLogin(apiKey, creds.projectID); err != nil {
return fmt.Errorf("API key validation failed: %w", err)
}
// Store the credentials (API key + project ID) together securely
if err := config.StoreCredentials(apiKey, creds.projectID); err != nil {
return fmt.Errorf("failed to store credentials: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "Successfully logged in (project: %s)\n", creds.projectID)
// Show helpful next steps
fmt.Fprint(cmd.OutOrStdout(), nextStepsMessage)
return nil
},
}
// Add flags
cmd.Flags().StringVar(&flags.publicKey, "public-key", "", "Public key for authentication")
cmd.Flags().StringVar(&flags.secretKey, "secret-key", "", "Secret key for authentication")
cmd.Flags().StringVar(&flags.projectID, "project-id", "", "Default project ID to set in configuration")
return cmd
}
func buildLogoutCmd() *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Remove stored credentials",
Long: `Remove stored API key and clear authentication credentials.`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if err := config.RemoveCredentials(); err != nil {
return fmt.Errorf("failed to remove credentials: %w", err)
}
fmt.Fprintln(cmd.OutOrStdout(), "Successfully logged out and removed stored credentials")
return nil
},
}
}
func buildStatusCmd() *cobra.Command {
return &cobra.Command{
Use: "status",
Short: "Show current authentication status and project ID",
Long: "Displays whether you are logged in and shows your currently configured project ID.",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
_, projectID, err := config.GetCredentials()
if err != nil {
return err
}
// TODO: Make API call to get token information
fmt.Fprintln(cmd.OutOrStdout(), "Logged in (API key stored)")
fmt.Fprintf(cmd.OutOrStdout(), "Project ID: %s\n", projectID)
return nil
},
}
}
func buildAuthCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "Manage authentication and credentials",
Long: `Manage authentication and credentials for TigerData Cloud Platform.`,
}
cmd.AddCommand(buildLoginCmd())
cmd.AddCommand(buildLogoutCmd())
cmd.AddCommand(buildStatusCmd())
return cmd
}
func flagOrEnvVar(flagVal, envVarName string) string {
if flagVal != "" {
return flagVal
}
return os.Getenv(envVarName)
}
// promptForCredentials prompts the user to enter any missing credentials
func promptForCredentials(consoleURL string, creds credentials) (credentials, error) {
// Check if we're in a terminal for interactive input
if !util.IsTerminal(os.Stdin) {
return credentials{}, fmt.Errorf("TTY not detected - credentials required. Use flags (--public-key, --secret-key, --project-id) or environment variables (TIGER_PUBLIC_KEY, TIGER_SECRET_KEY, TIGER_PROJECT_ID)")
}
fmt.Printf("You can find your API credentials and project ID at: %s/dashboard/settings\n\n", consoleURL)
reader := bufio.NewReader(os.Stdin)
// Prompt for public key if missing
if creds.publicKey == "" {
fmt.Print("Enter your public key: ")
publicKey, err := reader.ReadString('\n')
if err != nil {
return credentials{}, err
}
creds.publicKey = strings.TrimSpace(publicKey)
}
// Prompt for secret key if missing
if creds.secretKey == "" {
fmt.Print("Enter your secret key: ")
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return credentials{}, err
}
fmt.Println() // Print newline after hidden input
creds.secretKey = strings.TrimSpace(string(bytePassword))
}
// Prompt for project ID if missing
if creds.projectID == "" {
fmt.Print("Enter your project ID: ")
projectID, err := reader.ReadString('\n')
if err != nil {
return credentials{}, err
}
creds.projectID = strings.TrimSpace(projectID)
}
return creds, nil
}