From 73ebac440c164a0e515b61d42a46806dbcf11268 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Mon, 16 Jun 2014 16:42:40 +0200 Subject: [PATCH] Implemented working client side sessions lookup. --- src/app/spreed-webrtc-server/contact.go | 5 +- src/app/spreed-webrtc-server/hub.go | 77 +++++++++++-------------- src/app/spreed-webrtc-server/server.go | 4 +- src/app/spreed-webrtc-server/session.go | 31 ++++++---- 4 files changed, 57 insertions(+), 60 deletions(-) diff --git a/src/app/spreed-webrtc-server/contact.go b/src/app/spreed-webrtc-server/contact.go index 940015e4..995f92d6 100644 --- a/src/app/spreed-webrtc-server/contact.go +++ b/src/app/spreed-webrtc-server/contact.go @@ -24,7 +24,6 @@ package main import () type Contact struct { - A string - B string - Ok bool + A string + B string } diff --git a/src/app/spreed-webrtc-server/hub.go b/src/app/spreed-webrtc-server/hub.go index d1002fb3..4d81a644 100644 --- a/src/app/spreed-webrtc-server/hub.go +++ b/src/app/spreed-webrtc-server/hub.go @@ -188,12 +188,13 @@ func (h *Hub) CreateTurnData(id string) *DataTurn { } func (h *Hub) CreateSuserid(session *Session) (suserid string) { - if session.Userid != "" { + userid := session.Userid() + if userid != "" { m := hmac.New(sha256.New, h.encryptionSecret) - m.Write([]byte(session.Userid)) + m.Write([]byte(userid)) suserid = base64.StdEncoding.EncodeToString(m.Sum(nil)) } - return suserid + return } func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session { @@ -371,14 +372,15 @@ func (h *Hub) unregisterHandler(c *Connection) { return } session := c.Session + suserid := session.Userid() delete(h.connectionTable, c.Id) delete(h.sessionTable, c.Id) - if session != nil && session.Userid != "" { - user, ok := h.userTable[session.Userid] + if session != nil && suserid != "" { + user, ok := h.userTable[suserid] if ok { empty := user.RemoveSession(session) if empty { - delete(h.userTable, session.Userid) + delete(h.userTable, suserid) } } } @@ -427,20 +429,17 @@ func (h *Hub) sessionsHandler(c *Connection, srq *DataSessionsRequest, iid strin switch srq.Type { case "contact": contact := &Contact{} - err := h.contacts.Decode("contactConfirmed", srq.Token, contact) + err := h.contacts.Decode("contact", srq.Token, contact) if err != nil { log.Println("Failed to decode incoming contact token", err, srq.Token) return } - if !contact.Ok { - log.Println("Ignoring contact token without Ok", contact) - return - } // Use the userid which is not ours from the contact data. var userid string - if contact.A == c.Session.Userid { + suserid := c.Session.Userid() + if contact.A == suserid { userid = contact.B - } else if contact.B == c.Session.Userid { + } else if contact.B == suserid { userid = contact.A } if userid == "" { @@ -528,11 +527,12 @@ func (h *Hub) authenticateHandler(session *Session, st *SessionToken, userid str err := session.Authenticate(h.realm, st, userid) if err == nil { // Authentication success. + suserid := session.Userid() h.mutex.Lock() - user, ok := h.userTable[session.Userid] + user, ok := h.userTable[suserid] if !ok { - user = NewUser(session.Userid) - h.userTable[session.Userid] = user + user = NewUser(suserid) + h.userTable[suserid] = user } h.mutex.Unlock() user.AddSession(session) @@ -548,72 +548,61 @@ func (h *Hub) contactrequestHandler(c *Connection, to string, cr *DataContactReq if cr.Success { // Client replied with success. - // Decode Token and make sure c.Session.Userid and the to Session.UserId are a match. + // Decode Token and make sure c.Session.Userid and the to Session.Userid are a match. contact := &Contact{} - err = h.contacts.Decode("contactRequest", cr.Token, contact) + err = h.contacts.Decode("contact", cr.Token, contact) if err != nil { return err } - if contact.Ok { - return errors.New("received success with ok state set") - } - bSessionData := c.Session.Data() - if bSessionData.Userid == "" { + suserid := c.Session.Userid() + if suserid == "" { return errors.New("no userid") } h.mutex.RLock() - aSession, ok := h.sessionTable[to] + session, ok := h.sessionTable[to] h.mutex.RUnlock() if !ok { return errors.New("unknown to session for confirm") } - aSessionData := aSession.Data() - if aSessionData.Userid == "" { + userid := session.Userid() + if userid == "" { return errors.New("to has no userid for confirm") } - if aSessionData.Userid != contact.A { + if suserid != contact.A { return errors.New("contact mismatch in a") } - if bSessionData.Userid != contact.B { + if userid != contact.B { return errors.New("contact mismatch in b") } - contact.Ok = true - cr.Token, err = h.contacts.Encode("contactConfirmed", contact) } else { if cr.Token != "" { // Client replied with no success. - // Decode Token. - contact := &Contact{} - err = h.contacts.Decode("contactRequest", cr.Token, contact) - if err != nil { - return err - } // Remove token. cr.Token = "" } else { // New request. // Create Token with flag and c.Session.Userid and the to Session.Userid. - aSessionData := c.Session.Data() - if aSessionData.Userid == "" { + suserid := c.Session.Userid() + if suserid == "" { return errors.New("no userid") } h.mutex.RLock() - bSession, ok := h.sessionTable[to] + session, ok := h.sessionTable[to] h.mutex.RUnlock() if !ok { return errors.New("unknown to session") } - bSessionData := bSession.Data() - if bSessionData.Userid == "" { + userid := session.Userid() + if userid == "" { return errors.New("to has no userid") } - if bSessionData.Userid == aSessionData.Userid { + if userid == suserid { return errors.New("to userid cannot be the same as own userid") } // Create object. - contact := &Contact{aSessionData.Userid, bSessionData.Userid, false} + contact := &Contact{userid, suserid} // Serialize. - cr.Token, err = h.contacts.Encode("contactRequest", contact) + cr.Token, err = h.contacts.Encode("contact", contact) } } diff --git a/src/app/spreed-webrtc-server/server.go b/src/app/spreed-webrtc-server/server.go index 392048a6..cda9dd7e 100644 --- a/src/app/spreed-webrtc-server/server.go +++ b/src/app/spreed-webrtc-server/server.go @@ -44,7 +44,7 @@ func (s *Server) OnRegister(c *Connection) { Type: "Self", Id: c.Id, Sid: c.Session.Sid, - Userid: c.Session.Userid, + Userid: c.Session.Userid(), Suserid: c.h.CreateSuserid(c.Session), Token: token, Version: c.h.version, @@ -144,7 +144,7 @@ func (s *Server) OnText(c *Connection, b Buffer) { log.Println("Ignoring invalid contact request.", err) return } - msg.Chat.Chat.Status.ContactRequest.Userid = c.Session.Userid + msg.Chat.Chat.Status.ContactRequest.Userid = c.Session.Userid() } atomic.AddUint64(&c.h.unicastChatMessages, 1) s.Unicast(c, msg.Chat.To, msg.Chat) diff --git a/src/app/spreed-webrtc-server/session.go b/src/app/spreed-webrtc-server/session.go index 0d6f4fdf..66b2c1cc 100644 --- a/src/app/spreed-webrtc-server/session.go +++ b/src/app/spreed-webrtc-server/session.go @@ -33,13 +33,12 @@ var sessionNonces *securecookie.SecureCookie type Session struct { Id string Sid string - Userid string - Roomid string Ua string UpdateRev uint64 Status interface{} Nonce string mutex sync.RWMutex + userid string } func NewSession(id, sid string) *Session { @@ -60,8 +59,8 @@ func (s *Session) Update(update *SessionUpdate) uint64 { //fmt.Println("type update", key) switch key { - case "Roomid": - s.Roomid = update.Roomid + //case "Roomid": + // s.Roomid = update.Roomid case "Ua": s.Ua = update.Ua case "Status": @@ -83,7 +82,7 @@ func (s *Session) Authorize(realm string, st *SessionToken) (string, error) { if s.Id != st.Id || s.Sid != st.Sid { return "", errors.New("session id mismatch") } - if s.Userid != "" { + if s.userid != "" { return "", errors.New("session already authenticated") } @@ -100,7 +99,7 @@ func (s *Session) Authenticate(realm string, st *SessionToken, userid string) er s.mutex.Lock() defer s.mutex.Unlock() - if s.Userid != "" { + if s.userid != "" { return errors.New("session already authenticated") } if userid == "" { @@ -117,14 +116,18 @@ func (s *Session) Authenticate(realm string, st *SessionToken, userid string) er s.Nonce = "" } - s.Userid = userid + s.userid = userid s.UpdateRev++ return nil } func (s *Session) Token() *SessionToken { - return &SessionToken{Id: s.Id, Sid: s.Sid, Userid: s.Userid} + + s.mutex.RLock() + defer s.mutex.RUnlock() + + return &SessionToken{Id: s.Id, Sid: s.Sid, Userid: s.userid} } func (s *Session) Data() *DataSession { @@ -134,7 +137,7 @@ func (s *Session) Data() *DataSession { return &DataSession{ Id: s.Id, - Userid: s.Userid, + Userid: s.userid, Ua: s.Ua, Status: s.Status, Rev: s.UpdateRev, @@ -142,6 +145,12 @@ func (s *Session) Data() *DataSession { } +func (s *Session) Userid() string { + + return s.userid + +} + func (s *Session) DataSessionLeft(state string) *DataSession { s.mutex.RLock() @@ -163,7 +172,7 @@ func (s *Session) DataSessionJoined() *DataSession { return &DataSession{ Type: "Joined", Id: s.Id, - Userid: s.Userid, + Userid: s.userid, Ua: s.Ua, } @@ -177,7 +186,7 @@ func (s *Session) DataSessionStatus() *DataSession { return &DataSession{ Type: "Status", Id: s.Id, - Userid: s.Userid, + Userid: s.userid, Status: s.Status, Rev: s.UpdateRev, }