Verify permissions when joining lobby
This commit is contained in:
parent
008cda9abd
commit
469f633310
@ -3,20 +3,26 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/nrednav/cuid2"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/nrednav/cuid2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: This struct should have a creation time
|
// TODO: This struct should have a creation time
|
||||||
type Lobby struct {
|
type Lobby struct {
|
||||||
LobbyOwner string
|
LobbyOwner string
|
||||||
LobbyPlayers [3]string
|
LobbyPlayers []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type LobbyResult struct {
|
type LobbyResult struct {
|
||||||
LobbyId string
|
LobbyId string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LobbyCreateInput struct {
|
||||||
|
UserId string
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: We should remove entries from this map when they expire.
|
// TODO: We should remove entries from this map when they expire.
|
||||||
// TODO: Define how long lobbies last
|
// TODO: Define how long lobbies last
|
||||||
var lobbies = make(map[string]Lobby)
|
var lobbies = make(map[string]Lobby)
|
||||||
@ -30,9 +36,25 @@ func CreateLobby(writer http.ResponseWriter, request *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the UserId from the JSON payload
|
||||||
|
decoder := json.NewDecoder(request.Body)
|
||||||
|
var data LobbyCreateInput
|
||||||
|
err := decoder.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error in JSON decoding: %s\n", err)
|
||||||
|
writer.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(writer, "{\"error\": \"%s\"}", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
lobbyId := cuid2.Generate()
|
lobbyId := cuid2.Generate()
|
||||||
|
|
||||||
result := LobbyResult{LobbyId: lobbyId}
|
result := LobbyResult{LobbyId: lobbyId}
|
||||||
|
lobbies[lobbyId] = Lobby{
|
||||||
|
LobbyOwner: data.UserId,
|
||||||
|
LobbyPlayers: make([]string, 3),
|
||||||
|
}
|
||||||
|
log.Printf("Created lobby with id %s/%s", lobbyId, data.UserId)
|
||||||
|
|
||||||
jsonData, err := json.Marshal(result)
|
jsonData, err := json.Marshal(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -44,3 +66,23 @@ func CreateLobby(writer http.ResponseWriter, request *http.Request) {
|
|||||||
|
|
||||||
fmt.Fprintf(writer, "%s", jsonData)
|
fmt.Fprintf(writer, "%s", jsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verifies that the userId has access to the lobbyId
|
||||||
|
func VerifyLobbyAccess(userId, lobbyId string) bool {
|
||||||
|
lobby, ok := lobbies[lobbyId]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if lobby.LobbyOwner == userId {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, playerId := range lobby.LobbyPlayers {
|
||||||
|
if playerId == userId {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -2,8 +2,10 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
@ -15,8 +17,8 @@ var upgrader = websocket.Upgrader{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LobbyMsg struct {
|
type LobbyMsg struct {
|
||||||
Action string `json:action`
|
Action string `json:"action"`
|
||||||
Value string `json:value`
|
Value string `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LobbyWsConnect(writer http.ResponseWriter, request *http.Request) {
|
func LobbyWsConnect(writer http.ResponseWriter, request *http.Request) {
|
||||||
@ -45,7 +47,7 @@ func LobbyWsConnect(writer http.ResponseWriter, request *http.Request) {
|
|||||||
|
|
||||||
switch data.Action {
|
switch data.Action {
|
||||||
case "auth":
|
case "auth":
|
||||||
err = validateUserId(mt, conn, data.Value)
|
err = authenticateConnection(mt, conn, data.Value)
|
||||||
default:
|
default:
|
||||||
log.Print("no action :c")
|
log.Print("no action :c")
|
||||||
}
|
}
|
||||||
@ -57,18 +59,46 @@ func LobbyWsConnect(writer http.ResponseWriter, request *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateUserId(mt int, conn *websocket.Conn, userId string) error {
|
// Verifies that the user id & lobby id are valid, and that the user has permission to
|
||||||
_, ok := Users[userId]
|
// access the lobby
|
||||||
|
func authenticateConnection(mt int, conn *websocket.Conn, authInfo string) error {
|
||||||
|
// TODO: split userId by ','
|
||||||
|
|
||||||
var responseJson LobbyMsg
|
var err error
|
||||||
|
var result string
|
||||||
|
|
||||||
if ok {
|
authSections := strings.Split(authInfo, ",")
|
||||||
responseJson = LobbyMsg{Action: "auth", Value: "authorized"}
|
if len(authSections) != 2 {
|
||||||
|
err = errors.New("Expected 2 components to auth, in string " + authInfo)
|
||||||
|
result = "unauthenticated"
|
||||||
} else {
|
} else {
|
||||||
responseJson = LobbyMsg{Action: "auth", Value: "unauthorized"}
|
userId := authSections[0]
|
||||||
|
lobbyId := authSections[1]
|
||||||
|
|
||||||
|
if !VerifyLobbyAccess(userId, lobbyId) {
|
||||||
|
log.Printf("Unathorized to enter lobby: user %s to lobby %s", userId, lobbyId)
|
||||||
|
result = "unauthenticated"
|
||||||
|
} else {
|
||||||
|
_, ok := Users[userId]
|
||||||
|
|
||||||
|
// TODO: Verify lobby id
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
result = "authenticated"
|
||||||
|
} else {
|
||||||
|
result = "unauthenticated"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
json, err := json.Marshal(responseJson)
|
if err != nil {
|
||||||
|
log.Print("auth error: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
json, err := json.Marshal(LobbyMsg{
|
||||||
|
Action: "auth",
|
||||||
|
Value: result,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("json marshal: ", err)
|
log.Print("json marshal: ", err)
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user