-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.go
More file actions
214 lines (167 loc) · 5.23 KB
/
form.go
File metadata and controls
214 lines (167 loc) · 5.23 KB
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
package webx
import (
"sync"
"time"
"github.com/gofiber/fiber/v3"
"github.com/tkdeng/gocrypt"
"github.com/tkdeng/goutil"
"github.com/tkdeng/regex"
)
type FormHandler struct {
app App
sessionHash string
formSession *goutil.CacheMap[string, map[string]string]
}
type FormCtx struct {
fiber.Ctx
ctx fiber.Ctx
app App
Body map[string]any
Session *map[string]string
formSession *goutil.CacheMap[string, map[string]string]
session string
token string
}
// NewForm creates a new form handler.
//
// This method allows easily managing session verification for user forms.
//
// Note: this method assumes forms will be submitted with the client fetch api.
func (app App) NewForm(uri string, cb func(c FormCtx) error) *FormHandler {
sessionHash := string(goutil.URandBytes(256))
if hash, err := gocrypt.GenerateSalt(256); err == nil {
sessionHash = string(hash)
}
formSession := goutil.NewCache[string, map[string]string](10 * time.Minute)
var smu sync.RWMutex
sessionList := [][]byte{}
go func() {
time.Sleep(30 * time.Minute)
smu.Lock()
for i := len(sessionList) - 1; i >= 0; i-- {
if !formSession.Has(string(sessionList[i])) {
sessionList = append(sessionList[:i], sessionList[i+1:]...)
}
}
smu.Unlock()
}()
app.Use(uri, app.BlockBotHeader, func(c fiber.Ctx) error {
dID, err := deviceID(c, sessionHash)
if err != nil {
return app.Error(c, 400, "Bad Request!")
}
smu.RLock()
session := string(goutil.URandBytes(256, &sessionList))
smu.RUnlock()
sessionData := map[string]string{
"ip": c.IP(),
"deviceID": dID,
}
//todo: get browser fingerprint
// also add alternate methods for verifying user session
body, err := goutil.JSON.Parse(goutil.Clean(c.Body()))
if err != nil {
body = map[string]any{}
}
return cb(FormCtx{
ctx: c,
app: app,
Body: body,
Session: &sessionData,
formSession: formSession,
session: session,
token: string(goutil.RandBytes(64)),
})
})
//todo: add client side wasm and js for form handler
return &FormHandler{
app: app,
sessionHash: sessionHash,
formSession: formSession,
}
}
// API verifies and continues a verified form session, and updates the token every request.
//
// Note: this method assumes forms will be submitted with the client fetch api.
// Your API should include the "session" and updated "token" on every request.
func (handler *FormHandler) API(uri string, cb func(c FormCtx) error) {
handler.app.Use(uri, func(c fiber.Ctx) error {
body, err := goutil.JSON.Parse(goutil.Clean(c.Body()))
if err != nil {
return jsonErr(c, 400, "Bad Request!")
}
session := goutil.ToType[string](body["session"])
if session == "" || body["token"] == "" {
return jsonErr(c, 400, "Bad Request!")
}
sessionData, err := handler.formSession.Get(session)
if err != nil {
return jsonErr(c, 400, "Invalid Session!")
}
dID, err := deviceID(c, handler.sessionHash)
if err != nil || sessionData["deviceID"] != dID || sessionData["ip"] != c.IP() || sessionData["token"] != body["token"] {
handler.formSession.Del(session)
return jsonErr(c, 400, "Invalid Session!")
}
delete(sessionData, "token")
//todo: get browser fingerprint
// also add alternate methods for verifying user session
return cb(FormCtx{
app: handler.app,
ctx: c,
Body: body,
Session: &sessionData,
formSession: handler.formSession,
session: session,
token: string(goutil.RandBytes(64)),
})
})
}
func (ctx FormCtx) Render(url string, vars ...Map) error {
if len(vars) == 0 {
vars = append(vars, Map{})
}
vars[0]["session"] = ctx.session
vars[0]["token"] = ctx.token
(*ctx.Session)["token"] = ctx.token
ctx.formSession.Set(ctx.session, *ctx.Session, nil)
return ctx.app.Render(ctx.ctx, url, vars[0])
}
func (ctx FormCtx) JSON(success bool, json ...map[string]any) error {
if len(json) == 0 {
json = append(json, map[string]any{})
}
json[0]["success"] = success
json[0]["token"] = ctx.token
(*ctx.Session)["token"] = ctx.token
ctx.formSession.Set(ctx.session, *ctx.Session, nil)
return ctx.ctx.JSON(json[0])
}
func jsonErr(c fiber.Ctx, status int, msg string) error {
c.Status(status)
return c.JSON(map[string]any{
"success": false,
"msg": msg,
})
}
func deviceID(c fiber.Ctx, sessionHash string) (string, error) {
id := []byte{}
if val, err := goutil.JSON.Stringify(c.IPs()); err == nil {
id = goutil.Clean(val)
} else {
id = []byte(goutil.Clean(c.IP()))
}
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("User-Agent")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Accept")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Accept-Encoding")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Accept-Language")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Connection")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Sec-Ch-Ua")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Sec-Ch-Ua-Mobile")), ']')...)
id = append(id, regex.JoinBytes('[', goutil.Clean(c.Get("Sec-Ch-Ua-Platform")), ']')...)
hash, err := gocrypt.FastHash(string(id), sessionHash)
if err != nil {
return "", err
}
return hash, nil
}