-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
82 lines (66 loc) · 1.92 KB
/
client.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
package main
import (
"encoding/base64"
"encoding/json"
"net/http"
petname "github.com/dustinkirkland/golang-petname"
"github.com/google/uuid"
)
type Client struct {
Id string `json:"id,string"`
Name string `json:"name,string"`
}
const (
cookieName string = "fuse_chat_cookie"
)
// parseClientCookie parses the client cookie from the given HTTP request.
// It decodes the cookie value, unmarshals it into a Client struct,
// and validates the client ID using UUID parsing.
// If successful, it returns the parsed Client object.
// Otherwise, it returns an error.
func parseClientCookie(r *http.Request) (*Client, error) {
cookie, err := r.Cookie(cookieName)
if err != nil {
return nil, err
}
data, err := base64.StdEncoding.DecodeString(cookie.Value)
if err != nil {
return nil, err
}
client := &Client{}
err = json.Unmarshal(data, client)
if err != nil {
return nil, err
}
_, err = uuid.Parse(client.Id)
if err != nil {
return nil, err
}
return client, nil
}
// newClient creates a new client for the chat application.
// It takes in the http.ResponseWriter, http.Request, and a Chat instance.
// If the client cookie exists in the request, it parses the client information from the cookie.
// If the client cookie does not exist, it generates a new client with a unique ID and a random name.
// The client information is then stored in a cookie and set in the response.
// The function returns the created client.
func newClient(w http.ResponseWriter, r *http.Request, c *Chat) *Client {
client, err := parseClientCookie(r)
if err != nil {
client = &Client{
Id: uuid.New().String(),
Name: petname.Generate(2, "-"),
}
data, _ := json.Marshal(client)
http.SetCookie(w, &http.Cookie{
Name: cookieName,
Value: base64.StdEncoding.EncodeToString(data),
Path: "/",
MaxAge: 3600,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
}
return client
}