|
|
@ -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) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|