irc working
This commit is contained in:
+17
-21
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -112,30 +111,19 @@ func sendToAllExceptAndCloseIfFails(conn *websocket.Conn, message map[string]any
|
||||
}
|
||||
}
|
||||
|
||||
func handleUnauthenticatedMessage(ctx context.Context, conn *websocket.Conn, msg map[string]any) {
|
||||
func handleUnauthenticatedMessage(conn *websocket.Conn, msg map[string]any) {
|
||||
token := msg["token"].(string)
|
||||
subject, err := GetSubject(token)
|
||||
user, err := GetUserFromToken(token)
|
||||
if err != nil {
|
||||
log.Println("invalid or expired token:", err)
|
||||
conn.Close(websocket.StatusPolicyViolation, "invalid token")
|
||||
return
|
||||
}
|
||||
|
||||
var subjectId uint32
|
||||
parsed, err := strconv.ParseUint(subject, 10, 32)
|
||||
subjectId = uint32(parsed)
|
||||
if err != nil {
|
||||
conn.Close(websocket.StatusPolicyViolation, "invalid token")
|
||||
return
|
||||
}
|
||||
|
||||
user, err := GetUserDataById(ctx, subjectId)
|
||||
if err != nil {
|
||||
conn.Close(websocket.StatusPolicyViolation, "invalid token")
|
||||
err := conn.Close(websocket.StatusPolicyViolation, "invalid token")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
authenticatedConnections = append(authenticatedConnections, AuthConnection{connection: conn, user: *user})
|
||||
authenticatedConnections = append(authenticatedConnections, AuthConnection{connection: conn, user: user})
|
||||
mu.Unlock()
|
||||
sendAndCloseIfFails(conn, map[string]any{
|
||||
"authAs": user.Name,
|
||||
@@ -151,8 +139,16 @@ func handleAuthenticatedMessage(conn *websocket.Conn, msg map[string]any) {
|
||||
return
|
||||
}
|
||||
|
||||
auth := getConnectionDataIfAuth(conn)
|
||||
if auth == nil {
|
||||
sendAndCloseIfFails(conn, map[string]any{
|
||||
"error": "no auth",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
sendToAllExceptAndCloseIfFails(conn, map[string]any{
|
||||
"username": ,
|
||||
"message": message,
|
||||
"username": auth.user.Name,
|
||||
"message": message,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user