-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
216 lines (189 loc) · 5.31 KB
/
account.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
package commands
import (
"fmt"
"os"
"strconv"
"github.com/adrianosela/padl/cli/config"
"github.com/adrianosela/padl/lib/keymgr"
"github.com/adrianosela/padl/lib/keys"
"github.com/olekukonko/tablewriter"
cli "gopkg.in/urfave/cli.v1"
)
const (
defaultHostURL = "https://padl.adrianosela.com"
)
// AccountCmds - manage accounts and auth flows
var AccountCmds = cli.Command{
Name: "account",
Aliases: []string{"a"},
Usage: "Manage accounts and authentication",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create a new account on a padl server",
Flags: []cli.Flag{
emailFlag,
passwordFlag,
},
Before: createConfigIfDoesNotExist,
Action: createAccountHandler,
},
{
Name: "login",
Usage: "login to an account on a padl server",
Flags: []cli.Flag{
emailFlag,
passwordFlag,
pathFlag,
},
Before: createConfigIfDoesNotExist,
Action: loginAccountHandler,
},
{
Name: "rotate-key",
Usage: "create a fresh user key and publish the public key",
Flags: []cli.Flag{
pathFlag,
},
Action: rotateKeyHandler,
},
{
Name: "show",
Usage: "show account details based on padl token",
Flags: []cli.Flag{
jsonFlag,
},
Action: showAccountHandler,
},
},
}
func createConfigIfDoesNotExist(ctx *cli.Context) error {
cPath := ctx.GlobalString(name(ConfigFlag))
if _, err := config.GetConfig(cPath); err == nil {
return nil
}
c := &config.Config{HostURL: defaultHostURL}
if err := config.SetConfig(c, cPath); err != nil {
return fmt.Errorf("could not set configuration: %s", err)
}
return nil
}
func createAccountHandler(ctx *cli.Context) error {
c, err := getClient(ctx)
if err != nil {
return fmt.Errorf("could not initialize client: %s", err)
}
email := ctx.String(name(emailFlag))
if email == "" {
if email, err = promptText("Enter your email:", false); err != nil {
return fmt.Errorf("could not read user email")
}
}
pass := ctx.String(name(passwordFlag))
if pass == "" {
if pass, err = promptText("Enter your password:", true); err != nil {
return fmt.Errorf("could not read user password")
}
}
priv, pub, err := keys.GenerateRSAKeyPair(4096)
if err != nil {
return fmt.Errorf("could not generate key pair: %s", err)
}
// register user
err = c.Register(email, pass, string(keys.EncodePubKeyPEM(pub)))
if err != nil {
return err
}
// save private key in filesystem
keyMgr, err := keymgr.NewFSManager(config.GetDefaultPath())
if err != nil {
return fmt.Errorf("could not establish key manager: %s", err)
}
if err = keyMgr.PutPriv(keys.GetFingerprint(pub), string(keys.EncodePrivKeyPEM(priv))); err != nil {
return fmt.Errorf("could not save private key: %s", err)
}
fmt.Printf("registered user %s successfully!\n", email)
return nil
}
func rotateKeyHandler(ctx *cli.Context) error {
c, err := getClient(ctx)
if err != nil {
return fmt.Errorf("could not initialize client: %s", err)
}
// create new key
priv, pub, err := keys.GenerateRSAKeyPair(4096)
if err != nil {
return fmt.Errorf("could not generate key pair: %s", err)
}
// save private key in filesystem
keyMgr, err := keymgr.NewFSManager(config.GetDefaultPath())
if err != nil {
return fmt.Errorf("could not establish key manager: %s", err)
}
if err = keyMgr.PutPriv(keys.GetFingerprint(pub), string(keys.EncodePrivKeyPEM(priv))); err != nil {
return fmt.Errorf("could not save private key: %s", err)
}
// rotate key
if err = c.RotateUserKey(string(keys.EncodePubKeyPEM(pub))); err != nil {
return err
}
fmt.Println("rotated user key successfully!")
return nil
}
func loginAccountHandler(ctx *cli.Context) error {
c, err := getClient(ctx)
if err != nil {
return fmt.Errorf("could not initialize client: %s", err)
}
path := ctx.String(name(pathFlag))
email := ctx.String(name(emailFlag))
if email == "" {
if email, err = promptText("Enter your email:", false); err != nil {
return fmt.Errorf("could not read user email")
}
}
pass := ctx.String(name(passwordFlag))
if pass == "" {
if pass, err = promptText("Enter your password:", true); err != nil {
return fmt.Errorf("could not read user password")
}
}
tk, err := c.Login(email, pass)
if err != nil {
return fmt.Errorf("could not log in: %s", err)
}
conf, err := config.GetConfig(path)
if err != nil {
return fmt.Errorf("could not get config from file system: %s", err)
}
conf.User = email
conf.Token = tk
if err = config.SetConfig(conf, path); err != nil {
return fmt.Errorf("could not write config to file system: %s", err)
}
fmt.Printf("user %s logged in successfully!\n", email)
return nil
}
func showAccountHandler(ctx *cli.Context) error {
c, err := getClient(ctx)
if err != nil {
return fmt.Errorf("could not initialize client: %s", err)
}
claims, err := c.Valid()
if err != nil {
return fmt.Errorf("could not get token details: %s", err)
}
if ctx.Bool(name(jsonFlag)) {
return printJSON(&claims)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetAlignment(tablewriter.ALIGN_RIGHT)
table.Append([]string{"aud", claims.Audience})
table.Append([]string{"iss", claims.Issuer})
table.Append([]string{"sub", claims.Subject})
table.Append([]string{"iat", strconv.FormatInt(claims.IssuedAt, 10)})
table.Append([]string{"exp", strconv.FormatInt(claims.ExpiresAt, 10)})
table.Append([]string{"jti", claims.Id})
table.Render()
return nil
}