/
login.go
53 lines (43 loc) · 1.34 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
package client
import (
"github.com/chzyer/readline"
"github.com/mdouchement/standardfile/pkg/libsf"
"github.com/pkg/errors"
)
// Login connects to a StandardFile server.
func Login() error {
cfg := Config{}
endpoint, err := readline.Line("Endpoint: ")
if err != nil {
return errors.Wrap(err, "could not read endpoint from stdin")
}
cfg.Endpoint = endpoint
client, err := libsf.NewDefaultClient(cfg.Endpoint)
if err != nil {
return errors.Wrap(err, "could not reach given endpoint")
}
cfg.Email, err = readline.Line("Email: ")
if err != nil {
return errors.Wrap(err, "could not read email from stdin")
}
auth, err := client.GetAuthParams(cfg.Email)
if err != nil {
return errors.Wrap(err, "could not get auth params")
}
if err = auth.IntegrityCheck(); err != nil {
return errors.Wrap(err, "invalid auth params")
}
password, err := readline.Password("Password: ")
if err != nil {
return errors.Wrap(err, "could not read password from stdin")
}
cfg.KeyChain = *auth.SymmetricKeyPair(string(password))
err = client.Login(auth.Email(), cfg.KeyChain.Password)
if err != nil {
return errors.Wrap(err, "could not login")
}
cfg.BearerToken = client.BearerToken() // JWT or access token
cfg.Session = client.Session() // Can be empty if a JWT is used
cfg.KeyChain.Password = "" // Bearer is used instead
return Save(cfg)
}