Browse Source

Cleaned up token and session id generation in preperation for userids.

pull/28/head
Simon Eisenmann 11 years ago committed by Simon Eisenmann
parent
commit
c5bd56c718
  1. 2
      server.conf.in
  2. 11
      src/app/spreed-speakfreely-server/connection.go
  3. 51
      src/app/spreed-speakfreely-server/hub.go
  4. 4
      src/app/spreed-speakfreely-server/server.go
  5. 16
      src/app/spreed-speakfreely-server/session.go

2
server.conf.in

@ -24,7 +24,7 @@ listen = 127.0.0.1:8080
#stunURIs = stun.l.google.com:19302 #stunURIs = stun.l.google.com:19302
#turnURIs = turn:turnserver:port?transport=udp turn:anotherturnserver:port?transport=tcp turns:turnserver:443?transport=tcp #turnURIs = turn:turnserver:port?transport=udp turn:anotherturnserver:port?transport=tcp turns:turnserver:443?transport=tcp
#turnSecret = the-default-turn-shared-secret-do-not-keep #turnSecret = the-default-turn-shared-secret-do-not-keep
sessionSecret = the-default-secret-do-not-keep sessionSecret = the-default-secret-do-not-keep-me # Use 32 or 64 bytes random data
#tokenFile = tokens.txt # If set, everyone needs to give one of the tokens to launch the web client. One token per line in the file. #tokenFile = tokens.txt # If set, everyone needs to give one of the tokens to launch the web client. One token per line in the file.
#globalRoom = global # Enables a global room. Users in that room are in all rooms. #globalRoom = global # Enables a global room. Users in that room are in all rooms.
#defaultRoomEnabled = true # Set to false to disable default room. #defaultRoomEnabled = true # Set to false to disable default room.

11
src/app/spreed-speakfreely-server/connection.go

