@ -46,6 +46,7 @@ const (
@@ -46,6 +46,7 @@ const (
maxUsersLength = 5000
)
// TODO(longsleep): Get rid of MessageRequest type.
type MessageRequest struct {
From string
To string
@ -79,6 +80,7 @@ type Hub struct {
@@ -79,6 +80,7 @@ type Hub struct {
encryptionSecret [ ] byte
turnSecret [ ] byte
tickets * securecookie . SecureCookie
attestations * securecookie . SecureCookie
count uint64
mutex sync . RWMutex
buffers BufferCache
@ -114,11 +116,14 @@ func NewHub(version string, config *Config, sessionSecret, encryptionSecret, tur
@@ -114,11 +116,14 @@ func NewHub(version string, config *Config, sessionSecret, encryptionSecret, tur
h . tickets . MaxAge ( 86400 * 30 ) // 30 days
h . tickets . HashFunc ( sha256 . New )
h . tickets . BlockFunc ( aes . NewCipher )
h . attestations = securecookie . New ( h . sessionSecret , nil )
h . attestations . MaxAge ( 300 ) // 5 minutes
h . tickets . HashFunc ( sha256 . New )
h . buffers = NewBufferCache ( 1024 , bytes . MinRead )
h . buddyImages = NewImageCache ( )
h . tokenName = fmt . Sprintf ( "token@%s" , h . realm )
h . contacts = securecookie . New ( h . sessionSecret , h . encryptionSecret )
h . contacts . MaxAge ( 0 )
h . contacts . MaxAge ( 0 ) // Forever
h . contacts . HashFunc ( sha256 . New )
h . contacts . BlockFunc ( aes . NewCipher )
return h
@ -214,7 +219,7 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session {
@@ -214,7 +219,7 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session {
if st == nil {
sid := NewRandomString ( 32 )
id , _ := h . tickets . Encode ( "id" , sid )
session = NewSession ( id , sid )
session = NewSession ( h , id , sid )
log . Println ( "Created new session id" , len ( id ) , id , sid )
} else {
if userid == "" {
@ -223,7 +228,7 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session {
@@ -223,7 +228,7 @@ func (h *Hub) CreateSession(request *http.Request, st *SessionToken) *Session {
if ! usersEnabled {
userid = ""
}
session = NewSession ( st . Id , st . Sid )
session = NewSession ( h , st . Id , st . Sid )
}
if userid != "" {
@ -250,6 +255,25 @@ func (h *Hub) ValidateSession(id, sid string) bool {
@@ -250,6 +255,25 @@ func (h *Hub) ValidateSession(id, sid string) bool {
}
/ *
func ( h * Hub ) EncodeAttestation ( session * Session ) ( string , error ) {
attestation , err := h . attestations . Encode ( "attestation" , session . Id )
if err == nil {
session . UpdateAttestation ( attestation )
}
return attestation , err
}
func ( h * Hub ) DecodeAttestation ( token string ) ( string , error ) {
var id string
err := h . attestations . Decode ( "attestation" , token , & id )
return id , err
} * /
func ( h * Hub ) EncodeSessionToken ( st * SessionToken ) ( string , error ) {
return h . tickets . Encode ( h . tokenName , st )
@ -448,13 +472,27 @@ func (h *Hub) sessionsHandler(c *Connection, srq *DataSessionsRequest, iid strin
@@ -448,13 +472,27 @@ func (h *Hub) sessionsHandler(c *Connection, srq *DataSessionsRequest, iid strin
}
// Find foreign user.
h . mutex . RLock ( )
defer h . mutex . RUnlock ( )
user , ok := h . userTable [ userid ]
h . mutex . RUnlock ( )
if ! ok {
return
}
// Add sessions for forein user.
users = user . SessionsData ( )
case "session" :
id , err := c . Session . attestation . Decode ( srq . Token )
if err != nil {
log . Println ( "Failed to decode incoming attestation" , err , srq . Token )
return
}
h . mutex . RLock ( )
session , ok := h . sessionTable [ id ]
h . mutex . RUnlock ( )
if ! ok {
return
}
users = make ( [ ] * DataSession , 1 , 1 )
users [ 0 ] = session . Data ( )
default :
log . Println ( "Unkown incoming sessions request type" , srq . Type )
}