/
main.go
48 lines (42 loc) · 924 Bytes
/
main.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"io"
"net/http"
)
func getCode(data string) string {
h := hmac.New(sha256.New, []byte("ourkey"))
io.WriteString(h, data)
return fmt.Sprintf("%x", h.Sum(nil))
}
func main() {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie("session-id")
// cookie is not set
if err != nil {
//id, _ := uuid.NewV4()
cookie = &http.Cookie{
Name: "session-id",
}
}
if req.FormValue("email") != "" {
cookie.Value = req.FormValue("email")
}
fmt.Println(getCode(cookie.Value))
http.SetCookie(res, cookie)
io.WriteString(res, `<!DOCTYPE html>
<html>
<body>
<form method="POST">
`+cookie.Value+`
<input type="email" name="email">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>`)
})
http.ListenAndServe(":9000", nil)
}