forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
355 lines (289 loc) · 8.67 KB
/
auth.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package auth
import (
"chat/channel"
"chat/globals"
"chat/utils"
"context"
"database/sql"
"errors"
"fmt"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
)
func ParseToken(c *gin.Context, token string) *User {
instance, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return []byte(viper.GetString("secret")), nil
})
if err != nil {
return nil
}
if claims, ok := instance.Claims.(jwt.MapClaims); ok && instance.Valid {
if int64(claims["exp"].(float64)) < time.Now().Unix() {
return nil
}
user := &User{
Username: claims["username"].(string),
Password: claims["password"].(string),
}
if !user.Validate(c) {
return nil
}
return user
}
return nil
}
func ParseApiKey(c *gin.Context, key string) *User {
db := utils.GetDBFromContext(c)
if len(key) == 0 {
return nil
}
var user User
if err := globals.QueryRowDb(db, `
SELECT auth.id, auth.username, auth.password FROM auth
INNER JOIN apikey ON auth.id = apikey.user_id
WHERE apikey.api_key = ?
`, key).Scan(&user.ID, &user.Username, &user.Password); err != nil {
return nil
}
return &user
}
func getCode(c *gin.Context, cache *redis.Client, email string) string {
code, err := cache.Get(c, fmt.Sprintf("nio:otp:%s", email)).Result()
if err != nil {
return ""
}
return code
}
func checkCode(c *gin.Context, cache *redis.Client, email, code string) bool {
storage := getCode(c, cache, email)
if len(storage) == 0 {
return false
}
if storage != code {
return false
}
cache.Del(c, fmt.Sprintf("nio:top:%s", email))
return true
}
func setCode(c *gin.Context, cache *redis.Client, email, code string) {
cache.Set(c, fmt.Sprintf("nio:otp:%s", email), code, 5*time.Minute)
}
func generateCode(c *gin.Context, cache *redis.Client, email string) string {
code := utils.GenerateCode(6)
setCode(c, cache, email, code)
return code
}
func Verify(c *gin.Context, email string, checkout bool) error {
cache := utils.GetCacheFromContext(c)
code := generateCode(c, cache, email)
if checkout {
if err := channel.SystemInstance.IsValidMail(email); err != nil {
return err
}
}
return channel.SystemInstance.SendVerifyMail(email, code)
}
func SignUp(c *gin.Context, form RegisterForm) (string, error) {
db := utils.GetDBFromContext(c)
cache := utils.GetCacheFromContext(c)
username := strings.TrimSpace(form.Username)
password := strings.TrimSpace(form.Password)
email := strings.TrimSpace(form.Email)
code := strings.TrimSpace(form.Code)
enableVerify := channel.SystemInstance.IsMailValid()
if !utils.All(
validateUsername(username),
validatePassword(password),
validateEmail(email),
!enableVerify || validateCode(code),
) {
return "", errors.New("invalid username/password/email format")
}
if err := channel.SystemInstance.IsValidMail(form.Email); err != nil {
return "", err
}
if IsUserExist(db, username) {
return "", fmt.Errorf("username is already taken, please try another one username (your current username: %s)", username)
}
if IsEmailExist(db, email) {
return "", fmt.Errorf("email is already taken, please try another one email (your current email: %s)", email)
}
if enableVerify && !checkCode(c, cache, email, code) {
return "", errors.New("invalid email verification code")
}
hash := utils.Sha2Encrypt(password)
user := &User{
Username: username,
Password: hash,
Email: email,
BindID: getMaxBindId(db) + 1,
Token: utils.Sha2Encrypt(email + username),
}
if _, err := globals.ExecDb(db, `
INSERT INTO auth (username, password, email, bind_id, token)
VALUES (?, ?, ?, ?, ?)
`, user.Username, user.Password, user.Email, user.BindID, user.Token); err != nil {
return "", err
}
user.CreateInitialQuota(db)
return user.GenerateToken()
}
func Login(c *gin.Context, form LoginForm) (string, error) {
db := utils.GetDBFromContext(c)
username := strings.TrimSpace(form.Username)
password := strings.TrimSpace(form.Password)
if !utils.All(
validateUsernameOrEmail(username),
validatePassword(password),
) {
return "", errors.New("invalid username or password format")
}
hash := utils.Sha2Encrypt(password)
// get user from db by username (or email) and password
var user User
if err := globals.QueryRowDb(db, `
SELECT auth.id, auth.username, auth.password FROM auth
WHERE (auth.username = ? OR auth.email = ?) AND auth.password = ?
`, username, username, hash).Scan(&user.ID, &user.Username, &user.Password); err != nil {
return "", errors.New("invalid username or password")
}
if user.IsBanned(db) {
return "", errors.New("current user is banned")
}
return user.GenerateToken()
}
func DeepLogin(c *gin.Context, token string) (string, error) {
if !useDeeptrain() {
return "", errors.New("deeptrain mode is disabled")
}
user := Validate(token)
if user == nil {
return "", errors.New("cannot validate access token")
}
db := utils.GetDBFromContext(c)
if !IsUserExist(db, user.Username) {
if globals.CloseRegistration {
return "", errors.New("this site is not open for registration")
}
// register
password := utils.GenerateChar(64)
_, err := globals.QueryDb(db, "INSERT INTO auth (bind_id, username, token, password) VALUES (?, ?, ?, ?)",
user.ID, user.Username, utils.Extract(token, 255, ""), utils.Sha2Encrypt(password))
if err != nil {
return "", err
}
u := &User{
Username: user.Username,
Password: password,
}
u.CreateInitialQuota(db)
return u.GenerateToken()
}
// login
_ = globals.QueryRowDb(db, "UPDATE auth SET token = ? WHERE username = ?", utils.Extract(token, 255, ""), user.Username)
var password string
err := globals.QueryRowDb(db, "SELECT password FROM auth WHERE username = ?", user.Username).Scan(&password)
if err != nil {
return "", err
}
u := &User{
Username: user.Username,
Password: password,
}
if u.IsBanned(db) {
return "", errors.New("current user is banned")
}
return u.GenerateToken()
}
func Reset(c *gin.Context, form ResetForm) error {
db := utils.GetDBFromContext(c)
cache := utils.GetCacheFromContext(c)
email := strings.TrimSpace(form.Email)
code := strings.TrimSpace(form.Code)
password := strings.TrimSpace(form.Password)
if !utils.All(
validateEmail(email),
validateCode(code),
validatePassword(password),
) {
return errors.New("invalid email/code/password format")
}
if !IsEmailExist(db, email) {
return errors.New("email is not registered")
}
if !checkCode(c, cache, email, code) {
return errors.New("invalid email verification code")
}
user := GetUserByEmail(db, email)
if user == nil {
return errors.New("cannot find user by email")
}
if err := user.UpdatePassword(db, cache, password); err != nil {
return err
}
cache.Del(c, fmt.Sprintf("nio:otp:%s", email))
return nil
}
func (u *User) UpdatePassword(db *sql.DB, cache *redis.Client, password string) error {
hash := utils.Sha2Encrypt(password)
if _, err := globals.ExecDb(db, `
UPDATE auth SET password = ? WHERE id = ?
`, hash, u.ID); err != nil {
return err
}
cache.Del(context.Background(), fmt.Sprintf("nio:user:%s", u.Username))
return nil
}
func (u *User) Validate(c *gin.Context) bool {
if u.Username == "" || u.Password == "" {
return false
}
cache := utils.GetCacheFromContext(c)
if password, err := cache.Get(c, fmt.Sprintf("nio:user:%s", u.Username)).Result(); err == nil && len(password) > 0 {
return u.Password == password
}
db := utils.GetDBFromContext(c)
var count int
if err := globals.QueryRowDb(db, "SELECT COUNT(*) FROM auth WHERE username = ? AND password = ?", u.Username, u.Password).Scan(&count); err != nil || count == 0 {
if err != nil {
globals.Warn(fmt.Sprintf("validate user error: %s", err.Error()))
}
return false
}
if u.IsBanned(db) {
return false
}
cache.Set(c, fmt.Sprintf("nio:user:%s", u.Username), u.Password, 30*time.Minute)
return true
}
func (u *User) GenerateToken() (string, error) {
instance := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"username": u.Username,
"password": u.Password,
"exp": time.Now().Add(time.Hour * 24 * 30).Unix(),
})
token, err := instance.SignedString([]byte(viper.GetString("secret")))
if err != nil {
return "", err
} else if token == "" {
return "", errors.New("unable to generate token")
}
return token, nil
}
func (u *User) GenerateTokenSafe(db *sql.DB) (string, error) {
if len(u.Username) == 0 {
if err := globals.QueryRowDb(db, "SELECT username FROM auth WHERE id = ?", u.ID).Scan(&u.Username); err != nil {
return "", err
}
}
if len(u.Password) == 0 {
if err := globals.QueryRowDb(db, "SELECT password FROM auth WHERE id = ?", u.ID).Scan(&u.Password); err != nil {
return "", err
}
}
return u.GenerateToken()
}