@@ -8,8 +8,12 @@ import (
88 "time"
99)
1010
11- const timeOUT = 120
12- const sessionCOOKIENAME = "vdc.sid"
11+ const timeOut = 120
12+ const sessionCookieName = "vdc.sid"
13+
14+ const loginDialogFile = "logindialog.html"
15+ const loggedInDialogFile = "loggedindialog.html"
16+ const signUpDialogFile = "signupdialog.html"
1317
1418type session struct {
1519 SessionID string
@@ -31,9 +35,7 @@ func generateRandomString(n int) (string, error) {
3135
3236func authorize (next http.Handler ) http.Handler {
3337 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
34- // log.Printf("Sessions:\n%v\n", currentSessions)
35-
36- sessioncookie , _ := r .Cookie (sessionCOOKIENAME )
38+ sessioncookie , _ := r .Cookie (sessionCookieName )
3739 if sessioncookie == nil {
3840 http .Error (w , "Not logged in at all." , http .StatusUnauthorized )
3941 return
@@ -52,7 +54,7 @@ func authorize(next http.Handler) http.Handler {
5254 }
5355
5456 // Expire session on extra idle time
55- if time .Since (currentsession .lastActionAt ).Seconds () > timeOUT {
57+ if time .Since (currentsession .lastActionAt ).Seconds () > timeOut {
5658
5759 // Expire cookie
5860 sessioncookie .Expires = time .Now ().AddDate (- 1 , 0 , 0 )
@@ -82,27 +84,26 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
8284 password := r .PostFormValue ("password" )
8385
8486 // Validate user
85- //if username == "admin" && password == "Pass@word1" {
8687 if validateUser (username , password ) {
8788
8889 sessionid , _ := generateRandomString (12 )
8990
90- sessioncookie := http.Cookie {Name : sessionCOOKIENAME , Value : sessionid , Path : "/" , HttpOnly : true }
91+ sessioncookie := http.Cookie {Name : sessionCookieName , Value : sessionid , Path : "/" , HttpOnly : true }
9192 http .SetCookie (w , & sessioncookie )
9293
9394 sessionsLock .Lock ()
9495 currentSessions [sessionid ] = session {SessionID : sessionid , lastActionAt : time .Now ()}
9596 sessionsLock .Unlock ()
9697
97- http .ServeFile (w , r , "../public/loggedindialog.html" )
98+ http .ServeFile (w , r , loggedInDialogFile )
9899
99100 return
100101 }
101102
102103 }
103104 }
104105
105- http .ServeFile (w , r , "../public/logindialog.html" )
106+ http .ServeFile (w , r , loginDialogFile )
106107}
107108
108109func handleSignUp (w http.ResponseWriter , r * http.Request ) {
@@ -119,7 +120,7 @@ func handleSignUp(w http.ResponseWriter, r *http.Request) {
119120 }
120121 }
121122
122- http .ServeFile (w , r , "../public/signupdialog.html" )
123+ http .ServeFile (w , r , signUpDialogFile )
123124}
124125
125126func signin () http.Handler {
@@ -134,7 +135,7 @@ func signin() http.Handler {
134135
135136func signout () http.Handler {
136137 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
137- sessioncookie , _ := r .Cookie (sessionCOOKIENAME )
138+ sessioncookie , _ := r .Cookie (sessionCookieName )
138139 if sessioncookie != nil {
139140 sessionid := sessioncookie .Value
140141
@@ -143,7 +144,7 @@ func signout() http.Handler {
143144 sessionsLock .Unlock ()
144145 }
145146
146- sessioncookie = & http.Cookie {Name : sessionCOOKIENAME , Path : "/" , MaxAge : - 1 }
147+ sessioncookie = & http.Cookie {Name : sessionCookieName , Path : "/" , MaxAge : - 1 }
147148 http .SetCookie (w , sessioncookie )
148149 })
149150}
@@ -154,7 +155,7 @@ func cleanupSessions() {
154155 sessionsLock .Lock ()
155156
156157 for key , value := range currentSessions {
157- if time .Since (value .lastActionAt ) > (time .Second * timeOUT ) {
158+ if time .Since (value .lastActionAt ) > (time .Second * timeOut ) {
158159 sessionsToClean = append (sessionsToClean , key )
159160 }
160161 }
0 commit comments