Browse Source

Added non public session id to prepare for authorization.

pull/28/head
Simon Eisenmann 11 years ago committed by Simon Eisenmann
parent
commit
dfba1f5640
  1. 4
      doc/CHANNELING-API.txt
  2. 1
      src/app/spreed-speakfreely-server/channeling.go
  3. 10
      src/app/spreed-speakfreely-server/hub.go
  4. 17
      src/app/spreed-speakfreely-server/random.go
  5. 2
      src/app/spreed-speakfreely-server/rooms.go
  6. 1
      src/app/spreed-speakfreely-server/server.go
  7. 14
      src/app/spreed-speakfreely-server/session.go

4
doc/CHANNELING-API.txt

@ -85,6 +85,7 @@ Special purpose documents for channling
{ {
"Type": "Self", "Type": "Self",
"Id": "4", "Id": "4",
"Sid": "5157",
"Userid": "", "Userid": "",
"Token": "some-very-long-string", "Token": "some-very-long-string",
"Version": "server-version-number", "Version": "server-version-number",
@ -108,7 +109,8 @@ Special purpose documents for channling
Keys: Keys:
Type : Self (string) Type : Self (string)
Id : Channel id for this connection (string). Id : Public Session id for this connection (string).
Sid : Secure (non public) id for this session (string).
Userid : User id if this session belongs to an authenticated user. Else empty. Userid : User id if this session belongs to an authenticated user. Else empty.
Token : Security token (string), to restablish connection with the same Token : Security token (string), to restablish connection with the same
session. Pass the value as URL query parameter t, to the websocket URL. session. Pass the value as URL query parameter t, to the websocket URL.

1
src/app/spreed-speakfreely-server/channeling.go

@ -48,6 +48,7 @@ type DataAnswer struct {
type DataSelf struct { type DataSelf struct {
Type string Type string
Id string Id string
Sid string
Userid string Userid string
Token string Token string
Version string Version string

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

@ -165,13 +165,15 @@ func (h *Hub) CreateSession(st *SessionToken) *Session {
// random data in itself should be sufficent if we do not validate // random data in itself should be sufficent if we do not validate
// session ids somewhere? // session ids somewhere?
session := &Session{} var session *Session
if st == nil { if st == nil {
session.Id, _ = h.tickets.Encode("id", fmt.Sprintf("%s", securecookie.GenerateRandomKey(32))) sid := NewRandomString(32)
log.Println("Created new session id", len(session.Id), session.Id) id, _ := h.tickets.Encode("id", sid)
session = NewSession(id, sid, "")
log.Println("Created new session id", len(id), id, sid)
} else { } else {
session.Apply(st) session = NewSession(st.Id, st.Sid, st.Userid)
} }
return session return session

17
src/app/spreed-speakfreely-server/random.go

@ -23,7 +23,6 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"encoding/base64"
pseudoRand "math/rand" pseudoRand "math/rand"
"time" "time"
) )
@ -32,7 +31,7 @@ const (
dict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW0123456789" dict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW0123456789"
) )
func RandomString(length int) string { func NewRandomString(length int) string {
buf := make([]byte, length) buf := make([]byte, length)
_, err := rand.Read(buf) _, err := rand.Read(buf)
@ -50,20 +49,6 @@ func RandomString(length int) string {
} }
func RandomUrlString(length int) string {
buf := make([]byte, length)
_, err := rand.Read(buf)
if err != nil {
// fallback to pseudo-random
for i := 0; i < length; i++ {
buf[i] = byte(pseudoRand.Intn(256))
}
}
return base64.URLEncoding.EncodeToString(buf)
}
func init() { func init() {
// Make sure to seed default random generator. // Make sure to seed default random generator.
pseudoRand.Seed(time.Now().UTC().UnixNano()) pseudoRand.Seed(time.Now().UTC().UnixNano())

2
src/app/spreed-speakfreely-server/rooms.go

@ -37,7 +37,7 @@ type Rooms struct {
func (rooms *Rooms) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) { func (rooms *Rooms) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) {
name := RandomString(11) name := NewRandomString(11)
return 200, &Room{name, fmt.Sprintf("/%s", name)}, http.Header{"Content-Type": {"application/json"}} return 200, &Room{name, fmt.Sprintf("/%s", name)}, http.Header{"Content-Type": {"application/json"}}
} }

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

@ -43,6 +43,7 @@ func (s *Server) OnRegister(c *Connection) {
s.Unicast(c, c.Id, &DataSelf{ s.Unicast(c, c.Id, &DataSelf{
Type: "Self", Type: "Self",
Id: c.Id, Id: c.Id,
Sid: c.Session.Sid,
Userid: c.Session.Userid, Userid: c.Session.Userid,
Token: token, Token: token,
Version: c.h.version, Version: c.h.version,

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

@ -27,6 +27,7 @@ import (
type Session struct { type Session struct {
Id string Id string
Sid string
Userid string Userid string
Roomid string Roomid string
Ua string Ua string
@ -35,6 +36,16 @@ type Session struct {
mutex sync.RWMutex mutex sync.RWMutex
} }
func NewSession(id, sid, userid string) *Session {
return &Session{
Id: id,
Sid: sid,
Userid: userid,
}
}
func (s *Session) Update(update *SessionUpdate) uint64 { func (s *Session) Update(update *SessionUpdate) uint64 {
s.mutex.Lock() s.mutex.Lock()
@ -69,7 +80,7 @@ func (s *Session) Apply(st *SessionToken) {
} }
func (s *Session) Token() *SessionToken { func (s *Session) Token() *SessionToken {
return &SessionToken{Id: s.Id, Userid: s.Userid} return &SessionToken{Id: s.Id, Sid: s.Sid, Userid: s.Userid}
} }
func (s *Session) Data() *DataSession { func (s *Session) Data() *DataSession {
@ -97,5 +108,6 @@ type SessionUpdate struct {
type SessionToken struct { type SessionToken struct {
Id string Id string
Sid string
Userid string Userid string
} }

Loading…
Cancel
Save