diff options
author | Christopher Speller <crspeller@gmail.com> | 2016-12-22 07:52:02 -0500 |
---|---|---|
committer | Joram Wilander <jwawilander@gmail.com> | 2016-12-22 07:52:02 -0500 |
commit | c434e84114ef14b7a53ad25c658aaf5156f6d949 (patch) | |
tree | d5b26a01be90e80ef29e4be6cf5c0cdfdb571b96 /api/web_conn.go | |
parent | 64a86bd32f82b8c9c943c38949f6f7e105bcd54e (diff) | |
download | chat-c434e84114ef14b7a53ad25c658aaf5156f6d949.tar.gz chat-c434e84114ef14b7a53ad25c658aaf5156f6d949.tar.bz2 chat-c434e84114ef14b7a53ad25c658aaf5156f6d949.zip |
Adding session cache directly to web-conn (#4861)
Diffstat (limited to 'api/web_conn.go')
-rw-r--r-- | api/web_conn.go | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/api/web_conn.go b/api/web_conn.go index a2b801904..3b991d449 100644 --- a/api/web_conn.go +++ b/api/web_conn.go @@ -27,6 +27,7 @@ type WebConn struct { WebSocket *websocket.Conn Send chan model.WebSocketMessage SessionToken string + SessionExpiresAt int64 UserId string T goi18n.TranslateFunc Locale string @@ -40,12 +41,13 @@ func NewWebConn(c *Context, ws *websocket.Conn) *WebConn { } return &WebConn{ - Send: make(chan model.WebSocketMessage, 256), - WebSocket: ws, - UserId: c.Session.UserId, - SessionToken: c.Session.Token, - T: c.T, - Locale: c.Locale, + Send: make(chan model.WebSocketMessage, 256), + WebSocket: ws, + UserId: c.Session.UserId, + SessionToken: c.Session.Token, + SessionExpiresAt: c.Session.ExpiresAt, + T: c.T, + Locale: c.Locale, } } @@ -144,16 +146,25 @@ func (c *WebConn) writePump() { func (webCon *WebConn) InvalidateCache() { webCon.AllChannelMembers = nil webCon.LastAllChannelMembersTime = 0 + webCon.SessionExpiresAt = 0 } func (webCon *WebConn) isAuthenticated() bool { - if webCon.SessionToken == "" { - return false - } + // Check the expiry to see if we need to check for a new session + if webCon.SessionExpiresAt < model.GetMillis() { + if webCon.SessionToken == "" { + return false + } - session := GetSession(webCon.SessionToken) - if session == nil || session.IsExpired() { - return false + session := GetSession(webCon.SessionToken) + if session == nil || session.IsExpired() { + webCon.SessionToken = "" + webCon.SessionExpiresAt = 0 + return false + } + + webCon.SessionToken = session.Token + webCon.SessionExpiresAt = session.ExpiresAt } return true |