@ -112,24 +112,25 @@ func (c *Connection) close() {
func (c *Connection) register() error { func (c *Connection) register() error {
id, err := c.h.EncodeTicket("id", "") id, err := c.h.CreateSessionid()
if err != nil { if err != nil {
log.Println("Failed to create new Id while register", err) log.Println("Failed to create new Id while register", err)
return err return err
} }
c.Id = id c.Id = id
//log.Println("Created new id", id) log.Println("Created new id", len(id), id)
c.h.registerHandler(c) c.h.registerHandler(c)
return nil return nil
} }
func (c *Connection) reregister(token string) error { func (c *Connection) reregister(token string) error {
if id, err := c.h.DecodeTicket("token", token); err == nil { if st, err := c.h.DecodeSessionToken(token); err == nil {
c.Id = id c.Id = st.Id
c.h.registerHandler(c) c.h.registerHandler(c)
c.Session.Apply(st)
} else { } else {
log.Println("Error while decoding token", err) log.Println("Error while decoding session token", err)
c.register() c.register()
} }
return nil return nil

51
src/app/spreed-speakfreely-server/hub.go

@ -92,6 +92,10 @@ func NewHub(version string, config *Config, sessionSecret, turnSecret string) *H
turnSecret: []byte(turnSecret), turnSecret: []byte(turnSecret),
} }
if len(h.sessionSecret) < 32 {
log.Printf("Weak sessionSecret (only %d bytes). It is recommended to use a key with 32 or 64 bytes.\n", len(h.sessionSecret))
}
h.tickets = securecookie.New(h.sessionSecret, nil) h.tickets = securecookie.New(h.sessionSecret, nil)
h.buffers = NewBufferCache(1024, bytes.MinRead) h.buffers = NewBufferCache(1024, bytes.MinRead)
h.buddyImages = NewImageCache() h.buddyImages = NewImageCache()
@ -155,21 +159,27 @@ func (h *Hub) CreateTurnData(id string) *DataTurn {
} }
func (h *Hub) EncodeTicket(key, value string) (string, error) { func (h *Hub) CreateSessionid() (string, error) {
if value == "" { // NOTE(longsleep): Is it required to make this a secure cookie,
// Create new id. // random data in itself should be sufficent if we do not validate
value = fmt.Sprintf("%s", securecookie.GenerateRandomKey(16)) // session ids somewhere?
} value := fmt.Sprintf("%s", securecookie.GenerateRandomKey(32))
return h.tickets.Encode(key, value) return h.tickets.Encode("id", value)
}
func (h *Hub) EncodeSessionToken(st *SessionToken) (string, error) {
return h.tickets.Encode("token", st)
} }
func (h *Hub) DecodeTicket(key, value string) (string, error) { func (h *Hub) DecodeSessionToken(token string) (*SessionToken, error) {
result := "" st := &SessionToken{}
err := h.tickets.Decode(key, value, &result) err := h.tickets.Decode("token", token, st)
return result, err return st, err
} }
@ -180,8 +190,8 @@ func (h *Hub) GetRoom(id string) *RoomWorker {
if !ok { if !ok {
h.mutex.RUnlock() h.mutex.RUnlock()
h.mutex.Lock() h.mutex.Lock()
// need to re-check, another thread might have created the room // Need to re-check, another thread might have created the room
// while we waited for the lock // while we waited for the lock.
room, ok = h.roomTable[id] room, ok = h.roomTable[id]
if !ok { if !ok {
room = NewRoomWorker(h, id) room = NewRoomWorker(h, id)
@ -252,26 +262,23 @@ func (h *Hub) registerHandler(c *Connection) {
h.count++ h.count++
c.Idx = h.count c.Idx = h.count
s := &Session{Id: c.Id} s := &Session{Id: c.Id}
h.sessionTable[c.Id] = s
c.Session = s c.Session = s
c.IsRegistered = true c.IsRegistered = true
// Register connection or replace existing one. // Register connection or replace existing one.
if ec, ok := h.connectionTable[c.Id]; ok { if ec, ok := h.connectionTable[c.Id]; ok {
delete(h.connectionTable, ec.Id)
ec.IsRegistered = false ec.IsRegistered = false
ec.close() ec.close()
h.connectionTable[c.Id] = c
h.mutex.Unlock()
//log.Printf("Register (%d) from %s: %s (existing)\n", c.Idx, c.RemoteAddr, c.Id) //log.Printf("Register (%d) from %s: %s (existing)\n", c.Idx, c.RemoteAddr, c.Id)
} else {
h.connectionTable[c.Id] = c
//fmt.Println("registered", c.Id)
h.mutex.Unlock()
//log.Printf("Register (%d) from %s: %s\n", c.Idx, c.RemoteAddr, c.Id)
h.server.OnRegister(c)
} }
h.connectionTable[c.Id] = c
h.sessionTable[c.Id] = s
//fmt.Println("registered", c.Id)
h.mutex.Unlock()
//log.Printf("Register (%d) from %s: %s\n", c.Idx, c.RemoteAddr, c.Id)
h.server.OnRegister(c)
} }
func (h *Hub) unregisterHandler(c *Connection) { func (h *Hub) unregisterHandler(c *Connection) {

4
src/app/spreed-speakfreely-server/server.go

@ -37,7 +37,9 @@ type Server struct {
func (s *Server) OnRegister(c *Connection) { func (s *Server) OnRegister(c *Connection) {
//log.Println("OnRegister", c.id) //log.Println("OnRegister", c.id)
if token, err := c.h.EncodeTicket("token", c.Id); err == nil { st := &SessionToken{Id: c.Id}
if token, err := c.h.EncodeSessionToken(st); err == nil {
log.Println("Created new session token", len(token), token)
// Send stuff back. // Send stuff back.
s.Unicast(c, c.Id, &DataSelf{Type: "Self", Id: c.Id, Token: token, Version: c.h.version, Turn: c.h.CreateTurnData(c.Id), Stun: c.h.config.StunURIs}) s.Unicast(c, c.Id, &DataSelf{Type: "Self", Id: c.Id, Token: token, Version: c.h.version, Turn: c.h.CreateTurnData(c.Id), Stun: c.h.config.StunURIs})
} else { } else {

16
src/app/spreed-speakfreely-server/session.go

@ -27,6 +27,7 @@ import (
type Session struct { type Session struct {
Id string Id string
Userid string
Roomid string Roomid string
Ua string Ua string
UpdateRev uint64 UpdateRev uint64
@ -58,6 +59,15 @@ func (s *Session) Update(update *SessionUpdate) uint64 {
} }
func (s *Session) Apply(st *SessionToken) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Id = st.Id
s.Userid = st.Userid
}
func (s *Session) Data() *DataSession { func (s *Session) Data() *DataSession {
s.mutex.RLock() s.mutex.RLock()
@ -65,6 +75,7 @@ func (s *Session) Data() *DataSession {
return &DataSession{ return &DataSession{
Id: s.Id, Id: s.Id,
Userid: s.Userid,
Ua: s.Ua, Ua: s.Ua,
Status: s.Status, Status: s.Status,
Rev: s.UpdateRev, Rev: s.UpdateRev,
@ -79,3 +90,8 @@ type SessionUpdate struct {
Ua string Ua string
Status interface{} Status interface{}
} }
type SessionToken struct {
Id string
Userid string
}

Loading…
Cancel
Save