forked from jaeles-project/jaeles
/
user.go
59 lines (51 loc) · 1.38 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
package database
import (
"crypto/sha1"
"fmt"
"github.com/hktalent/jaeles/utils"
"strconv"
"time"
"github.com/hktalent/jaeles/database/models"
)
// SelectUser get password of one user to compare
func SelectUser(username string) string {
var user models.User
DB.Where("username = ?", username).First(&user)
if user.Username == username {
return user.Password
}
return ""
}
// CreateUser used to create new user
func CreateUser(username string, password string) {
oldpass := SelectUser(username)
if oldpass != "" {
UpdateUser(username, password)
} else {
rawToken := fmt.Sprintf("%v-%v", username, strconv.FormatInt(time.Now().Unix(), 10))
rawSecret := fmt.Sprintf("%v-%v-%v", username, password, strconv.FormatInt(time.Now().Unix(), 10))
userObj := models.User{
Username: username,
Email: username,
Password: GenHash(password),
Secret: GenHash(rawSecret),
Token: GenHash(rawToken),
}
utils.GoodF("Create new credentials %v:%v", username, password)
DB.Create(&userObj)
}
}
// UpdateUser update default sign
func UpdateUser(username string, password string) {
var userObj models.User
DB.Where("username = ?", username).First(&userObj)
userObj.Password = GenHash(password)
DB.Save(&userObj)
}
// GenHash generate SHA1 hash
func GenHash(text string) string {
h := sha1.New()
h.Write([]byte(text))
hashed := h.Sum(nil)
return fmt.Sprintf("%x", hashed)
}