forked from qbee-io/qbee-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogin.go
294 lines (236 loc) · 7.2 KB
/
login.go
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2023 qbee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
)
const loginPath = "/api/v2/login"
// LoginRequest is the request body for the Login API.
type LoginRequest struct {
// Email is the user email.
Email string `json:"email"`
// Password is the user password.
Password string `json:"password"`
}
// LoginResponse is the response body for the Login API.
type LoginResponse struct {
// Token is the authentication token to be used as Bearer token in the Authorization header.
Token string `json:"token"`
}
// LoginConfig is the configuration file for the CLI authentication.
type LoginConfig struct {
AuthToken string `json:"token"`
RefreshToken string `json:"refresh_token"`
BaseURL string `json:"base_url"`
}
// LoginWriteConfig writes the CLI authentication configuration to the user's home directory.
func LoginWriteConfig(config LoginConfig) error {
dirname, err := os.UserHomeDir()
if err != nil {
return err
}
qbeeConfigDir := filepath.Join(dirname, ".qbee")
if err := os.MkdirAll(qbeeConfigDir, 0700); err != nil {
return err
}
configFile := filepath.Join(qbeeConfigDir, "qbee-cli.json")
jsonConfig, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
err = os.WriteFile(configFile, jsonConfig, 0600)
return err
}
// LoginReadConfig reads the CLI authentication configuration from the user's home directory.
func LoginReadConfig() (*LoginConfig, error) {
dirname, err := os.UserHomeDir()
if err != nil {
return nil, err
}
qbeeConfigDir := filepath.Join(dirname, ".qbee")
if err := os.MkdirAll(qbeeConfigDir, 0700); err != nil {
return nil, err
}
configFile := filepath.Join(qbeeConfigDir, "qbee-cli.json")
jsonConfig, err := os.ReadFile(configFile)
if err != nil {
return nil, err
}
config := new(LoginConfig)
if err := json.Unmarshal(jsonConfig, config); err != nil {
return nil, err
}
return config, nil
}
// LoginGetAuthenticatedClient returns a new authenticated API Client.
func LoginGetAuthenticatedClient(ctx context.Context) (*Client, error) {
if os.Getenv("QBEE_EMAIL") != "" && os.Getenv("QBEE_PASSWORD") != "" {
email := os.Getenv("QBEE_EMAIL")
password := os.Getenv("QBEE_PASSWORD")
cli := New()
if os.Getenv("QBEE_BASEURL") != "" {
cli = cli.WithBaseURL(os.Getenv("QBEE_BASEURL"))
}
if err := cli.Authenticate(ctx, email, password); err != nil {
return nil, err
}
return cli, nil
}
var config *LoginConfig
var err error
if config, err = LoginReadConfig(); err != nil {
return nil, err
}
client := New().WithBaseURL(config.BaseURL).
WithAuthToken(config.AuthToken).
WithRefreshToken(config.RefreshToken)
if err := client.RefreshToken(ctx); err != nil {
return nil, fmt.Errorf("unable to refresh token, please login again: %w", err)
}
if err := LoginWriteConfig(*config); err != nil {
return nil, err
}
return client, nil
}
// Login returns a new authenticated API Client.
func (cli *Client) Login(ctx context.Context, email, password string) (string, error) {
request := &LoginRequest{
Email: email,
Password: password,
}
response := new(LoginResponse)
if err := cli.Call(ctx, http.MethodPost, loginPath, request, &response); err != nil {
// If the error is an API error, check if it's a 2FA challenge.
if apiError := make(Error); errors.As(err, &apiError) {
if challenge, has2FAChallenge := apiError["challenge"].(string); has2FAChallenge {
return cli.Login2FA(ctx, challenge)
}
return "", err
}
}
return response.Token, nil
}
// Login2FAMethod is a container for the 2FA provider and code used during login.
type Login2FAMethod struct {
Provider string
Code string
}
// Login2FARequest contains the request body for the Login 2FA API.
type Login2FARequest struct {
Challenge string `json:"challenge,omitempty"`
Provider string `json:"preferProvider,omitempty"`
Code string `json:"code,omitempty"`
}
// Login2FAResponse is the response body for the Login 2FA API.
type Login2FAResponse struct {
Challenge string `json:"challenge,omitempty"`
Token string `json:"token,omitempty"`
}
var valid2FAProviders = []string{"google", "email"}
const login2FAChallengeGetPath = "/api/v2/challenge-get"
const login2FAChallengeVerifyPath = "/api/v2/challenge-verify"
// Login2FA returns a new authenticated API Client.
func (cli *Client) Login2FA(ctx context.Context, challenge string) (string, error) {
login2FAMethod := findProvided2FAMethod()
if login2FAMethod == nil {
var err error
provider, err := prompt2FAProvider()
if err != nil {
return "", err
}
login2FAMethod = &Login2FAMethod{
Provider: provider,
}
}
challengeGetRequest := &Login2FARequest{
Challenge: challenge,
Provider: login2FAMethod.Provider,
}
challengeGetResponse := new(Login2FAResponse)
if err := cli.Call(ctx, http.MethodPost, login2FAChallengeGetPath, challengeGetRequest, &challengeGetResponse); err != nil {
return "", err
}
// The code might already have been provided as an environment variable.
if login2FAMethod.Code == "" {
code, err := prompt2FACode()
if err != nil {
return "", err
}
login2FAMethod.Code = code
}
challengeVerifyRequest := &Login2FARequest{
Challenge: challengeGetResponse.Challenge,
Code: login2FAMethod.Code,
}
challengeVerifyResponse := new(Login2FAResponse)
if err := cli.Call(ctx, http.MethodPost, login2FAChallengeVerifyPath, challengeVerifyRequest, &challengeVerifyResponse); err != nil {
return "", err
}
return challengeVerifyResponse.Token, nil
}
func findProvided2FAMethod() *Login2FAMethod {
if os.Getenv("QBEE_2FA_CODE") != "" {
fmt.Printf("Using 2FA code from environment variable QBEE_2FA_CODE as a google 2FA provider\n")
return &Login2FAMethod{
Provider: "google",
Code: os.Getenv("QBEE_2FA_CODE"),
}
}
return nil
}
func prompt2FAProvider() (string, error) {
fmt.Printf("Select 2FA provider:\n")
for i, provider := range valid2FAProviders {
fmt.Printf("%d) %s\n", i+1, provider)
}
fmt.Printf("Choice: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil {
log.Fatal(err)
}
providerIndex := scanner.Text()
index, err := strconv.Atoi(providerIndex)
if err != nil {
return "", err
}
if index < 1 || index > len(valid2FAProviders) {
return "", fmt.Errorf("invalid provider")
}
provider := valid2FAProviders[index-1]
return provider, nil
}
func prompt2FACode() (string, error) {
fmt.Printf("Enter 2FA code: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil {
return "", err
}
code := scanner.Text()
return code, nil
}