-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
176 lines (150 loc) · 4 KB
/
users.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
package rabbit
import (
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
const (
// SigUserLogin: user *User, c *gin.Context
SigUserLogin = "user.login"
// SigUserLogout: user *User, c *gin.Context
SigUserLogout = "user.logout"
//SigUserCreate: user *User, c *gin.Context
SigUserCreate = "user.create"
)
// set session
func Login(c *gin.Context, user *User) {
db := c.MustGet(DbField).(*gorm.DB)
SetLastLogin(db, user, c.ClientIP())
session := sessions.Default(c)
session.Set(UserField, user.ID)
session.Save()
Sig().Emit(SigUserLogin, user, c)
}
// 1. remove context
// 2. remove session
func Logout(c *gin.Context, user *User) {
// 1
c.Set(UserField, nil)
// 2
session := sessions.Default(c)
session.Delete(UserField)
session.Save()
Sig().Emit(SigUserLogout, user, c)
}
// password
func CheckPassword(dbPassword, password string) bool {
return dbPassword == HashPassword(password)
}
func SetPassword(db *gorm.DB, user *User, password string) (err error) {
p := HashPassword(password)
if err = UpdateFields(db, user, map[string]any{
"Password": p,
}); err != nil {
return err
}
user.Password = p
return err
}
func HashPassword(password string) string {
salt := GetEnv(ENV_PASSWORD_SALT)
hashVal := sha256.Sum256([]byte(salt + password))
return fmt.Sprintf("sha256$%s%x", salt, hashVal)
}
// user
func GetUserByID(db *gorm.DB, userID uint) (*User, error) {
var val User
result := db.Where("id", userID).Where("Enabled", true).Preload("Roles").Take(&val)
if result.Error != nil {
return nil, result.Error
}
return &val, nil
}
func GetUserByEmail(db *gorm.DB, email string) (user *User, err error) {
var val User
result := db.Where("email", strings.ToLower(email)).Take(&val)
if result.Error != nil {
return nil, result.Error
}
return &val, nil
}
func IsExistByEmail(db *gorm.DB, email string) bool {
_, err := GetUserByEmail(db, email)
return err == nil
}
func CreateUser(db *gorm.DB, email, password string) (*User, error) {
user := User{
Email: email,
Password: HashPassword(password),
Enabled: true,
Activated: false,
}
result := db.Create(&user)
return &user, result.Error
}
func SetLastLogin(db *gorm.DB, user *User, lastIp string) error {
now := time.Now().Truncate(1 * time.Second)
vals := map[string]any{
"LastLoginIP": lastIp,
"LastLogin": &now,
}
user.LastLogin = &now
user.LastLoginIP = lastIp
return db.Model(user).Updates(vals).Error
}
/*
timestamp-uid-token
base64(email$timestamp) + "-" + sha256(salt + logintimestamp + password + email$timestamp)
*/
func EncodeHashToken(user *User, timestamp int64, useLastLogin bool) (hash string) {
logintimestamp := "0"
if useLastLogin && user.LastLogin != nil {
logintimestamp = fmt.Sprintf("%d", user.LastLogin.Unix())
}
t := fmt.Sprintf("%s$%d", user.Email, timestamp)
salt := GetEnv(ENV_PASSWORD_SALT)
hashVal := sha256.Sum256([]byte(salt + logintimestamp + user.Password + t))
hash = base64.RawStdEncoding.EncodeToString([]byte(t)) + "-" + fmt.Sprintf("%x", hashVal)
return hash
}
/*
base64(email$timestamp) + "-" + sha256(salt + logintimestamp + password + email$timestamp)
*/
func DecodeHashToken(db *gorm.DB, hash string, useLastLogin bool) (user *User, err error) {
vals := strings.Split(hash, "-")
if len(vals) != 2 {
return nil, errors.New("bad token")
}
data, err := base64.RawStdEncoding.DecodeString(vals[0])
if err != nil {
return nil, errors.New("bad token")
}
vals = strings.Split(string(data), "$")
if len(vals) != 2 {
return nil, errors.New("bad token")
}
ts, err := strconv.ParseInt(vals[1], 10, 64)
if err != nil {
return nil, errors.New("bad token")
}
// check expired
if time.Now().Unix() > ts {
return nil, errors.New("token expired")
}
user, err = GetUserByEmail(db, vals[0])
if err != nil {
return nil, errors.New("bad token")
}
token := EncodeHashToken(user, ts, useLastLogin)
if token != hash {
return nil, errors.New("bad token")
}
return user, nil
}