/
user.go
175 lines (152 loc) · 3.91 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
166
167
168
169
170
171
172
173
174
175
package model
import (
"context"
"encoding/base64"
"time"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/spolu/settle/lib/db"
"github.com/spolu/settle/lib/errors"
"github.com/spolu/settle/lib/token"
"golang.org/x/crypto/scrypt"
)
// User represents a user object. User objects are not managed by the mint and
// solely accesed in read-only mode, leaving user management to an external
// system with access to the same underlying mintDB.
type User struct {
Token string
Created time.Time
Username string
PasswordHash string `db:"password_hash"`
}
// CreateUser creates and stores a new User object.
func CreateUser(
ctx context.Context,
username string,
password string,
) (*User, error) {
user := User{
Token: token.New("user"),
Created: time.Now().UTC(),
Username: username,
}
h, err := scrypt.Key([]byte(password), []byte(user.Token), 16384, 8, 1, 64)
if err != nil {
return nil, errors.Trace(err)
}
user.PasswordHash = base64.StdEncoding.EncodeToString(h)
ext := db.Ext(ctx, "mint")
if _, err := sqlx.NamedExec(ext, `
INSERT INTO users
(token, created, username, password_hash)
VALUES
(:token, :created, :username, :password_hash)
`, user); err != nil {
switch err := err.(type) {
case *pq.Error:
if err.Code.Name() == "unique_violation" {
return nil, errors.Trace(ErrUniqueConstraintViolation{err})
}
case sqlite3.Error:
if err.ExtendedCode == sqlite3.ErrConstraintUnique {
return nil, errors.Trace(ErrUniqueConstraintViolation{err})
}
}
return nil, errors.Trace(err)
}
return &user, nil
}
// Save updates the object database representation with the in-memory values.
func (u *User) Save(
ctx context.Context,
) error {
ext := db.Ext(ctx, "mint")
_, err := sqlx.NamedExec(ext, `
UPDATE users
SET username = :username, password_hash = :password_hash
WHERE token = :token
`, u)
if err != nil {
return errors.Trace(err)
}
return nil
}
// LoadUserByToken attempts to load a user with the given user token.
func LoadUserByToken(
ctx context.Context,
token string,
) (*User, error) {
user := User{
Token: token,
}
ext := db.Ext(ctx, "mint")
if rows, err := sqlx.NamedQuery(ext, `
SELECT *
FROM users
WHERE token = :token
`, user); err != nil {
return nil, errors.Trace(err)
} else if !rows.Next() {
return nil, nil
} else if err := rows.StructScan(&user); err != nil {
defer rows.Close()
return nil, errors.Trace(err)
} else if err := rows.Close(); err != nil {
return nil, errors.Trace(err)
}
return &user, nil
}
// LoadUserByUsername attempts to load a user with the given username.
func LoadUserByUsername(
ctx context.Context,
username string,
) (*User, error) {
user := User{
Username: username,
}
ext := db.Ext(ctx, "mint")
if rows, err := sqlx.NamedQuery(ext, `
SELECT *
FROM users
WHERE username = :username
`, user); err != nil {
return nil, errors.Trace(err)
} else if !rows.Next() {
return nil, nil
} else if err := rows.StructScan(&user); err != nil {
defer rows.Close()
return nil, errors.Trace(err)
} else if err := rows.Close(); err != nil {
return nil, errors.Trace(err)
}
return &user, nil
}
// CheckPassword checks if the provided password matches the password hash
// associated with that user.
func (u *User) CheckPassword(
ctx context.Context,
password string,
) error {
h, err := scrypt.Key([]byte(password), []byte(u.Token), 16384, 8, 1, 64)
if err != nil {
return errors.Trace(err)
}
if u.PasswordHash != base64.StdEncoding.EncodeToString(h) {
return errors.Newf("Password mismatch")
}
return nil
}
// UpdatePassword updates the password hash in memory using the provided
// password value.
func (u *User) UpdatePassword(
ctx context.Context,
password string,
) error {
h, err := scrypt.Key([]byte(password), []byte(u.Token), 16384, 8, 1, 64)
if err != nil {
return errors.Trace(err)
}
u.PasswordHash = base64.StdEncoding.EncodeToString(h)
return nil
}