Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/server: fix /demologin to properly redirect to home page #98319

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/server/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (s *authenticationServer) demoLogin(w http.ResponseWriter, req *http.Reques

w.Header()["Set-Cookie"] = []string{cookie.String()}
w.Header()["Location"] = []string{"/"}
w.WriteHeader(302)
w.WriteHeader(http.StatusTemporaryRedirect)
_, _ = w.Write([]byte("you can use the UI now"))
}

Expand Down
18 changes: 17 additions & 1 deletion pkg/server/server_controller_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func (c *serverController) attemptLoginToAllTenants() http.Handler {
}
defer r.Body.Close()

redirect := false
redirectLocation := "/" // default to home page
for _, name := range tenantNames {
server, err := c.getServer(ctx, name)
if err != nil {
Expand Down Expand Up @@ -185,6 +187,16 @@ func (c *serverController) attemptLoginToAllTenants() http.Handler {
name: string(name),
setCookie: setCookieHeader,
})
// In the case of /demologin, we want to redirect to the provided location
// in the header. If we get back a cookie along with an
// http.StatusTemporaryRedirect code, be sure to transfer the response code
// along with the Location into the ResponseWriter later.
if sw.code == http.StatusTemporaryRedirect {
redirect = true
if locationHeader, ok := sw.Header()["Location"]; ok && len(locationHeader) > 0 {
redirectLocation = locationHeader[0]
}
}
}
}
// If the map has entries, the method to create the aggregated session should
Expand Down Expand Up @@ -217,7 +229,11 @@ func (c *serverController) attemptLoginToAllTenants() http.Handler {
return
}
}
w.WriteHeader(http.StatusOK)
if redirect {
http.Redirect(w, r, redirectLocation, http.StatusTemporaryRedirect)
} else {
w.WriteHeader(http.StatusOK)
}
} else {
w.WriteHeader(http.StatusUnauthorized)
}
Expand Down