This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
111 lines (98 loc) 路 2.8 KB
/
server.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
package server
import (
"encoding/json"
"html/template"
"net/http"
"net/url"
"github.com/kamilsk/passport/errors"
"github.com/kamilsk/passport/static"
"github.com/kamilsk/passport/transfer/api/v1/tracker"
)
const (
// MarkerKey is used to find and store required cookie value.
MarkerKey = "marker"
)
// New returns a new instance of Passport server.
func New(baseURL string, service Service) *Server {
u, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
return &Server{baseURL: u, service: service, template: template.Must(template.New("script").Parse(passport()))}
}
// Server handles HTTP requests.
type Server struct {
baseURL *url.URL
service Service
template *template.Template
}
// GetTrackerInstructionV1 is responsible for `GET /api/v1/tracker/instruction` request handling.
func (s *Server) GetTrackerInstructionV1(rw http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie(MarkerKey)
if err != nil {
cookie = &http.Cookie{Name: MarkerKey}
}
response := s.service.HandleTrackerInstructionV1(tracker.InstructionRequest{EncryptedMarker: cookie.Value})
if response.Error != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
cookie.MaxAge, cookie.Path, cookie.Value = 0, "/", response.EncryptedMarker
http.SetCookie(rw, cookie)
rw.Header().Set("Content-Type", "application/javascript")
rw.WriteHeader(http.StatusOK)
s.template.Execute(rw, struct {
BaseURL *url.URL
Endpoint string
Limit uint8
Threshold uint8
Correct int
Watch int
Debug bool
}{
BaseURL: s.baseURL,
Endpoint: "/api/v1/tracker/fingerprint",
Limit: 5,
Threshold: 3,
Correct: 100, // Milliseconds
Watch: 1000, // Milliseconds
Debug: true,
})
}
// PostTrackerFingerprintV1 is responsible for `POST /api/v1/tracker/fingerprint` request handling.
func (s *Server) PostTrackerFingerprintV1(rw http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie(MarkerKey)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
defer req.Body.Close()
request := tracker.FingerprintRequest{EncryptedMarker: cookie.Value, Header: req.Header}
if err := json.NewDecoder(req.Body).Decode(&request.Payload); err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
response := s.service.HandleTrackerFingerprintV1(request)
if response.Error != nil {
if err, is := response.Error.(errors.ApplicationError); is {
if _, is := err.IsClientError(); is {
rw.WriteHeader(http.StatusBadRequest)
return
}
}
rw.WriteHeader(http.StatusInternalServerError)
return
}
if response.Fingerprint.UpdatedAt.Valid {
rw.WriteHeader(http.StatusOK)
return
}
rw.WriteHeader(http.StatusCreated)
}
func passport() string {
b, err := static.Asset("static/scripts/passport.min.js")
if err != nil {
panic(err)
}
return string(b)
}