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
|
package db
import (
"errors"
"fmt"
"log"
)
type Account struct {
UserName string `validate:"required,alphanum,max=64"`
Password string `json:",omitempty" validate:"required,min=10,max=128"`
Info *Login
}
type Login struct {
id uint
Level uint `validate:"gte=0,lte=100"`
FirstName, LastName string `validate:"required,alphanumunicode"`
Bearer *Bearer
}
func (ac *Account) CreateAccount(safe *SafeDB) error {
const sqlStatement string = `
INSERT INTO Accounts (
id,
UserName,
Password,
Level,
FirstName,
LastName
)
VALUES (NULL, ?, ?, ?, ?, ?);
`
err := safe.validate.Struct(ac)
if err != nil {
return err
}
safe.mu.Lock()
defer safe.mu.Unlock()
_, err = safe.db.Exec(
sqlStatement,
ac.UserName,
ToBlake3(ac.Password),
ac.Info.FirstName,
ac.Info.LastName,
ac.Info.Level,
)
return err
}
func (ac *Account) Login(safe *SafeDB) error {
const sqlStatementQuery string = `
SELECT id, Password, Level, FirstName, LastName
FROM Accounts
WHERE Accounts.UserName = ?
`
err := safe.validate.Struct(ac)
fmt.Println(ac.Password, ac.UserName)
if err != nil {
log.Println(err)
return err
}
ac.Info = &Login{}
ac.Info.Bearer = &Bearer{}
safe.mu.Lock()
row := safe.db.QueryRow(sqlStatementQuery, ac.UserName)
safe.mu.Unlock()
var Password string
err = row.Scan(
&ac.Info.id,
&Password,
&ac.Info.FirstName,
&ac.Info.LastName,
&ac.Info.Level,
)
if err != nil {
return err
}
if Password != ToBlake3(ac.Password) {
return errors.New("Auth failed")
}
ac.Password = ""
err = ac.Info.Bearer.Generate(safe, ac.Info)
if err != nil {
return err
}
return err
}
func (ac *Account) fromBearer(safe *SafeDB, b *Bearer) error {
const sqlStatementAccount string = `
SELECT UserName, Password, Level, FirstName, LastName
FROM Accounts
WHERE Accounts.id = ?
`
safe.mu.Lock()
row := safe.db.QueryRow(sqlStatementAccount, b.accountId)
safe.mu.Unlock()
ac.Info = &Login{}
ac.Info.id = b.accountId
ac.Info.Bearer = b
err := row.Scan(
&ac.UserName,
&ac.Password,
&ac.Info.FirstName,
&ac.Info.LastName,
&ac.Info.Level,
)
if err != nil {
return err
}
ac.Info.Bearer = b
return err
}
|