Skip to content

Commit

Permalink
pins should be strings not integers
Browse files Browse the repository at this point in the history
  • Loading branch information
tibroc committed Dec 5, 2024
1 parent 169ce99 commit 255aad3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
6 changes: 3 additions & 3 deletions room-hub/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type RoomConfig struct {
}

// creates a PairingPIN message and marshals it to JSON
func createPairingPINMessage(pin int) ([]byte, error) {
func createPairingPINMessage(pin string) ([]byte, error) {

message := PairingPINMessage{
Type: MessageTypePairingPIN,
Expand All @@ -51,12 +51,12 @@ func createPairingPINMessage(pin int) ([]byte, error) {

type PairingPINUserInputMessage struct {
Type MessageType `json:"type" validate:"required"`
PIN int `json:"PIN" validate:"required,numeric,min=100000,max=999999"`
PIN string `json:"PIN" validate:"required,numeric,len=6"`
}

type PairingPINMessage struct {
Type MessageType `json:"type" validate:"required"`
PIN int `json:"PIN" validate:"required"`
PIN string `json:"PIN" validate:"required"`
}

type PairingPINFoundMessage struct {
Expand Down
34 changes: 16 additions & 18 deletions room-hub/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"crypto/rand"
"encoding/json"
"errors"
"github.com/go-playground/validator/v10"
"log"
"math/big"
"net/http"
"os"
"strconv"
"sync"
"time"

"github.com/go-playground/validator/v10"

"github.com/google/uuid"
"github.com/gorilla/websocket"
)
Expand Down Expand Up @@ -49,7 +49,7 @@ var upgrader = websocket.Upgrader{
type ConnectionManager struct {
sync.Mutex
rooms map[string]*Room
pinToRoom map[int]string // Used to look up roomID via room PIN
pinToRoom map[string]string // Used to look up roomID via room PIN
}

// Struct to store room connection and configuration
Expand All @@ -62,11 +62,11 @@ type Room struct {
// Initialize ConnectionManager instance
var connManager = &ConnectionManager{
rooms: make(map[string]*Room),
pinToRoom: make(map[int]string),
pinToRoom: make(map[string]string),
}

// Function to generate a new room ID
func generatePIN() (int, error) {
func generatePIN() (string, error) {
const PINLength int = 6
const maxTries = 1000

Expand All @@ -75,26 +75,24 @@ func generatePIN() (int, error) {
var pin, err = randomString(PINLength, Digits)
if err != nil {
log.Printf("failed to generate room ID: %s", err)
return 0, err
return "", err
}

var pinInt, _ = strconv.Atoi(pin)

_, PINexists := connManager.pinToRoom[pinInt]
_, PINexists := connManager.pinToRoom[pin]
if !PINexists {
return pinInt, nil
return pin, nil
}
}
// No pin generated after maxTries
return 0, errors.New("PIN generation failed")
return "", errors.New("PIN generation failed")
}

func generateVerificationCode() (string, error) {
const length int = 4
return randomString(length, ASCIILettersUppercase+Digits)
}

func rotatePIN(roomID string, oldPIN int, roomConn *websocket.Conn) (newPIN int) {
func rotatePIN(roomID string, oldPIN string, roomConn *websocket.Conn) (newPIN string) {
connManager.Lock()
// Generate a new PIN
newPIN, err := generatePIN()
Expand All @@ -111,16 +109,16 @@ func rotatePIN(roomID string, oldPIN int, roomConn *websocket.Conn) (newPIN int)
roomPINMessageJSON, err := createPairingPINMessage(newPIN)
if err != nil {
roomConn.Close()
return 0
return ""
}

if err := roomConn.WriteMessage(websocket.TextMessage, roomPINMessageJSON); err != nil {
log.Println("write error:", err)
roomConn.Close()
return 0
return ""
}

log.Printf("Rotated PIN for roomId: %s, pin: %d", roomID, newPIN)
log.Printf("Rotated PIN for roomId: %s, pin: %s", roomID, newPIN)
return newPIN
}

Expand Down Expand Up @@ -227,7 +225,7 @@ func handleRegisterRoomMessage(roomConn *websocket.Conn, msg RegisterRoomMessage
return
}

log.Printf("Room connected with pin: %d, roomID: %s", pin, roomID)
log.Printf("Room connected with pin: %s, roomID: %s", pin, roomID)

currentPIN := pin
go func() {
Expand Down Expand Up @@ -380,7 +378,7 @@ func handlePairingPINMessage(pluginConn *websocket.Conn, pairingPINUserInputMess
connManager.Lock()
roomID, exists := connManager.pinToRoom[pin]
if !exists {
log.Printf("Invalid pin received: %d", pin)
log.Printf("Invalid pin received: %s", pin)
connManager.Unlock()

// Respond with an error message
Expand Down Expand Up @@ -408,7 +406,7 @@ func handlePairingPINMessage(pluginConn *websocket.Conn, pairingPINUserInputMess
close(stopChan)
connManager.Unlock()

log.Printf("Resolved room with roomID: %s for pin: %d", roomID, pin)
log.Printf("Resolved room with roomID: %s for pin: %s", roomID, pin)

return room
}
Expand Down

0 comments on commit 255aad3

Please sign in to comment.