-
Notifications
You must be signed in to change notification settings - Fork 119
/
login.go
108 lines (88 loc) · 2.67 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
package login
import (
"encoding/base64"
"strconv"
"github.com/dnote/cli/client"
"github.com/dnote/cli/core"
"github.com/dnote/cli/crypt"
"github.com/dnote/cli/infra"
"github.com/dnote/cli/log"
"github.com/dnote/cli/utils"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var example = `
dnote login`
// NewCmd returns a new login command
func NewCmd(ctx infra.DnoteCtx) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Login to dnote server",
Example: example,
RunE: newRun(ctx),
}
return cmd
}
// Do dervies credentials on the client side and requests a session token from the server
func Do(ctx infra.DnoteCtx, email, password string) error {
presigninResp, err := client.GetPresignin(ctx, email)
if err != nil {
return errors.Wrap(err, "getting presiginin")
}
masterKey, authKey, err := crypt.MakeKeys([]byte(password), []byte(email), presigninResp.Iteration)
if err != nil {
return errors.Wrap(err, "making keys")
}
authKeyB64 := base64.StdEncoding.EncodeToString(authKey)
signinResp, err := client.Signin(ctx, email, authKeyB64)
if err != nil {
return errors.Wrap(err, "requesting session")
}
cipherKeyDec, err := crypt.AesGcmDecrypt(masterKey, signinResp.CipherKeyEnc)
if err != nil {
return errors.Wrap(err, "decrypting cipher key")
}
cipherKeyDecB64 := base64.StdEncoding.EncodeToString(cipherKeyDec)
db := ctx.DB
tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "beginning a transaction")
}
if err := core.UpsertSystem(tx, infra.SystemCipherKey, cipherKeyDecB64); err != nil {
return errors.Wrap(err, "saving enc key")
}
if err := core.UpsertSystem(tx, infra.SystemSessionKey, signinResp.Key); err != nil {
return errors.Wrap(err, "saving session key")
}
if err := core.UpsertSystem(tx, infra.SystemSessionKeyExpiry, strconv.FormatInt(signinResp.ExpiresAt, 10)); err != nil {
return errors.Wrap(err, "saving session key")
}
tx.Commit()
return nil
}
func newRun(ctx infra.DnoteCtx) core.RunEFunc {
return func(cmd *cobra.Command, args []string) error {
var email, password string
if err := utils.PromptInput("email", &email); err != nil {
return errors.Wrap(err, "getting email input")
}
if email == "" {
return errors.New("Email is empty")
}
if err := utils.PromptPassword("password", &password); err != nil {
return errors.Wrap(err, "getting password input")
}
if password == "" {
return errors.New("Password is empty")
}
err := Do(ctx, email, password)
if errors.Cause(err) == client.ErrInvalidLogin {
log.Error("wrong login\n")
return nil
} else if err != nil {
return errors.Wrap(err, "logging in")
}
log.Success("logged in\n")
return nil
}
}