From 75a9a00e87da7bc4bb6990330ecb6b217d98de82 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 30 Dec 2015 21:50:46 +0100 Subject: improved response handling at websocket interface diff --git a/session_store.go b/session_store.go index 7fe6585..0d149c6 100644 --- a/session_store.go +++ b/session_store.go @@ -58,6 +58,19 @@ type getSessionRequest struct { response chan getSessionResponse } +type listSessionsResponse struct { + sessions map[string]string + responsecode int + errorstring string +} + +type listSessionsRequest struct { + user string + password string + trusted bool + response chan listSessionsResponse +} + type removeSessionResponse struct { responsecode int errorstring string @@ -80,6 +93,7 @@ type SessionStore struct { done chan bool newChan chan newSessionRequest getChan chan getSessionRequest + listChan chan listSessionsRequest removeChan chan removeSessionRequest } @@ -134,6 +148,30 @@ func (self *SessionStore) get(user, id string) (resp getSessionResponse) { return } +func (self *SessionStore) list(user, password string, trusted bool) (resp listSessionsResponse) { + resp.responsecode = http.StatusOK + resp.errorstring = "OK" + // TODO: enable this check as soon as the session store handles the rddb itself + // if !trusted { + // if ok, err := self.rddb.CheckPassword(user, password); err != nil { + // resp.responsecode = http.StatusInternalServerError + // resp.errorstring = err.Error() + // return + // } else if !ok { + // resp.responsecode = http.StatusUnauthorized + // resp.errorstring = "invalid username and/or password" + // return + // } + // } + resp.sessions = make(map[string]string) + if sessions, exists := self.store[user]; exists { + for id, e := range sessions { + resp.sessions[id] = e.refId + } + } + return +} + func (self *SessionStore) remove(user, id string) (resp removeSessionResponse) { resp.responsecode = http.StatusOK resp.errorstring = "OK" @@ -164,6 +202,8 @@ func (self *SessionStore) dispatchRequests() { req.response <- self.new(req.ctx, req.refId) case req := <-self.getChan: req.response <- self.get(req.user, req.id) + case req := <-self.listChan: + req.response <- self.list(req.user, req.password, req.trusted) case req := <-self.removeChan: req.response <- self.remove(req.user, req.id) } @@ -176,6 +216,7 @@ func (self *SessionStore) dispatchRequests() { type SessionStoreChan struct { newChan chan<- newSessionRequest getChan chan<- getSessionRequest + listChan chan listSessionsRequest removeChan chan<- removeSessionRequest } @@ -203,6 +244,19 @@ func (self *SessionStoreChan) Get(user, id string) (*SessionChan, string, int, s return res.session, res.refId, res.responsecode, res.errorstring } +func (self *SessionStoreChan) List(user, password string, trusted bool) (map[string]string, int, string) { + res_ch := make(chan listSessionsResponse) + req := listSessionsRequest{} + req.user = user + req.password = password + req.trusted = trusted + req.response = res_ch + self.listChan <- req + + res := <-res_ch + return res.sessions, res.responsecode, res.errorstring +} + func (self *SessionStoreChan) Remove(user, id string) (int, string) { res_ch := make(chan removeSessionResponse) req := removeSessionRequest{} @@ -219,6 +273,7 @@ func (self *SessionStore) GetInterface() *SessionStoreChan { ch := &SessionStoreChan{} ch.newChan = self.newChan ch.getChan = self.getChan + ch.listChan = self.listChan ch.removeChan = self.removeChan return ch } @@ -230,6 +285,7 @@ func (self *SessionStore) Cleanup() { close(self.done) close(self.newChan) close(self.getChan) + close(self.listChan) close(self.removeChan) } @@ -241,6 +297,7 @@ func NewSessionStore(conf *Config) (store *SessionStore, err error) { store.store = make(map[string]map[string]*SessionStoreElement) store.newChan = make(chan newSessionRequest, 10) store.getChan = make(chan getSessionRequest, 10) + store.listChan = make(chan listSessionsRequest, 10) store.removeChan = make(chan removeSessionRequest, 10) go store.dispatchRequests() -- cgit v0.10.2