2021-09-08 18:04:56 +00:00
|
|
|
package application
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-09-27 18:49:00 +00:00
|
|
|
"net/url"
|
2021-09-08 18:04:56 +00:00
|
|
|
|
|
|
|
"goauthentik.io/api"
|
2021-09-09 08:56:20 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2/constants"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *Application) configureForward() error {
|
|
|
|
a.mux.HandleFunc("/akprox/auth", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if _, ok := r.URL.Query()["traefik"]; ok {
|
|
|
|
a.forwardHandleTraefik(rw, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.forwardHandleNginx(rw, r)
|
|
|
|
})
|
|
|
|
a.mux.HandleFunc("/akprox/auth/traefik", a.forwardHandleTraefik)
|
|
|
|
a.mux.HandleFunc("/akprox/auth/nginx", a.forwardHandleNginx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) forwardHandleTraefik(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
claims, err := a.getClaims(r)
|
|
|
|
if claims != nil && err == nil {
|
2021-12-01 19:05:56 +00:00
|
|
|
a.addHeaders(rw.Header(), claims)
|
2021-12-02 09:01:54 +00:00
|
|
|
rw.Header().Set("User-Agent", r.Header.Get("User-Agent"))
|
2021-12-01 19:05:56 +00:00
|
|
|
a.log.WithField("headers", rw.Header()).Trace("headers written to forward_auth")
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
} else if claims == nil && a.IsAllowlisted(r) {
|
|
|
|
a.log.Trace("path can be accessed without authentication")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
host := ""
|
2021-09-10 10:43:57 +00:00
|
|
|
s, _ := a.sessions.Get(r, constants.SeesionName)
|
2021-09-08 18:04:56 +00:00
|
|
|
// Optional suffix, which is appended to the URL
|
|
|
|
if *a.proxyConfig.Mode == api.PROXYMODE_FORWARD_SINGLE {
|
|
|
|
host = web.GetHost(r)
|
|
|
|
} else if *a.proxyConfig.Mode == api.PROXYMODE_FORWARD_DOMAIN {
|
2021-09-27 18:49:00 +00:00
|
|
|
eh, _ := url.Parse(a.proxyConfig.ExternalHost)
|
|
|
|
host = eh.Host
|
2021-09-10 10:43:57 +00:00
|
|
|
}
|
|
|
|
// set the redirect flag to the current URL we have, since we redirect
|
|
|
|
// to a (possibly) different domain, but we want to be redirected back
|
|
|
|
// to the application
|
|
|
|
// see https://doc.traefik.io/traefik/middlewares/forwardauth/
|
|
|
|
// X-Forwarded-Uri is only the path, so we need to build the entire URL
|
2021-12-21 18:16:01 +00:00
|
|
|
s.Values[constants.SessionRedirect] = a.getTraefikForwardUrl(r).String()
|
2021-12-20 21:21:53 +00:00
|
|
|
if r.Header.Get("X-Forwarded-Uri") == "/akprox/start" {
|
|
|
|
a.log.Info("Detected potential redirect loop")
|
|
|
|
if val, ok := s.Values[constants.SessionLoopDetection]; !ok {
|
|
|
|
s.Values[constants.SessionLoopDetection] = 1
|
|
|
|
} else {
|
|
|
|
s.Values[constants.SessionLoopDetection] = val.(int) + 1
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 10:43:57 +00:00
|
|
|
err = s.Save(r, rw)
|
|
|
|
if err != nil {
|
|
|
|
a.log.WithError(err).Warning("failed to save session before redirect")
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2021-12-20 21:21:53 +00:00
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
proto := r.Header.Get("X-Forwarded-Proto")
|
|
|
|
if proto != "" {
|
|
|
|
proto = proto + ":"
|
|
|
|
}
|
2021-09-09 08:56:20 +00:00
|
|
|
rdFinal := fmt.Sprintf("%s//%s%s", proto, host, "/akprox/start")
|
2021-09-08 18:04:56 +00:00
|
|
|
a.log.WithField("url", rdFinal).Debug("Redirecting to login")
|
|
|
|
http.Redirect(rw, r, rdFinal, http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) forwardHandleNginx(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
claims, err := a.getClaims(r)
|
|
|
|
if claims != nil && err == nil {
|
2021-12-01 19:05:56 +00:00
|
|
|
a.addHeaders(rw.Header(), claims)
|
2021-12-02 09:01:54 +00:00
|
|
|
rw.Header().Set("User-Agent", r.Header.Get("User-Agent"))
|
2021-09-09 08:56:20 +00:00
|
|
|
rw.WriteHeader(200)
|
2021-12-01 19:05:56 +00:00
|
|
|
a.log.WithField("headers", rw.Header()).Trace("headers written to forward_auth")
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
} else if claims == nil && a.IsAllowlisted(r) {
|
|
|
|
a.log.Trace("path can be accessed without authentication")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(rw, "unauthorized request", http.StatusUnauthorized)
|
|
|
|
}
|