diff --git a/web/login.go b/web/login.go index 4ace4a8..c1956a8 100644 --- a/web/login.go +++ b/web/login.go @@ -10,6 +10,7 @@ import ( "github.com/Team254/cheesy-arena/model" "github.com/google/uuid" "net/http" + "net/url" "time" ) @@ -67,7 +68,11 @@ func (web *Web) userIsAdmin(w http.ResponseWriter, r *http.Request) bool { if session != nil && session.Username == adminUser { return true } else { - http.Redirect(w, r, "/login?redirect="+r.URL.Path, 307) + redirect := r.URL.Path + if r.URL.RawQuery != "" { + redirect += "?" + r.URL.RawQuery + } + http.Redirect(w, r, "/login?redirect="+url.QueryEscape(redirect), 307) return false } } diff --git a/web/login_test.go b/web/login_test.go index c9704fe..f344cf9 100644 --- a/web/login_test.go +++ b/web/login_test.go @@ -13,32 +13,35 @@ func TestLoginDisplay(t *testing.T) { web.arena.EventSettings.AdminPassword = "admin" // Check that hitting a protected page redirects to the login. - recorder := web.getHttpResponse("/match_play") + recorder := web.getHttpResponse("/match_play?p1=v1&p2=v2") assert.Equal(t, 307, recorder.Code) - assert.Equal(t, "/login?redirect=/match_play", recorder.Header().Get("Location")) + assert.Equal(t, "/login?redirect=%2Fmatch_play%3Fp1%3Dv1%26p2%3Dv2", recorder.Header().Get("Location")) - recorder = web.getHttpResponse("/login?redirect=/match_play") + recorder = web.getHttpResponse("/login?redirect=%2Fmatch_play%3Fp1%3Dv1%26p2%3Dv2") assert.Equal(t, 200, recorder.Code) assert.Contains(t, recorder.Body.String(), "Log In - Untitled Event - Cheesy Arena") // Check logging in with the wrong username and right password. - recorder = web.postHttpResponse("/login?redirect=/match_play", "username=blorpy&password=reader") + recorder = web.postHttpResponse("/login?redirect=%2Fmatch_play%3Fp1%3Dv1%26p2%3Dv2", + "username=blorpy&password=reader") assert.Equal(t, 200, recorder.Code) assert.Contains(t, recorder.Body.String(), "Invalid login credentials.") // Check logging in with the right username and wrong password. - recorder = web.postHttpResponse("/login?redirect=/match_play", "username=admin&password=blorpy") + recorder = web.postHttpResponse("/login?redirect=%2Fmatch_play%3Fp1%3Dv1%26p2%3Dv2", + "username=admin&password=blorpy") assert.Equal(t, 200, recorder.Code) assert.Contains(t, recorder.Body.String(), "Invalid login credentials.") // Check logging in with the right username and password. - recorder = web.postHttpResponse("/login?redirect=/match_play", "username=admin&password=admin") + recorder = web.postHttpResponse("/login?redirect=%2Fmatch_play%3Fp1%3Dv1%26p2%3Dv2", + "username=admin&password=admin") assert.Equal(t, 303, recorder.Code) - assert.Equal(t, "/match_play", recorder.Header().Get("Location")) + assert.Equal(t, "/match_play?p1=v1&p2=v2", recorder.Header().Get("Location")) cookie := recorder.Header().Get("Set-Cookie") assert.Contains(t, cookie, "session_token=") // Check that hitting the reader-level protected page works now. - recorder = web.getHttpResponseWithHeaders("/match_play", map[string]string{"Cookie": cookie}) + recorder = web.getHttpResponseWithHeaders("/match_play?p1=v1&p2=v2", map[string]string{"Cookie": cookie}) assert.Equal(t, 200, recorder.Code) }