Skip to content

Commit 8d22493

Browse files
author
Jesse
committed
Merge branch 'smartSessionCache' into develop
2 parents 5deacc7 + 5f4f675 commit 8d22493

File tree

24 files changed

+1753
-464
lines changed

24 files changed

+1753
-464
lines changed

app/controllers/app.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package controllers
22

33
import (
44
"encoding/json"
5+
"net/url"
56

67
"github.com/Emyrk/LendingBot/src/core"
78
"github.com/Emyrk/LendingBot/src/core/email"
89
"github.com/revel/revel"
9-
"net/url"
1010

1111
// Init logger
1212
_ "github.com/Emyrk/LendingBot/src/log"
@@ -121,9 +121,12 @@ func (c App) Login() revel.Result {
121121

122122
c.Session[SESSION_EMAIL] = email
123123

124-
SetCacheEmail(c.Session.ID(), email)
125-
126-
c.SetCookie(GetTimeoutCookie())
124+
httpCookie, err := SetCacheEmail(c.Session.ID(), c.ClientIP, email)
125+
if err != nil {
126+
llog.Errorf("Error setting email cache: %s", err.Error())
127+
} else {
128+
c.SetCookie(httpCookie)
129+
}
127130

128131
AppPageHitLogin.Inc()
129132

@@ -164,12 +167,17 @@ func (c App) Register() revel.Result {
164167

165168
c.Session[SESSION_EMAIL] = e
166169

167-
SetCacheEmail(c.Session.ID(), e)
168-
169170
u, err := state.FetchUser(e)
170171
if err != nil {
171172
llog.Errorf("Error fetching new user: %s", err)
172173
} else {
174+
httpCookie, err := SetCacheEmail(c.Session.ID(), c.ClientIP, u.Username)
175+
if err != nil {
176+
llog.Errorf("Error setting email cache: %s", err.Error())
177+
} else {
178+
c.SetCookie(httpCookie)
179+
}
180+
173181
link := MakeURL("verifyemail/" + url.QueryEscape(u.Username) + "/" + url.QueryEscape(u.VerifyString))
174182

175183
emailRequest := email.NewHTMLRequest(email.SMTP_EMAIL_NO_REPLY, []string{
@@ -297,7 +305,8 @@ func (c App) ValidAuth() revel.Result {
297305

298306
//called before any auth required function
299307
func (c App) AppAuthUser() revel.Result {
300-
if !ValidCacheEmail(c.Session.ID(), c.Session[SESSION_EMAIL]) {
308+
email := c.Session[SESSION_EMAIL]
309+
if email != "" && !ValidCacheEmail(c.Session.ID(), c.ClientIP, email) {
301310
c.Session[SESSION_EMAIL] = ""
302311
}
303312

app/controllers/appAdmin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@ func (s AppAdmin) ConductAudit() revel.Result {
6969
func (s AppAdmin) AuthUserAdmin() revel.Result {
7070
llog := appAdminLog.WithField("method", "AuthUserAdmin")
7171

72-
if !ValidCacheEmail(s.Session.ID(), s.Session[SESSION_EMAIL]) {
73-
llog.Warningf("Warning invalid cache: [%s] sessionId:[%s]\n", s.Session[SESSION_EMAIL], s.Session.ID())
72+
if !ValidCacheEmail(s.Session.ID(), s.ClientIP, s.Session[SESSION_EMAIL]) {
73+
llog.Warningf("Warning invalid cache: email[%s] sessionId:[%s] url[%s]", s.Session[SESSION_EMAIL], s.Session.ID(), s.Request.URL)
7474
s.Session[SESSION_EMAIL] = ""
7575
return s.Redirect(App.Index)
7676
}
7777

78-
err := SetCacheEmail(s.Session.ID(), s.Session[SESSION_EMAIL])
78+
httpCookie, err := SetCacheEmail(s.Session.ID(), s.ClientIP, s.Session[SESSION_EMAIL])
7979
if err != nil {
80-
llog.Warningf("Warning failed to set cache: [%s] and error: %s\n", s.Session.ID(), err.Error())
80+
llog.Warningf("Warning failed to set cache: email[%s] sessionId:[%s] url[%s] and error: %s", s.Session[SESSION_EMAIL], s.Session.ID(), s.Request.URL, err.Error())
8181
s.Session[SESSION_EMAIL] = ""
8282
return s.Redirect(App.Index)
83+
} else {
84+
s.SetCookie(httpCookie)
8385
}
8486

8587
if !state.HasUserPrivilege(s.Session[SESSION_EMAIL], userdb.Admin) {
@@ -89,8 +91,6 @@ func (s AppAdmin) AuthUserAdmin() revel.Result {
8991
//do not cache auth pages yet
9092
s.Response.Out.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
9193

92-
s.SetCookie(GetTimeoutCookie())
93-
9494
return nil
9595
}
9696

app/controllers/appAuthRequired.go

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"fmt"
66
"io"
77
"net/url"
8+
"strconv"
9+
"strings"
10+
"time"
811

912
"github.com/Emyrk/LendingBot/balancer"
1013
"github.com/Emyrk/LendingBot/src/core/email"
@@ -16,6 +19,8 @@ import (
1619
var _ = userdb.SaltLength
1720
var SkipAuth = false
1821

22+
var ignoredRoutes = map[string]bool{"/logout": true, "/dashboard/getactivitylog": true}
23+
1924
var appAuthrequiredLog = log.WithFields(log.Fields{
2025
"package": "controllers",
2126
"file": "appAuthrequiredLog",
@@ -88,7 +93,12 @@ func (r AppAuthRequired) Dashboard() revel.Result {
8893
}
8994

9095
func (r AppAuthRequired) Logout() revel.Result {
91-
DeleteCacheToken(r.Session.ID())
96+
llog := appAuthrequiredLog.WithField("method", "Logout")
97+
if err := DeleteCacheToken(r.Session.ID(), r.ClientIP, r.Session[SESSION_EMAIL]); err != nil {
98+
llog.Error("Error logging user[%s] out: %s", r.Session[SESSION_EMAIL], err.Error())
99+
r.Response.Status = 500
100+
}
101+
delete(r.Session, SESSION_EMAIL)
92102
AppPageHitInfoLogout.Inc()
93103
return r.Redirect(App.Index)
94104
}
@@ -103,6 +113,54 @@ func (r AppAuthRequired) InfoDashboard() revel.Result {
103113
return r.RenderTemplate("AppAuthRequired/InfoDashboard.html")
104114
}
105115

116+
func (r AppAuthRequired) ChangeExpiry() revel.Result {
117+
llog := appAuthrequiredLog.WithField("method", "ChangeExpiry")
118+
119+
data := make(map[string]interface{})
120+
121+
sesExp, err := strconv.Atoi(r.Params.Form.Get("sesexp"))
122+
if err != nil {
123+
llog.Errorf("Error parsing int user[%s] expiration: %s", r.Session[SESSION_EMAIL], r.Params.Form.Get("sesexp"))
124+
data[JSON_ERROR] = "Internal error. Please contact: [email protected]"
125+
r.Response.Status = 500
126+
return r.RenderJSON(data)
127+
}
128+
129+
err = state.SetUserExpiry(r.Session[SESSION_EMAIL], time.Duration(sesExp)*time.Millisecond)
130+
if err != nil {
131+
llog.Errorf("Error setting user[%s] exp: %s", r.Session[SESSION_EMAIL], err.Error())
132+
data[JSON_ERROR] = "Internal error. Please contact: [email protected]"
133+
r.Response.Status = 500
134+
return r.RenderJSON(data)
135+
}
136+
137+
err = SetCacheDurEnd(r.Session[SESSION_EMAIL], time.Duration(sesExp)*time.Millisecond)
138+
if err != nil {
139+
llog.Errorf("Error setting user[%s] cache session exp: %s", r.Session[SESSION_EMAIL], err.Error())
140+
data[JSON_ERROR] = "Internal error. Please contact: [email protected]"
141+
r.Response.Status = 500
142+
return r.RenderJSON(data)
143+
}
144+
r.SetCookie(GetTimeoutCookie(time.Duration(sesExp) * time.Millisecond))
145+
return r.RenderJSON(data)
146+
}
147+
148+
func (r AppAuthRequired) GetExpiry() revel.Result {
149+
llog := appAuthrequiredLog.WithField("method", "GetExpiry")
150+
151+
data := make(map[string]interface{})
152+
153+
dur, err := GetCacheDur(r.Session[SESSION_EMAIL])
154+
if err != nil {
155+
llog.Errorf("Error getting user[%s] exp: %s", r.Session[SESSION_EMAIL], err.Error())
156+
data[JSON_ERROR] = "Internal error. Please contact: [email protected]"
157+
r.Response.Status = 500
158+
return r.RenderJSON(data)
159+
}
160+
data["sesexp"] = *dur / time.Millisecond
161+
return r.RenderJSON(data)
162+
}
163+
106164
func (r AppAuthRequired) Enable2FA() revel.Result {
107165
llog := appAuthrequiredLog.WithField("method", "Enable2FA")
108166

@@ -179,23 +237,23 @@ func (r AppAuthRequired) SettingsDashboardUser() revel.Result {
179237
r.ViewArgs["verified"] = fmt.Sprintf("%t", u.Verified)
180238
r.ViewArgs["has2FA"] = fmt.Sprintf("%t", u.Has2FA)
181239
r.ViewArgs["enabled2FA"] = fmt.Sprintf("%t", u.Enabled2FA)
240+
r.ViewArgs["minSessionTime"] = fmt.Sprintf("%d", CACHE_TIME_USER_SESSION_MIN/time.Minute)
241+
r.ViewArgs["maxSessionTime"] = fmt.Sprintf("%d", CACHE_TIME_USER_SESSION_MAX/time.Hour*60)
242+
r.ViewArgs["currentSessionTime"] = fmt.Sprintf("%d", u.SessionExpiryTime/time.Minute)
182243

183-
if u.PoloniexKeys.APIKeyEmpty() {
184-
r.ViewArgs["poloniexKey"] = ""
185-
} else {
186-
s, err := u.PoloniexKeys.DecryptAPIKeyString(u.GetCipherKey(state.CipherKey))
187-
if err != nil {
188-
llog.Errorf("Error decrypting Api Keys String: %s\n", err.Error())
189-
s = ""
190-
}
191-
r.ViewArgs["poloniexKey"] = s
244+
uss, err := GetUserActiveSessions(r.Session[SESSION_EMAIL], r.Session.ID())
245+
if err != nil {
246+
llog.Error("Error getting user active sessions: %s", err.Error())
192247
}
193-
194-
if u.PoloniexKeys.SecretKeyEmpty() {
195-
r.ViewArgs["poloniexSecret"] = ""
196-
} else {
197-
r.ViewArgs["poloniexSecret"] = ""
248+
b, err := json.Marshal(uss)
249+
if err != nil {
250+
llog.Errorf("Error marshalling user sessions: %s", err.Error())
251+
b = []byte("[]")
198252
}
253+
if len(uss) == 0 {
254+
b = []byte("[]")
255+
}
256+
r.ViewArgs["sessions"] = string(b)
199257

200258
AppPageHitSetSettingDashUser.Inc()
201259
return r.RenderTemplate("AppAuthRequired/SettingsDashboardUser.html")
@@ -395,8 +453,31 @@ func (r AppAuthRequired) GetActivityLogs() revel.Result {
395453
return r.RenderJSON(data)
396454
}
397455

456+
func (r AppAuthRequired) DeleteSession() revel.Result {
457+
llog := appAuthrequiredLog.WithField("method", "DeleteSession")
458+
459+
data := make(map[string]interface{})
460+
//delete session
461+
if err := DeleteCacheToken(r.Params.Form.Get("sesid"), r.ClientIP, r.Session[SESSION_EMAIL]); err != nil {
462+
llog.Error("Error deleting user session: %s", err.Error())
463+
data[JSON_ERROR] = "Server error, failed to delete session. Contact support: [email protected]."
464+
r.Response.Status = 500
465+
return r.RenderJSON(data)
466+
}
467+
//get active sessions
468+
uss, err := GetUserActiveSessions(r.Session[SESSION_EMAIL], r.Session.ID())
469+
if err != nil {
470+
llog.Error("Error getting user active sessions after delete: %s", err.Error())
471+
data[JSON_ERROR] = "Server error, failed to delete session. Contact support: [email protected]."
472+
r.Response.Status = 500
473+
return r.RenderJSON(data)
474+
}
475+
data["ses"] = uss
476+
return r.RenderJSON(data)
477+
}
478+
398479
func (r AppAuthRequired) UserDashboard() revel.Result {
399-
if revel.DevMode {
480+
if revel.DevMode || strings.Contains(revel.RunMode, "dev") {
400481
return r.RenderError(&revel.Error{
401482
Title: "404 Error.",
402483
Description: "Looks like you are lost.",
@@ -409,25 +490,32 @@ func (r AppAuthRequired) UserDashboard() revel.Result {
409490
func (r AppAuthRequired) AuthUser() revel.Result {
410491
llog := appAuthrequiredLog.WithField("method", "AuthUser")
411492

412-
if !ValidCacheEmail(r.Session.ID(), r.Session[SESSION_EMAIL]) {
413-
llog.Warningf("Warning invalid cache: [%s] sessionId:[%s]\n", r.Session[SESSION_EMAIL], r.Session.ID())
493+
if !ValidCacheEmail(r.Session.ID(), r.ClientIP, r.Session[SESSION_EMAIL]) {
494+
llog.Warningf("Warning invalid cache: email[%s] sessionId:[%s] url[%s]", r.Session[SESSION_EMAIL], r.Session.ID(), r.Request.URL)
414495
r.Session[SESSION_EMAIL] = ""
415496
r.Response.Status = 403
416497
return r.RenderTemplate("errors/403.html")
417498
}
418499

419-
err := SetCacheEmail(r.Session.ID(), r.Session[SESSION_EMAIL])
500+
//must add rep
501+
if ignoredRoutes[r.Request.RequestURI] == true {
502+
return nil
503+
}
504+
505+
AppPageAuthUser.Inc()
506+
507+
httpCookie, err := SetCacheEmail(r.Session.ID(), r.ClientIP, r.Session[SESSION_EMAIL])
420508
if err != nil {
421-
llog.Warningf("Warning failed to set cache: [%s] and error: %s\n", r.Session.ID(), err.Error())
509+
llog.Warningf("Warning failed to set cache: email[%s] sessionId:[%s] url[%s] and error: %s", r.Session[SESSION_EMAIL], r.Session.ID(), r.Request.URL, err.Error())
422510
r.Session[SESSION_EMAIL] = ""
423511
r.Response.Status = 403
424512
return r.RenderTemplate("errors/403.html")
513+
} else {
514+
r.SetCookie(httpCookie)
425515
}
516+
426517
//do not cache auth pages
427518
// r.Response.Out.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
428519

429-
r.SetCookie(GetTimeoutCookie())
430-
431-
AppPageAuthUser.Inc()
432520
return nil
433521
}

app/controllers/appSysAdmin.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,21 @@ func (s AppSysAdmin) DeleteLogs() revel.Result {
185185
func (s AppSysAdmin) AuthUserSysAdmin() revel.Result {
186186
llog := appSysAdminLog.WithField("method", "AuthUserSysAdmin")
187187

188-
if !ValidCacheEmail(s.Session.ID(), s.Session[SESSION_EMAIL]) {
189-
llog.Warningf("Warning has invalid cache: [%s] sessionId:[%s]\n", s.Session[SESSION_EMAIL], s.Session.ID())
188+
if !ValidCacheEmail(s.Session.ID(), s.ClientIP, s.Session[SESSION_EMAIL]) {
189+
llog.Warningf("Warning invalid cache: email[%s] sessionId:[%s] url[%s]", s.Session[SESSION_EMAIL], s.Session.ID(), s.Request.URL)
190190
s.Session[SESSION_EMAIL] = ""
191191
s.Response.Status = 403
192192
return s.RenderTemplate("errors/403.html")
193193
}
194194

195-
err := SetCacheEmail(s.Session.ID(), s.Session[SESSION_EMAIL])
195+
httpCookie, err := SetCacheEmail(s.Session.ID(), s.ClientIP, s.Session[SESSION_EMAIL])
196196
if err != nil {
197-
llog.Warningf("Warning failed to set cache: [%s] and error: %s\n", s.Session.ID(), err.Error())
197+
llog.Warningf("Warning failed to set cache: email[%s] sessionId:[%s] url[%s] and error: %s", s.Session[SESSION_EMAIL], s.Session.ID(), s.Request.URL, err.Error())
198198
s.Session[SESSION_EMAIL] = ""
199199
s.Response.Status = 403
200200
return s.RenderTemplate("errors/403.html")
201+
} else {
202+
s.SetCookie(httpCookie)
201203
}
202204

203205
if !state.HasUserPrivilege(s.Session[SESSION_EMAIL], userdb.SysAdmin) {
@@ -208,7 +210,5 @@ func (s AppSysAdmin) AuthUserSysAdmin() revel.Result {
208210
//do not cache auth pages yet
209211
// s.Response.Out.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
210212

211-
s.SetCookie(GetTimeoutCookie())
212-
213213
return nil
214214
}

app/controllers/launch.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ func Launch() {
7474
//devEmpty mode
7575
//should be all in memory with empty data
7676

77-
state = core.NewStateWithMap()
78-
state.NewUser("[email protected]", "admin")
77+
state = core.NewStateWithMongoEmpty()
78+
ape := state.NewUser("[email protected]", "admin")
79+
if ape != nil {
80+
fmt.Println(ape)
81+
}
7982
state.UpdateUserPrivilege("[email protected]", "SysAdmin")
80-
83+
Balancer = balancer.NewBalancer(state.CipherKey, revel.Config.StringDefault("database.uri", "mongodb://localhost:27017"), "", "")
84+
// return
8185
//to be used for unit testing/regression testing
8286
case DEV_MONGO:
8387
//mongo

0 commit comments

Comments
 (0)