-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
112 lines (91 loc) · 2.36 KB
/
account.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
package main
import (
"fmt"
"golang.org/x/crypto/bcrypt"
"log"
"time"
)
type Account struct {
username string
password string
maxChars uint8
lastIp string
lastDate time.Time
email string
characters map[string]*Player
}
func NewAccount() *Account {
return &Account{
characters: make(map[string]*Player),
}
}
func (a *Account) UserName() string {
return a.username
}
func (a *Account) SetUserName(newName string) {
a.username = newName
}
func (a *Account) PasswordHash() string {
return a.password
}
func (a *Account) SetPasswordHash(hashedPass string) {
a.password = hashedPass
}
func (a *Account) HashAndSetPassword(plainPass string) {
passHash, err := HashPassword(plainPass)
if err != nil {
log.Panic(err.Error())
}
a.password = passHash
}
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
func (a *Account) CheckPasswordHash(toCheck string) bool {
err := bcrypt.CompareHashAndPassword([]byte(a.password), []byte(toCheck))
return err == nil
}
func (a *Account) MaxChars() uint8 {
return a.maxChars
}
func (a *Account) SetMaxChars(newMax uint8) {
a.maxChars = newMax
}
func (a *Account) LastIP() string {
return a.lastIp
}
func (a *Account) SetLastIP(newIP string) {
a.lastIp = newIP
}
func (a *Account) LastDate() time.Time {
return a.lastDate
}
func (a *Account) SetLastDate(newDate time.Time) {
a.lastDate = newDate
}
func (a *Account) UpdateLastDate() {
a.lastDate = time.Now()
}
func (a *Account) Email() string {
return a.email
}
func (a *Account) SetEmail(newEmail string) {
a.email = newEmail
}
func (a *Account) SaveAccountToDBQuery() string {
lastDate := TimeString(a.LastDate())
return fmt.Sprintf("INSERT INTO Account VALUES ('%s', '%s', %d, '%s', '%s', '%s') AS new ON DUPLICATE KEY UPDATE "+
"username=new.username, password=new.password, max_chars=new.max_chars, last_ip=new.last_ip, last_date=new.last_date, email=new.email",
a.UserName(), a.PasswordHash(), a.MaxChars(), a.LastIP(), lastDate, a.Email())
}
func CreateAccountTableDBQuery() string {
return "CREATE TABLE IF NOT EXISTS Account(" +
"username VARCHAR(20) PRIMARY KEY," +
"password CHAR(60) NOT NULL," +
"max_chars TINYINT UNSIGNED NOT NULL DEFAULT 3," +
"last_ip VARCHAR(15)," +
"last_date TIMESTAMP," +
"email VARCHAR(319) NOT NULL" +
")"
}