Skip to content

Commit d0c1948

Browse files
committed
Backend: protect the /ws endpoint from unauthorized access.
1 parent 079efd4 commit d0c1948

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

backend/server/ws.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"sync"
1010

1111
"github.com/coder/websocket"
12+
13+
"flowey/db"
1214
)
1315

1416
type connection struct {
@@ -69,9 +71,26 @@ func (handler *wsHandler) handle(writer http.ResponseWriter, request *http.Reque
6971
}
7072

7173
func (handler *wsHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
74+
sessionKey, err := request.Cookie(sessionKeyCookieName)
75+
if err != nil {
76+
writer.WriteHeader(http.StatusUnauthorized)
77+
return
78+
}
79+
80+
err = db.Authorize(sessionKey.Value)
81+
if err != nil {
82+
switch err {
83+
case db.Unathorized:
84+
writer.WriteHeader(http.StatusUnauthorized)
85+
case db.InternalServerError:
86+
writer.WriteHeader(http.StatusInternalServerError)
87+
}
88+
return
89+
}
90+
7291
handler.waitGroup.Add(1)
7392

74-
err := handler.handle(writer, request)
93+
err = handler.handle(writer, request)
7594
if err != nil {
7695
log.Println(err)
7796
return

0 commit comments

Comments
 (0)