-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.go
More file actions
135 lines (116 loc) · 3.21 KB
/
auth.go
File metadata and controls
135 lines (116 loc) · 3.21 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
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/BurntSushi/toml"
"github.com/getlantern/lantern-headless-client/auth"
"github.com/pterm/pterm"
)
type authData struct {
UserID int64
UserToken string
}
func readAuth() {
var data authData
if _, err := toml.DecodeFile(configFilePath, &data); err != nil {
pterm.Debug.Printfln("Error reading config. Using defaults. Error: %s", err.Error())
userToken = ""
userID = 0
} else {
pterm.Debug.Printfln("Read config: %v", data)
userID = data.UserID
userToken = data.UserToken
}
}
func login(ctx context.Context, client auth.Client, email, password string) error {
sp, _ := spinner.Start("Logging in")
defer sp.Stop()
resp, _, err := client.Login(ctx, email, password, deviceId)
if err != nil || !resp.Success {
return fmt.Errorf("error logging in: %w", err)
}
userID = resp.LegacyID
userToken = resp.LegacyToken
pterm.Success.Println("Logged in successfully")
return persistAuth()
}
func persistAuth() error {
f, err := os.Create(configFilePath)
if err != nil {
return fmt.Errorf("error creating config file: %w", err)
}
defer func(f *os.File) {
_ = f.Close()
}(f)
if err = toml.NewEncoder(f).Encode(authData{
userID,
userToken,
}); err != nil {
return fmt.Errorf("error writing config: %w", err)
}
return nil
}
var passwordInput = pterm.DefaultInteractiveTextInput.WithMask("*")
func askForCredentials(inputEmail, inputPassword string) (string, string) {
email, password := inputEmail, inputPassword
if inputEmail == "" {
email, _ = pterm.DefaultInteractiveTextInput.Show("Enter your email")
}
if inputPassword == "" {
password, _ = passwordInput.Show("Enter your password")
}
if email == "" || password == "" {
pterm.Error.Println("Email and password are required")
os.Exit(2)
}
return email, password
}
func signup(ctx context.Context, client auth.Client, email, password string) error {
sp, _ := spinner.Start("Signing up")
defer sp.Stop()
_, err := client.SignUp(ctx, email, password)
if err != nil {
return fmt.Errorf("error signing up: %w", err)
}
code, _ := passwordInput.Show("Please confirm your email address by entering the code sent to your email")
if result, err := client.SignupEmailConfirmation(ctx, &auth.ConfirmSignupRequest{
Email: email,
Code: code,
}); !result || err != nil {
return fmt.Errorf("error confirming email: %w", err)
}
pterm.Info.Println("Email confirmed successfully, logging in")
return login(ctx, client, email, password)
}
func logout() {
userID = 0
userToken = ""
_ = persistAuth()
}
func authCmd(ctx context.Context, cmd *AuthCmd, overrideAuthURL string, logWriter io.Writer) {
authUrl := auth.DefaultAPIURL
if overrideAuthURL != "" {
authUrl = overrideAuthURL
}
client := auth.NewClient(authUrl, args.Insecure, logWriter)
var err error
switch {
case cmd.Login != nil:
u, p := askForCredentials(cmd.Login.Email, cmd.Login.Password)
err = login(ctx, client, u, p)
case cmd.Signup != nil:
u, p := askForCredentials(cmd.Signup.Email, cmd.Signup.Password)
err = signup(ctx, client, u, p)
case cmd.Logout != nil:
logout()
default:
printHelp()
os.Exit(2)
}
if err != nil {
pterm.Error.Printfln("Error authenticating: %s", err.Error())
os.Exit(2)
}
}