JWT token based auth
This commit is contained in:
+3
-1
@@ -3,5 +3,7 @@ package rtc
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrRoomIdMissing = errors.New("roomId is not set")
|
||||
ErrRoomIdMissing = errors.New("room is not passed in")
|
||||
ErrRoomNotFound = errors.New("requested room does not exist")
|
||||
ErrPermissionDenied = errors.New("no permissions to access the room")
|
||||
)
|
||||
|
||||
+43
-14
@@ -7,22 +7,25 @@ import (
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
// A RoomManager maintains active rooms that are hosted on the current node
|
||||
type RoomManager struct {
|
||||
rtcConf config.RTCConfig
|
||||
externalIP string
|
||||
|
||||
config WebRTCConfig
|
||||
|
||||
rooms map[string]*Room
|
||||
roomLock sync.RWMutex
|
||||
rooms map[string]*Room
|
||||
roomsByName map[string]*Room
|
||||
roomLock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewRoomManager(rtcConf config.RTCConfig, externalIP string) (m *RoomManager, err error) {
|
||||
m = &RoomManager{
|
||||
rtcConf: rtcConf,
|
||||
externalIP: externalIP,
|
||||
rooms: make(map[string]*Room),
|
||||
roomLock: sync.RWMutex{},
|
||||
rtcConf: rtcConf,
|
||||
externalIP: externalIP,
|
||||
rooms: make(map[string]*Room),
|
||||
roomsByName: make(map[string]*Room),
|
||||
roomLock: sync.RWMutex{},
|
||||
}
|
||||
|
||||
wc, err := NewWebRTCConfig(&rtcConf, externalIP)
|
||||
@@ -33,27 +36,53 @@ func NewRoomManager(rtcConf config.RTCConfig, externalIP string) (m *RoomManager
|
||||
return
|
||||
}
|
||||
|
||||
func (m *RoomManager) GetRoom(roomId string) *Room {
|
||||
func (m *RoomManager) GetRoom(idOrName string) *Room {
|
||||
m.roomLock.RLock()
|
||||
defer m.roomLock.RUnlock()
|
||||
return m.rooms[roomId]
|
||||
rm := m.rooms[idOrName]
|
||||
if rm == nil {
|
||||
rm = m.roomsByName[idOrName]
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (m *RoomManager) GetRoomWithConstraint(idOrName string, onlyName string) (*Room, error) {
|
||||
if idOrName == "" {
|
||||
idOrName = onlyName
|
||||
}
|
||||
if idOrName == "" {
|
||||
return nil, ErrRoomIdMissing
|
||||
}
|
||||
|
||||
rm := m.GetRoom(idOrName)
|
||||
if rm == nil {
|
||||
return nil, ErrRoomNotFound
|
||||
}
|
||||
|
||||
if onlyName != "" && rm.Name != onlyName {
|
||||
return nil, ErrPermissionDenied
|
||||
}
|
||||
return rm, nil
|
||||
}
|
||||
|
||||
func (m *RoomManager) CreateRoom(req *livekit.CreateRoomRequest) (r *Room, err error) {
|
||||
r, err = NewRoomForRequest(req, &m.config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r = NewRoomForRequest(req, &m.config)
|
||||
m.roomLock.Lock()
|
||||
defer m.roomLock.Unlock()
|
||||
m.rooms[r.Sid] = r
|
||||
m.roomsByName[r.Name] = r
|
||||
return
|
||||
}
|
||||
|
||||
func (m *RoomManager) DeleteRoom(roomId string) error {
|
||||
func (m *RoomManager) DeleteRoom(idOrName string) error {
|
||||
rm := m.GetRoom(idOrName)
|
||||
if rm == nil {
|
||||
return nil
|
||||
}
|
||||
m.roomLock.Lock()
|
||||
defer m.roomLock.Unlock()
|
||||
delete(m.rooms, roomId)
|
||||
delete(m.rooms, rm.Sid)
|
||||
delete(m.roomsByName, rm.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
package rtc_test
|
||||
+4
-10
@@ -4,7 +4,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/thoas/go-funk"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
@@ -22,24 +21,19 @@ type Room struct {
|
||||
//tracks map[string][]Track
|
||||
}
|
||||
|
||||
func NewRoomForRequest(req *livekit.CreateRoomRequest, config *WebRTCConfig) (*Room, error) {
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func NewRoomForRequest(req *livekit.CreateRoomRequest, config *WebRTCConfig) *Room {
|
||||
return &Room{
|
||||
Room: livekit.Room{
|
||||
Sid: utils.NewGuid(utils.RoomPrefix),
|
||||
Name: req.Name,
|
||||
EmptyTimeout: req.EmptyTimeout,
|
||||
MaxParticipants: req.MaxParticipants,
|
||||
CreationTime: time.Now().Unix(),
|
||||
Token: id.String(),
|
||||
},
|
||||
config: *config,
|
||||
lock: sync.RWMutex{},
|
||||
participants: make(map[string]Participant),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) GetParticipant(id string) Participant {
|
||||
@@ -57,9 +51,9 @@ func (r *Room) GetParticipants() []Participant {
|
||||
func (r *Room) ToRoomInfo(node *livekit.Node) *livekit.RoomInfo {
|
||||
return &livekit.RoomInfo{
|
||||
Sid: r.Sid,
|
||||
Name: r.Name,
|
||||
NodeIp: node.Ip,
|
||||
CreationTime: r.CreationTime,
|
||||
Token: r.Token,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+27
-4
@@ -15,6 +15,32 @@ const (
|
||||
numParticipants = 3
|
||||
)
|
||||
|
||||
func TestNewRoomForRequest(t *testing.T) {
|
||||
req := &livekit.CreateRoomRequest{
|
||||
Name: "myroom",
|
||||
EmptyTimeout: 120,
|
||||
MaxParticipants: 50,
|
||||
}
|
||||
|
||||
rm := rtc.NewRoomForRequest(req, &rtc.WebRTCConfig{})
|
||||
assert.NotEmpty(t, rm.Sid)
|
||||
assert.Equal(t, req.Name, rm.Name)
|
||||
assert.Equal(t, req.EmptyTimeout, rm.EmptyTimeout)
|
||||
assert.Equal(t, req.MaxParticipants, rm.MaxParticipants)
|
||||
}
|
||||
|
||||
func TestToRoomInfo(t *testing.T) {
|
||||
rm := rtc.NewRoomForRequest(&livekit.CreateRoomRequest{
|
||||
Name: "myroom",
|
||||
EmptyTimeout: 120,
|
||||
MaxParticipants: 50,
|
||||
}, &rtc.WebRTCConfig{})
|
||||
info := rm.ToRoomInfo(&livekit.Node{Ip: "0.0.0.0"})
|
||||
assert.Equal(t, rm.Sid, info.Sid)
|
||||
assert.Equal(t, rm.Name, info.Name)
|
||||
assert.Equal(t, "0.0.0.0", info.NodeIp)
|
||||
}
|
||||
|
||||
func TestRoomJoin(t *testing.T) {
|
||||
t.Run("joining returns existing participant data", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, numParticipants)
|
||||
@@ -96,10 +122,7 @@ func TestNewTrack(t *testing.T) {
|
||||
}
|
||||
|
||||
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {
|
||||
rm, err := rtc.NewRoomForRequest(&livekit.CreateRoomRequest{}, &rtc.WebRTCConfig{})
|
||||
if err != nil {
|
||||
panic("could not create a room")
|
||||
}
|
||||
rm := rtc.NewRoomForRequest(&livekit.CreateRoomRequest{}, &rtc.WebRTCConfig{})
|
||||
for i := 0; i < num; i++ {
|
||||
participant := newMockParticipant("")
|
||||
err := rm.Join(participant)
|
||||
|
||||
Reference in New Issue
Block a user