-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
307 lines (255 loc) · 6.42 KB
/
api.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
package main
import (
"crypto/rand"
"database/sql"
"encoding/base64"
"fmt"
"time"
"golang.org/x/crypto/bcrypt"
)
const (
permUser int = iota
permAdmin
permSystem
)
func newToken(id int) (string, error) {
var length int = 50
token := make([]byte, length)
_, err := rand.Read(token)
if err != nil {
return "", err
}
tokenString := "DO_NOT_SHARE_THIS" + "." + base64.URLEncoding.EncodeToString([]byte(fmt.Sprint(id))) + "." + base64.URLEncoding.EncodeToString(token)
return tokenString, nil
}
func createUser(username, password string, cash int, admin bool, db *sql.DB) (id int, token string, err error) {
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return 0, "", err
}
_, err = db.Exec("INSERT INTO users (username, password, joined) VALUES(?, ?, ?)", username, hashedBytes, time.Now().Unix())
if err != nil {
return 0, "", err
}
row := db.QueryRow("SELECT id FROM users WHERE username = ?", username)
err = row.Scan(&id)
if err != nil {
return 0, "", err
}
userToken, err := newToken(id)
if err != nil {
return 0, "", err
}
_, err = db.Exec("INSERT OR REPLACE INTO tokens (id, token) VALUES(?, ?)", id, userToken)
if err != nil {
return 0, "", err
}
_, err = db.Exec("INSERT OR REPLACE INTO userdata (id, cash, admin) VALUES (?, ?, ?)", id, cash, admin)
if err != nil {
return 0, "", err
}
return id, userToken, nil
}
func addUserToken(id int, token string) error {
_, err := db.Exec("INSERT OR REPLACE INTO tokens (id, token) VALUES(?, ?)", id, token)
return err
}
// type recaptchaResponse struct {
// Success bool `json:"success"`
// ErrorCodes []string `json:"error-codes"`
// }
// func verifyRecaptcha(response, secret string) bool {
// data := url.Values{}
// data.Set("secret", secret)
// data.Set("response", response)
// resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", data)
// if err != nil {
// logError(err)
// return false
// }
// defer resp.Body.Close()
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// logError(err)
// return false
// }
// var recaptchaResp recaptchaResponse
// err = json.Unmarshal(body, &recaptchaResp)
// if err != nil {
// logError(err)
// return false
// }
// return recaptchaResp.Success
// }
type apiUserObject struct {
Id int `json:"id"`
Username string `json:"username"`
Cash int `json:"cash"`
Password string `json:"password"`
}
type apiErrorObject struct {
Message string `json:"message"`
}
func userFromToken(token string, permission int) (apiUserObject, error) {
var object apiUserObject
id, err := idFromToken(token)
if err != nil {
return apiUserObject{}, err
}
object.Id = id
username, err := usernameFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Username = username
cash, err := cashFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Cash = cash
if permission >= permSystem {
password, err := passwordFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Password = password
}
return object, nil
}
func userFromId(id, permission int) (apiUserObject, error) {
var object apiUserObject
object.Id = id
username, err := usernameFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Username = username
cash, err := cashFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Cash = cash
if permission >= permSystem {
password, err := passwordFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Password = password
}
return object, nil
}
func userFromUsername(username string, permission int) (apiUserObject, error) {
var object apiUserObject
id, err := idFromUsername(username)
if err != nil {
return apiUserObject{}, err
}
object.Id = id
cash, err := cashFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Cash = cash
if permission >= permSystem {
password, err := passwordFromId(id)
if err != nil {
return apiUserObject{}, err
}
object.Password = password
}
return object, nil
}
func idFromToken(token string) (int, error) {
row := db.QueryRow("SELECT id FROM tokens WHERE token = ?", token)
var id int
err := row.Scan(&id)
return id, err
}
func usernameFromId(id int) (string, error) {
row := db.QueryRow("SELECT username FROM users WHERE id = ?", id)
var username string
err := row.Scan(&username)
return username, err
}
func cashFromId(id int) (int, error) {
row := db.QueryRow("SELECT cash FROM userdata WHERE id = ?", id)
var cash int
err := row.Scan(&cash)
return cash, err
}
func idFromUsername(username string) (int, error) {
row := db.QueryRow("SELECT id FROM users WHERE username = ?", username)
var id int
err := row.Scan(&id)
return id, err
}
func passwordFromId(id int) (string, error) {
row := db.QueryRow("SELECT password FROM users WHERE id = ?", id)
var password string
err := row.Scan(&password)
return password, err
}
func updateFromId(id int, user apiUserObject) error {
og, err := userFromId(id, permSystem)
if err != nil {
return err
}
if user.Username != og.Username {
_, err := db.Exec("UPDATE users SET username = ? WHERE id = ?", user.Username, id)
if err != nil {
return err
}
}
if user.Cash != og.Cash {
_, err := db.Exec("UPDATE userdata SET cash = ? WHERE id = ?", user.Cash, id)
if err != nil {
return err
}
}
if user.Password != "" {
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
_, err = db.Exec("UPDATE users SET password = ? WHERE id = ?", hashedBytes, id)
if err != nil {
return err
}
return nil
}
return nil
}
func updateFromToken(token string, user apiUserObject) error {
og, err := userFromToken(token, permSystem)
if err != nil {
return err
}
id, err := idFromToken(token)
if err != nil {
return err
}
if user.Username != og.Username {
_, err := db.Exec("UPDATE users SET username = ? WHERE id = ?", user.Username, id)
if err != nil {
return err
}
}
if user.Cash != og.Cash {
_, err := db.Exec("UPDATE userdata SET cash = ? WHERE id = ?", user.Cash, id)
if err != nil {
return err
}
}
if user.Password != "" {
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
_, err = db.Exec("UPDATE users SET password = ? WHERE id = ?", hashedBytes)
if err != nil {
return err
}
return nil
}
return nil
}