-
Notifications
You must be signed in to change notification settings - Fork 9
/
user.go
165 lines (140 loc) · 3.4 KB
/
user.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
package cli
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
homedir "github.com/mitchellh/go-homedir"
"github.com/spolu/settle/lib/env"
"github.com/spolu/settle/lib/errors"
"github.com/spolu/settle/lib/out"
"github.com/spolu/settle/mint"
)
// Credentials rerpesents the credentials of the currently logged in user.
type Credentials struct {
Username string `json:"username"`
Host string `json:"mint"`
Password string `json:"password"`
}
const (
// credentialsKey the context.Context key to store the credentials
credentialsKey ContextKey = "cli.credentials"
)
// WithCredentials stores the credentials in the provided context.
func WithCredentials(
ctx context.Context,
credentials *Credentials,
) context.Context {
return context.WithValue(ctx, credentialsKey, credentials)
}
// GetCredentials returns the credentials currently stored in the context.
func GetCredentials(
ctx context.Context,
) *Credentials {
return ctx.Value(credentialsKey).(*Credentials)
}
// CredentialsPath returns the crendentials path for the current environment.
func CredentialsPath(
ctx context.Context,
) (*string, error) {
path, err := homedir.Expand(
fmt.Sprintf("~/.settle/credentials-%s.json", env.Get(ctx).Environment))
if err != nil {
return nil, errors.Trace(err)
}
err = os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
return nil, errors.Trace(err)
}
return &path, nil
}
// CurrentUser retrieves the current user by reading CredentialsPath.
func CurrentUser(
ctx context.Context,
) (*Credentials, error) {
path, err := CredentialsPath(ctx)
if err != nil {
return nil, errors.Trace(err)
}
if _, err := os.Stat(*path); os.IsNotExist(err) {
return nil, nil
}
raw, err := ioutil.ReadFile(*path)
if err != nil {
return nil, errors.Trace(err)
}
var c Credentials
err = json.Unmarshal(raw, &c)
if err != nil {
return nil, errors.Trace(err)
}
return &c, nil
}
// Login logs the user in by storing its credentials after valdation in
// CredentialsPath.
func Login(
ctx context.Context,
address string,
password string,
) error {
username, host, err := mint.UsernameAndMintHostFromAddress(ctx, address)
if err != nil {
return errors.Trace(err)
}
creds := &Credentials{
Host: host,
Username: username,
Password: password,
}
// Check the credentials validity.
m := &Mint{
Host: creds.Host,
Credentials: creds,
}
status, raw, err := m.Get(ctx, "/balances", url.Values{})
if err != nil {
return errors.Trace(err)
}
if *status != http.StatusOK {
var e errors.ConcreteUserError
err = raw.Extract("error", &e)
if err != nil {
return errors.Trace(err)
}
return errors.Trace(
errors.Newf("(%s) %s", e.ErrCode, e.ErrMessage))
}
path, err := CredentialsPath(ctx)
if err != nil {
return errors.Trace(err)
}
formatted, err := json.MarshalIndent(creds, "", " ")
if err != nil {
return errors.Trace(err)
}
err = ioutil.WriteFile(*path, formatted, 0644)
if err != nil {
return errors.Trace(err)
}
out.Statf("[Storing credentials] file=%s\n", *path)
return nil
}
// Logout logs the user out by destoying its credentials at CredentialsPath.
func Logout(
ctx context.Context,
) error {
path, err := CredentialsPath(ctx)
if err != nil {
return errors.Trace(err)
}
err = os.Remove(*path)
if err != nil {
return errors.Trace(err)
}
out.Statf("[Erasing credentials] file=%s\n", *path)
return nil
}