2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-09-10 14:19:29 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2021-09-08 18:04:56 +00:00
|
|
|
"strconv"
|
2022-01-19 18:15:57 +00:00
|
|
|
"strings"
|
2021-09-08 18:04:56 +00:00
|
|
|
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2/constants"
|
|
|
|
)
|
|
|
|
|
2022-09-22 08:10:29 +00:00
|
|
|
func urlPathSet(originalUrl string, newPath string) string {
|
|
|
|
u, err := url.Parse(originalUrl)
|
|
|
|
if err != nil {
|
|
|
|
return originalUrl
|
|
|
|
}
|
|
|
|
u.Path = newPath
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:19:29 +00:00
|
|
|
func urlJoin(originalUrl string, newPath string) string {
|
|
|
|
u, err := url.Parse(originalUrl)
|
|
|
|
if err != nil {
|
|
|
|
return originalUrl
|
|
|
|
}
|
|
|
|
u.Path = path.Join(u.Path, newPath)
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
func (a *Application) redirectToStart(rw http.ResponseWriter, r *http.Request) {
|
2022-05-20 08:10:26 +00:00
|
|
|
s, err := a.sessions.Get(r, constants.SessionName)
|
2022-02-17 19:50:47 +00:00
|
|
|
if err != nil {
|
2022-01-19 18:15:57 +00:00
|
|
|
a.log.WithError(err).Warning("failed to decode session")
|
|
|
|
}
|
2023-01-17 17:56:48 +00:00
|
|
|
if r.Header.Get(constants.HeaderAuthorization) != "" && *a.proxyConfig.InterceptHeaderAuth {
|
2023-01-14 21:20:52 +00:00
|
|
|
rw.WriteHeader(401)
|
2023-01-14 21:18:22 +00:00
|
|
|
er := a.errorTemplates.Execute(rw, ErrorPageData{
|
|
|
|
Title: "Unauthenticated",
|
2023-01-17 17:56:48 +00:00
|
|
|
Message: "Due to 'Receive header authentication' being set, no redirect is performed.",
|
2023-01-14 21:18:22 +00:00
|
|
|
ProxyPrefix: "/outpost.goauthentik.io",
|
|
|
|
})
|
|
|
|
if er != nil {
|
|
|
|
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 08:10:29 +00:00
|
|
|
|
|
|
|
redirectUrl := urlPathSet(a.proxyConfig.ExternalHost, r.URL.Path)
|
|
|
|
|
2022-01-19 18:15:57 +00:00
|
|
|
if a.Mode() == api.PROXYMODE_FORWARD_DOMAIN {
|
|
|
|
dom := strings.TrimPrefix(*a.proxyConfig.CookieDomain, ".")
|
|
|
|
// In forward_domain we only check that the current URL's host
|
|
|
|
// ends with the cookie domain (remove the leading period if set)
|
|
|
|
if !strings.HasSuffix(r.URL.Hostname(), dom) {
|
|
|
|
a.log.WithField("url", r.URL.String()).WithField("cd", dom).Warning("Invalid redirect found")
|
2022-01-25 09:57:53 +00:00
|
|
|
redirectUrl = a.proxyConfig.ExternalHost
|
2022-01-19 18:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 21:25:47 +00:00
|
|
|
if _, redirectSet := s.Values[constants.SessionRedirect]; !redirectSet {
|
|
|
|
s.Values[constants.SessionRedirect] = redirectUrl
|
|
|
|
err = s.Save(r, rw)
|
|
|
|
if err != nil {
|
|
|
|
a.log.WithError(err).Warning("failed to save session before redirect")
|
|
|
|
}
|
2022-01-19 18:15:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:32:22 +00:00
|
|
|
urlArgs := url.Values{
|
2022-10-20 19:27:34 +00:00
|
|
|
redirectParam: []string{redirectUrl},
|
2022-02-18 09:32:22 +00:00
|
|
|
}
|
2022-02-08 19:25:38 +00:00
|
|
|
authUrl := urlJoin(a.proxyConfig.ExternalHost, "/outpost.goauthentik.io/start")
|
2022-02-18 09:32:22 +00:00
|
|
|
http.Redirect(rw, r, authUrl+"?"+urlArgs.Encode(), http.StatusFound)
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 19:27:34 +00:00
|
|
|
func (a *Application) redirect(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
redirect := a.proxyConfig.ExternalHost
|
|
|
|
rd, ok := a.checkRedirectParam(r)
|
|
|
|
if ok {
|
|
|
|
redirect = rd
|
|
|
|
}
|
|
|
|
s, _ := a.sessions.Get(r, constants.SessionName)
|
|
|
|
redirectR, ok := s.Values[constants.SessionRedirect]
|
|
|
|
if ok {
|
|
|
|
redirect = redirectR.(string)
|
|
|
|
}
|
|
|
|
a.log.WithField("redirect", redirect).Trace("final redirect")
|
|
|
|
http.Redirect(rw, r, redirect, http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
// toString Generic to string function, currently supports actual strings and integers
|
|
|
|
func toString(in interface{}) string {
|
|
|
|
switch v := in.(type) {
|
|
|
|
case string:
|
|
|
|
return v
|
|
|
|
case *string:
|
|
|
|
return *v
|
|
|
|
case int:
|
|
|
|
return strconv.Itoa(v)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-12-02 14:17:32 +00:00
|
|
|
|
|
|
|
func contains(s []string, e string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-01-27 17:14:02 +00:00
|
|
|
|
|
|
|
func cleanseHeaders(headers http.Header) map[string]string {
|
|
|
|
h := make(map[string]string)
|
|
|
|
for hk, hv := range headers {
|
|
|
|
if len(hv) > 0 {
|
|
|
|
h[hk] = hv[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|