1
1
package main
2
2
3
3
import (
4
+ verify "collab/verify"
4
5
"encoding/json"
6
+ "io"
5
7
"log"
6
8
"net/http"
7
9
"os"
@@ -37,33 +39,57 @@ type Message struct {
37
39
content []byte
38
40
}
39
41
40
- func verifyToken (token string ) bool {
42
+ func verifyToken (token string ) ( bool , string ) {
41
43
client := & http.Client {}
42
44
USER_SERVICE_URI := os .Getenv ("USER_SERVICE_URI" )
43
45
if USER_SERVICE_URI == "" {
44
46
USER_SERVICE_URI = "http://localhost:3001"
45
47
}
46
- req , err := http .NewRequest ("GET" , USER_SERVICE_URI + "/auth/verify-token" , nil )
48
+ req , err := http .NewRequest ("GET" , USER_SERVICE_URI + "/auth/verify-token" , nil )
47
49
if err != nil {
48
50
log .Println ("Error creating request:" , err )
49
- return false
51
+ return false , ""
50
52
}
51
53
52
- req .Header .Set ("Authorization" , "Bearer " + token )
54
+ req .Header .Set ("Authorization" , "Bearer " + token )
53
55
54
56
resp , err := client .Do (req )
55
57
if err != nil {
56
58
log .Println ("Error making request:" , err )
57
- return false
59
+ return false , ""
58
60
}
59
61
defer resp .Body .Close ()
60
62
63
+ var response struct {
64
+ Message string `json:"message"`
65
+ Data struct {
66
+ ID string `json:"id"`
67
+ Username string `json:"username"`
68
+ Email string `json:"email"`
69
+ IsAdmin bool `json:"isAdmin"`
70
+ } `json:"data"`
71
+ }
72
+
73
+ body , err := io .ReadAll (resp .Body )
74
+ if err != nil {
75
+ log .Println ("Error reading response body:" , err )
76
+ return false , ""
77
+ }
78
+
79
+ // Unmarshal the response body into the struct
80
+ if err := json .Unmarshal (body , & response ); err != nil {
81
+ log .Println ("Error unmarshaling response:" , err )
82
+ return false , ""
83
+ }
84
+
85
+ // Check if the token was verified successfully
61
86
if resp .StatusCode != http .StatusOK {
62
87
log .Println ("Token verification failed with status:" , resp .Status )
63
- return false
88
+ return false , ""
64
89
}
65
90
66
- return true ;
91
+ // Return true and the ID from the response
92
+ return true , response .Data .ID
67
93
}
68
94
69
95
// NewHub creates a new hub instance
@@ -114,7 +140,7 @@ func (h *Hub) Run() {
114
140
}
115
141
116
142
// ServeWs handles WebSocket requests
117
- func serveWs (hub * Hub , c * gin.Context ) {
143
+ func serveWs (hub * Hub , c * gin.Context , roomMappings * verify. RoomMappings ) {
118
144
roomID := c .Query ("roomID" )
119
145
if roomID == "" {
120
146
http .Error (c .Writer , "roomID required" , http .StatusBadRequest )
@@ -130,10 +156,19 @@ func serveWs(hub *Hub, c *gin.Context) {
130
156
client := & Client {conn : conn , roomID : roomID }
131
157
hub .register <- client
132
158
133
- go handleMessages (client , hub )
159
+ go handleMessages (client , hub , roomMappings )
160
+ }
161
+
162
+ func authenticateClient (token string , client * Client , roomMappings * verify.RoomMappings ) bool {
163
+ ok , userID := verifyToken (token )
164
+ if ! ok {
165
+ log .Println ("bruh" )
166
+ return false
167
+ }
168
+ return verify .VerifyRoom (roomMappings , client .roomID , userID )
134
169
}
135
170
136
- func handleMessages (client * Client , hub * Hub ) {
171
+ func handleMessages (client * Client , hub * Hub , roomMappings * verify. RoomMappings ) {
137
172
defer func () {
138
173
hub .unregister <- client
139
174
}()
@@ -150,25 +185,18 @@ func handleMessages(client *Client, hub *Hub) {
150
185
log .Printf ("Failed to parse message: %v" , err )
151
186
continue
152
187
}
153
-
154
188
// Handle authentication message
155
189
if msgData ["type" ] == "auth" {
156
- token , ok := msgData ["token" ].(string )
157
- if ! ok {
158
- log .Printf ("Auth message missing token" )
159
- continue
160
- }
161
- if verifyToken (token ) { // Implement this function to verify the token
162
- client .authenticated = true
163
- log .Println ("Client authenticated successfully" )
164
- } else {
165
- log .Println ("Invalid auth token" )
166
- client .conn .WriteMessage (websocket .TextMessage , []byte ("Authentication failed" ))
167
- client .conn .Close ()
168
- break
169
- }
170
- continue
171
- }
190
+ token , ok := msgData ["token" ].(string )
191
+ if ! ok || ! authenticateClient (token , client , roomMappings ) {
192
+ log .Println ("Authentication failed" )
193
+ client .conn .WriteMessage (websocket .TextMessage , []byte ("Authentication failed" ))
194
+ client .conn .Close ()
195
+ break
196
+ }
197
+ client .authenticated = true
198
+ log .Println ("Client authenticated successfully" )
199
+ }
172
200
173
201
if msgData ["type" ] == "close_session" {
174
202
closeMessage := Message {
@@ -218,9 +246,15 @@ func main() {
218
246
hub := NewHub ()
219
247
go hub .Run ()
220
248
249
+ REDIS_URI := os .Getenv ("REDIS_URI" )
250
+ if REDIS_URI == "" {
251
+ REDIS_URI = "localhost:9190"
252
+ }
253
+ roomMappings := verify .InitialiseRoomMappings (REDIS_URI , 1 )
254
+
221
255
// WebSocket connection endpoint
222
256
r .GET ("/ws" , func (c * gin.Context ) {
223
- serveWs (hub , c )
257
+ serveWs (hub , c , roomMappings )
224
258
})
225
259
226
260
// Status endpoint
0 commit comments