2019-04-20 22:15:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-04-26 17:44:22 +00:00
|
|
|
"strings"
|
2019-04-20 22:15:36 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
func getRedirectURL(r *http.Request, fallback string) (string, error) {
|
2019-04-20 22:15:36 +00:00
|
|
|
var (
|
2019-04-21 01:22:45 +00:00
|
|
|
params url.Values
|
2019-04-26 17:44:22 +00:00
|
|
|
redirURL string
|
|
|
|
sessURL string
|
2019-04-20 22:15:36 +00:00
|
|
|
)
|
|
|
|
|
2019-04-26 17:44:22 +00:00
|
|
|
if cookieStore != nil {
|
|
|
|
sess, _ := cookieStore.Get(r, strings.Join([]string{mainCfg.Cookie.Prefix, "main"}, "-"))
|
|
|
|
if s, ok := sess.Values["go"].(string); ok {
|
|
|
|
sessURL = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
switch {
|
|
|
|
case r.URL.Query().Get("go") != "":
|
|
|
|
// We have a GET request, use "go" query param
|
|
|
|
redirURL = r.URL.Query().Get("go")
|
|
|
|
params = r.URL.Query()
|
|
|
|
|
|
|
|
case r.FormValue("go") != "":
|
|
|
|
// We have a POST request, use "go" form value
|
2019-04-20 22:15:36 +00:00
|
|
|
redirURL = r.FormValue("go")
|
|
|
|
params = url.Values{} // No need to read other form fields
|
2019-04-21 01:22:45 +00:00
|
|
|
|
2019-04-26 17:44:22 +00:00
|
|
|
case sessURL != "":
|
|
|
|
redirURL = sessURL
|
|
|
|
params = url.Values{}
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
default:
|
|
|
|
// No URL specified, use specified fallback URL
|
|
|
|
return fallback, nil
|
2019-04-20 22:15:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
// Remove the "go" parameter as it is a parameter for nginx-sso
|
2019-04-20 22:15:36 +00:00
|
|
|
params.Del("go")
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
// Parse given URL to extract attached parameters
|
2019-04-20 22:15:36 +00:00
|
|
|
pURL, err := url.Parse(redirURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to parse redirect URL")
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
// Transfer parameters from URL to params set
|
2019-04-20 22:15:36 +00:00
|
|
|
for k, vs := range pURL.Query() {
|
|
|
|
for _, v := range vs {
|
|
|
|
params.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:22:45 +00:00
|
|
|
// Re-add assembled parameters to URL
|
2019-04-20 22:15:36 +00:00
|
|
|
pURL.RawQuery = params.Encode()
|
|
|
|
|
|
|
|
return pURL.String(), nil
|
|
|
|
}
|