Browse Source

Implemented working client side sessions lookup.

pull/48/head
Simon Eisenmann 11 years ago
parent
commit
73ebac440c
  1. 5
      src/app/spreed-webrtc-server/contact.go
  2. 77
      src/app/spreed-webrtc-server/hub.go
  3. 4
      src/app/spreed-webrtc-server/server.go
  4. 31
      src/app/spreed-webrtc-server/session.go

5
src/app/spreed-webrtc-server/contact.go

@ -24,7 +24,6 @@ package main
import () import ()
type Contact struct { type Contact struct {
A string A string
B string B string
Ok bool
} }

77
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) { func (h *Hub) CreateSuserid(session *Session) (suserid string) {
if session.Userid != "" { userid := session.Userid()
if userid != "" {
m := hmac.New(sha256.New, h.encryptionSecret) m := hmac.New(sha256.New, h.encryptionSecret)
m.Write([]byte(session.Userid)) m.Write([]byte(userid))
suserid = base64.StdEncoding.EncodeToString(m.Sum(nil)) suserid = base64.StdEncoding.EncodeToString(m.Sum(nil))
} }
return suserid return
} }
func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session { func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session {
@ -371,14 +372,15 @@ func (h *Hub) unregisterHandler(c *Connection) {
return return
} }
session := c.Session session := c.Session
suserid := session.Userid()
delete(h.connectionTable, c.Id) delete(h.connectionTable, c.Id)
delete(h.sessionTable, c.Id) delete(h.sessionTable, c.Id)
if session != nil && session.Userid != "" { if session != nil && suserid != "" {
user, ok := h.userTable[session.Userid] user, ok := h.userTable[suserid]
if ok { if ok {
empty := user.RemoveSession(session) empty := user.RemoveSession(session)
if empty { 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 { switch srq.Type {
case "contact": case "contact":
contact := &Contact{} contact := &Contact{}
err := h.contacts.Decode("contactConfirmed", srq.Token, contact) err := h.contacts.Decode("contact", srq.Token, contact)
if err != nil { if err != nil {
log.Println("Failed to decode incoming contact token", err, srq.Token) log.Println("Failed to decode incoming contact token", err, srq.Token)
return return
} }
if !contact.Ok {
log.Println("Ignoring contact token without Ok", contact)
return
}
// Use the userid which is not ours from the contact data. // Use the userid which is not ours from the contact data.
var userid string var userid string
if contact.A == c.Session.Userid { suserid := c.Session.Userid()
if contact.A == suserid {
userid = contact.B userid = contact.B
} else if contact.B == c.Session.Userid { } else if contact.B == suserid {
userid = contact.A userid = contact.A
} }
if userid == "" { if userid == "" {
@ -528,11 +527,12 @@ func (h *Hub) authenticateHandler(session *Session, st *SessionToken, userid str
err := session.Authenticate(h.realm, st, userid) err := session.Authenticate(h.realm, st, userid)
if err == nil { if err == nil {
// Authentication success. // Authentication success.
suserid := session.Userid()
h.mutex.Lock() h.mutex.Lock()
user, ok := h.userTable[session.Userid] user, ok := h.userTable[suserid]
if !ok { if !ok {
user = NewUser(session.Userid) user = NewUser(suserid)
h.userTable[session.Userid] = user h.userTable[suserid] = user
} }
h.mutex.Unlock() h.mutex.Unlock()
user.AddSession(session) user.AddSession(session)
@ -548,72 +548,61 @@ func (h *Hub) contactrequestHandler(c *Connection, to string, cr *DataContactReq
if cr.Success { if cr.Success {
// Client replied with 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{} contact := &Contact{}
err = h.contacts.Decode("contactRequest", cr.Token, contact) err = h.contacts.Decode("contact", cr.Token, contact)
if err != nil { if err != nil {
return err return err
} }
if contact.Ok { suserid := c.Session.Userid()
return errors.New("received success with ok state set") if suserid == "" {
}
bSessionData := c.Session.Data()
if bSessionData.Userid == "" {
return errors.New("no userid") return errors.New("no userid")
} }
h.mutex.RLock() h.mutex.RLock()
aSession, ok := h.sessionTable[to] session, ok := h.sessionTable[to]
h.mutex.RUnlock() h.mutex.RUnlock()
if !ok { if !ok {
return errors.New("unknown to session for confirm") return errors.New("unknown to session for confirm")
} }
aSessionData := aSession.Data() userid := session.Userid()
if aSessionData.Userid == "" { if userid == "" {
return errors.New("to has no userid for confirm") 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") return errors.New("contact mismatch in a")
} }
if bSessionData.Userid != contact.B { if userid != contact.B {
return errors.New("contact mismatch in b") return errors.New("contact mismatch in b")
} }
contact.Ok = true
cr.Token, err = h.contacts.Encode("contactConfirmed", contact)
} else { } else {
if cr.Token != "" { if cr.Token != "" {
// Client replied with no success. // Client replied with no success.
// Decode Token.
contact := &Contact{}
err = h.contacts.Decode("contactRequest", cr.Token, contact)
if err != nil {
return err
}
// Remove token. // Remove token.
cr.Token = "" cr.Token = ""
} else { } else {
// New request. // New request.
// Create Token with flag and c.Session.Userid and the to Session.Userid. // Create Token with flag and c.Session.Userid and the to Session.Userid.
aSessionData := c.Session.Data() suserid := c.Session.Userid()
if aSessionData.Userid == "" { if suserid == "" {
return errors.New("no userid") return errors.New("no userid")
} }
h.mutex.RLock() h.mutex.RLock()
bSession, ok := h.sessionTable[to] session, ok := h.sessionTable[to]
h.mutex.RUnlock() h.mutex.RUnlock()
if !ok { if !ok {
return errors.New("unknown to session") return errors.New("unknown to session")
} }
bSessionData := bSession.Data() userid := session.Userid()
if bSessionData.Userid == "" { if userid == "" {
return errors.New("to has no 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") return errors.New("to userid cannot be the same as own userid")
} }
// Create object. // Create object.
contact := &Contact{aSessionData.Userid, bSessionData.Userid, false} contact := &Contact{userid, suserid}
// Serialize. // Serialize.
cr.Token, err = h.contacts.Encode("contactRequest", contact) cr.Token, err = h.contacts.Encode("contact", contact)
} }
} }

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

@ -44,7 +44,7 @@ func (s *Server) OnRegister(c *Connection) {
Type: "Self", Type: "Self",
Id: c.Id, Id: c.Id,
Sid: c.Session.Sid, Sid: c.Session.Sid,
Userid: c.Session.Userid, Userid: c.Session.Userid(),
Suserid: c.h.CreateSuserid(c.Session), Suserid: c.h.CreateSuserid(c.Session),
Token: token, Token: token,
Version: c.h.version, Version: c.h.version,
@ -144,7 +144,7 @@ func (s *Server) OnText(c *Connection, b Buffer) {
log.Println("Ignoring invalid contact request.", err) log.Println("Ignoring invalid contact request.", err)
return 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) atomic.AddUint64(&c.h.unicastChatMessages, 1)
s.Unicast(c, msg.Chat.To, msg.Chat) s.Unicast(c, msg.Chat.To, msg.Chat)

31
src/app/spreed-webrtc-server/session.go

@ -33,13 +33,12 @@ var sessionNonces *securecookie.SecureCookie
type Session struct { type Session struct {
Id string Id string
Sid string Sid string
Userid string
Roomid string
Ua string Ua string
UpdateRev uint64 UpdateRev uint64
Status interface{} Status interface{}
Nonce string Nonce string
mutex sync.RWMutex mutex sync.RWMutex
userid string
} }
func NewSession(id, sid string) *Session { func NewSession(id, sid string) *Session {
@ -60,8 +59,8 @@ func (s *Session) Update(update *SessionUpdate) uint64 {
//fmt.Println("type update", key) //fmt.Println("type update", key)
switch key { switch key {
case "Roomid": //case "Roomid":
s.Roomid = update.Roomid // s.Roomid = update.Roomid
case "Ua": case "Ua":
s.Ua = update.Ua s.Ua = update.Ua
case "Status": 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 { if s.Id != st.Id || s.Sid != st.Sid {
return "", errors.New("session id mismatch") return "", errors.New("session id mismatch")
} }
if s.Userid != "" { if s.userid != "" {
return "", errors.New("session already authenticated") return "", errors.New("session already authenticated")
} }
@ -100,7 +99,7 @@ func (s *Session) Authenticate(realm string, st *SessionToken, userid string) er
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()
if s.Userid != "" { if s.userid != "" {
return errors.New("session already authenticated") return errors.New("session already authenticated")
} }
if userid == "" { if userid == "" {
@ -117,14 +116,18 @@ func (s *Session) Authenticate(realm string, st *SessionToken, userid string) er
s.Nonce = "" s.Nonce = ""
} }
s.Userid = userid s.userid = userid
s.UpdateRev++ s.UpdateRev++
return nil return nil
} }
func (s *Session) Token() *SessionToken { 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 { func (s *Session) Data() *DataSession {
@ -134,7 +137,7 @@ func (s *Session) Data() *DataSession {
return &DataSession{ return &DataSession{
Id: s.Id, Id: s.Id,
Userid: s.Userid, Userid: s.userid,
Ua: s.Ua, Ua: s.Ua,
Status: s.Status, Status: s.Status,
Rev: s.UpdateRev, 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 { func (s *Session) DataSessionLeft(state string) *DataSession {
s.mutex.RLock() s.mutex.RLock()
@ -163,7 +172,7 @@ func (s *Session) DataSessionJoined() *DataSession {
return &DataSession{ return &DataSession{
Type: "Joined", Type: "Joined",
Id: s.Id, Id: s.Id,
Userid: s.Userid, Userid: s.userid,
Ua: s.Ua, Ua: s.Ua,
} }
@ -177,7 +186,7 @@ func (s *Session) DataSessionStatus() *DataSession {
return &DataSession{ return &DataSession{
Type: "Status", Type: "Status",
Id: s.Id, Id: s.Id,
Userid: s.Userid, Userid: s.userid,
Status: s.Status, Status: s.Status,
Rev: s.UpdateRev, Rev: s.UpdateRev,
} }

Loading…
Cancel
Save