forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (46 loc) · 1.01 KB
/
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
49
50
51
52
53
package main
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"github.com/nu7hatch/gouuid"
"io"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
func home(res http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie("session-id")
// cookie is not set
if err != nil {
id, _ := uuid.NewV4()
code := getCode(id.String())
val := code + "|" + id.String()
cookie = &http.Cookie{
Name: "session-id",
Value: val,
}
}
values := strings.Split(cookie.Value, "|")
code := getCode(values[1])
cookieCode := values[0]
fmt.Fprintln(res, code)
fmt.Fprintln(res, cookieCode)
if code != cookieCode {
fmt.Fprintln(res, "Cookie monsters says: someone's had their hands in my cookies!")
cookie = &http.Cookie{
Name: "session-id",
Value: "0",
MaxAge: -1,
}
}
http.SetCookie(res, cookie)
}
func getCode(data string) string {
h := hmac.New(sha256.New, []byte("ourkey"))
io.WriteString(h, data)
return fmt.Sprintf("%x", h.Sum(nil))
}