-
Notifications
You must be signed in to change notification settings - Fork 1
/
register_user.go
299 lines (252 loc) · 6.17 KB
/
register_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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package registerUser
import (
"database/sql"
"encoding/binary"
"encoding/json"
"errors"
"github.com/bashar-saleh/auth-nanos/entities"
"github.com/bashar-saleh/gonanos/nanos"
"github.com/dgrijalva/jwt-go"
"golang.org/x/crypto/bcrypt"
"log"
"time"
)
func NewRegisterUserNanos(
workersMaxCount int,
taskQueueCapacity int,
db *sql.DB,
nameValidationRules []func(name string) (bool, string),
usernameValidationRules []func(username string) (bool, string),
passwordValidationRules []func(password string) (bool, string),
emailValidationRules []func(email string) (bool, string),
phoneValidationRules []func(phone string) (bool, string),
) chan nanos.Message {
worker := ®isterUserWorker{
db: db,
nameValidationRules: nameValidationRules,
emailValidationRules: emailValidationRules,
passwordValidationRules: passwordValidationRules,
phoneValidationRules: phoneValidationRules,
usernameValidationRules: usernameValidationRules,
}
worker.prepareStore()
myNanos := nanos.Nanos{
WorkersMaxCount: workersMaxCount,
TaskQueueCapacity: taskQueueCapacity,
Worker: worker,
}
return myNanos.TasksChannel()
}
type registerUserWorker struct {
db *sql.DB
nameValidationRules []func(name string) (bool, string)
usernameValidationRules []func(username string) (bool, string)
passwordValidationRules []func(password string) (bool, string)
emailValidationRules []func(email string) (bool, string)
phoneValidationRules []func(phone string) (bool, string)
}
func (w *registerUserWorker) Work(msg nanos.Message) {
// extract content from message
userData, err := entities.UserFromBytes(msg.Content)
if err != nil {
select {
case msg.ErrTo <- err:
return
default:
return
}
}
// validate user data
isValid, nonValidMsg := w.validate(userData)
if !isValid {
select {
case msg.ErrTo <- errors.New(nonValidMsg):
return
default:
return
}
}
// check if the username or email or phone exist before
err = w.isUserExist(userData)
if err != nil {
select {
case msg.ErrTo <- err:
return
default:
return
}
}
// saving to db
id, err := w.saveUserToDB(userData)
if err != nil {
select {
case msg.ErrTo <- err:
return
default:
return
}
}
// return response
rawID := make([]byte, 8)
binary.LittleEndian.PutUint64(rawID, uint64(id))
if err != nil {
select {
case msg.ErrTo <- err:
default:
return
}
}
select {
case msg.ResTo <- nanos.Message{Content: rawID}:
return
default:
return
}
}
func (w *registerUserWorker) hashPassword(pass string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.MinCost)
if err != nil {
return "", err
}
return string(hash), nil
}
func (w *registerUserWorker) prepareStore() {
// check if table exists
rows, err := w.db.Query("select name from sqlite_master where name='users' and type='table'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
if rows.Next() {
return
}
// create users table
stmt := `
create table users (
id integer not null primary key autoincrement,
name text,
username text,
password text,
email text,
phone text,
roles text
);
delete from users;`
_, err = w.db.Exec(stmt)
if err != nil {
log.Fatal(err)
}
}
func (w *registerUserWorker) validate(userData entities.User) (bool, string) {
// validate name
for i := range w.nameValidationRules {
isValid, nonValidMsg := w.nameValidationRules[i](userData.Name)
if !isValid {
return false, nonValidMsg
}
}
// validate username
for i := range w.usernameValidationRules {
isValid, nonValidMsg := w.usernameValidationRules[i](userData.Username)
if !isValid {
return false, nonValidMsg
}
}
// validate password
for i := range w.passwordValidationRules {
isValid, nonValidMsg := w.passwordValidationRules[i](userData.Password)
if !isValid {
return false, nonValidMsg
}
}
// validate email
for i := range w.emailValidationRules {
isValid, nonValidMsg := w.emailValidationRules[i](userData.Email)
if !isValid {
return false, nonValidMsg
}
}
// validate phone
for i := range w.phoneValidationRules {
isValid, nonValidMsg := w.phoneValidationRules[i](userData.Phone)
if !isValid {
return false, nonValidMsg
}
}
return true, ""
}
func (w *registerUserWorker) isUserExist(userData entities.User) error {
q := "SELECT username, email, phone FROM users WHERE "
var qValus []interface{}
if userData.Username != "" {
q += "(username = ?) "
qValus = append(qValus, userData.Username)
} else {
q += "(0 = 1) "
}
if userData.Email != "" {
q += "OR (email = ?) "
qValus = append(qValus, userData.Email)
} else {
q += "OR (1 = 0) "
}
if userData.Phone != "" {
q += "OR (phone = ?) "
qValus = append(qValus, userData.Phone)
} else {
q += "OR (1 = 0) "
}
rows, err := w.db.Query(q, qValus...)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var username string
var email string
var phone string
err := rows.Scan(&username, &email, &phone)
if err != nil {
return err
}
return errors.New("User with data: username=" + username + " email=" + email + " phone=" + phone + " is exist before")
}
return nil
}
func (w *registerUserWorker) saveUserToDB(userData entities.User) (int64, error) {
tx, err := w.db.Begin()
if err != nil {
return 0, nil
}
// stringify roles
var rolesString string
if len(userData.Roles) == 0 || userData.Roles == nil {
rolesString = ""
} else {
raw, err := json.Marshal(userData.Roles)
if err != nil {
return 0, err
}
rolesString = string(raw)
}
stmt, err := tx.Prepare("insert into users (name, username, email, phone,password, roles) values (?, ?, ?, ?, ?, ?)")
if err != nil {
return 0, err
}
defer stmt.Close()
// hashing password
hashedPassword, err := w.hashPassword(userData.Password)
result, err := stmt.Exec(userData.Name, userData.Username, userData.Email, userData.Phone, hashedPassword, rolesString)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
err = tx.Commit()
if err != nil {
return 0, err
}
return id, nil
}