-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathclient_login.go
More file actions
213 lines (168 loc) · 5.14 KB
/
client_login.go
File metadata and controls
213 lines (168 loc) · 5.14 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
package login
import (
"fmt"
"io"
"net/url"
"os"
log "github.com/sirupsen/logrus"
"github.com/briandowns/spinner"
"github.com/hookdeck/hookdeck-cli/pkg/ansi"
configpkg "github.com/hookdeck/hookdeck-cli/pkg/config"
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
"github.com/hookdeck/hookdeck-cli/pkg/open"
"github.com/hookdeck/hookdeck-cli/pkg/validators"
)
var openBrowser = open.Browser
var canOpenBrowser = open.CanOpenBrowser
// Login function is used to obtain credentials via hookdeck dashboard.
func Login(config *configpkg.Config, input io.Reader) error {
var s *spinner.Spinner
if config.Profile.APIKey != "" {
log.WithFields(log.Fields{
"prefix": "login.Login",
"APIKey": config.Profile.APIKey,
}).Debug("Logging in with API key")
s = ansi.StartNewSpinner("Verifying credentials...", os.Stdout)
response, err := config.GetAPIClient().ValidateAPIKey()
if err != nil {
ansi.StopSpinner(s, "", os.Stdout)
if !hookdeck.IsUnauthorizedError(err) {
return err
}
// Rejected key: continue into browser login below (must clear key first
// or we would re-enter this branch only).
fmt.Fprintln(os.Stdout, "Your saved API key is no longer valid. Starting browser sign-in...")
config.Profile.APIKey = ""
} else {
message := SuccessMessage(response.UserName, response.UserEmail, response.OrganizationName, response.ProjectName, response.ProjectMode == "console")
ansi.StopSpinner(s, message, os.Stdout)
config.Profile.ApplyValidateAPIKeyResponse(response, true)
if err = config.Profile.SaveProfile(); err != nil {
return err
}
if err = config.Profile.UseProfile(); err != nil {
return err
}
return nil
}
}
parsedBaseURL, err := url.Parse(config.APIBaseURL)
if err != nil {
return err
}
client := &hookdeck.Client{
BaseURL: parsedBaseURL,
TelemetryDisabled: config.TelemetryDisabled,
}
session, err := client.StartLogin(config.DeviceName)
if err != nil {
return err
}
if isSSH() || !canOpenBrowser() {
fmt.Printf("To authenticate with Hookdeck, please go to: %s\n", session.BrowserURL)
s = ansi.StartNewSpinner("Waiting for confirmation...", os.Stdout)
} else {
fmt.Printf("Press Enter to open the browser (^C to quit)")
fmt.Fscanln(input)
s = ansi.StartNewSpinner("Waiting for confirmation...", os.Stdout)
err = openBrowser(session.BrowserURL)
if err != nil {
msg := fmt.Sprintf("Failed to open browser, please go to %s manually.", session.BrowserURL)
ansi.StopSpinner(s, msg, os.Stdout)
s = ansi.StartNewSpinner("Waiting for confirmation...", os.Stdout)
}
}
response, err := session.WaitForAPIKey(0, 0)
if err != nil {
return err
}
err = validators.APIKey(response.APIKey)
if err != nil {
return err
}
config.Profile.ApplyPollAPIKeyResponse(response, "")
if err = config.Profile.SaveProfile(); err != nil {
return err
}
if err = config.Profile.UseProfile(); err != nil {
return err
}
config.RefreshCachedAPIClient()
message := SuccessMessage(response.UserName, response.UserEmail, response.OrganizationName, response.ProjectName, response.ProjectMode == "console")
ansi.StopSpinner(s, message, os.Stdout)
return nil
}
func GuestLogin(config *configpkg.Config) (string, error) {
parsedBaseURL, err := url.Parse(config.APIBaseURL)
if err != nil {
return "", err
}
client := &hookdeck.Client{
BaseURL: parsedBaseURL,
TelemetryDisabled: config.TelemetryDisabled,
}
fmt.Println("\n🚩 You are using the CLI for the first time without a permanent account. Creating a guest account...")
session, err := client.StartGuestLogin(config.DeviceName)
if err != nil {
return "", err
}
response, err := session.WaitForAPIKey(0, 0)
if err != nil {
return "", err
}
if err = validators.APIKey(response.APIKey); err != nil {
return "", err
}
config.Profile.ApplyPollAPIKeyResponse(response, session.GuestURL)
if err = config.Profile.SaveProfile(); err != nil {
return "", err
}
if err = config.Profile.UseProfile(); err != nil {
return "", err
}
return session.GuestURL, nil
}
func CILogin(config *configpkg.Config, apiKey string, name string) error {
parsedBaseURL, err := url.Parse(config.APIBaseURL)
if err != nil {
return err
}
client := &hookdeck.Client{
BaseURL: parsedBaseURL,
APIKey: apiKey,
TelemetryDisabled: config.TelemetryDisabled,
}
deviceName := name
if deviceName == "" {
deviceName = config.DeviceName
}
response, err := client.CreateCIClient(hookdeck.CreateCIClientInput{
DeviceName: deviceName,
})
if err != nil {
return err
}
if err := validators.APIKey(response.APIKey); err != nil {
return err
}
config.Profile.ApplyCIClient(response)
if err = config.Profile.SaveProfile(); err != nil {
return err
}
if err = config.Profile.UseProfile(); err != nil {
return err
}
color := ansi.Color(os.Stdout)
log.Println(fmt.Sprintf(
"The Hookdeck CLI is configured on project %s in organization %s\n",
color.Bold(response.ProjectName),
color.Bold(response.OrganizationName),
))
return nil
}
func isSSH() bool {
if os.Getenv("SSH_TTY") != "" || os.Getenv("SSH_CONNECTION") != "" || os.Getenv("SSH_CLIENT") != "" {
return true
}
return false
}