2021-05-02 23:49:16 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2021-09-04 12:50:29 +01:00
|
|
|
"fmt"
|
2021-05-02 23:49:16 +01:00
|
|
|
"net/http"
|
|
|
|
|
2021-09-04 12:50:29 +01:00
|
|
|
"github.com/gorilla/mux"
|
2021-05-02 23:49:16 +01:00
|
|
|
"goauthentik.io/internal/config"
|
2021-05-18 09:15:52 +01:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-05-02 23:49:16 +01:00
|
|
|
staticWeb "goauthentik.io/web"
|
2021-07-13 10:06:51 +01:00
|
|
|
staticDocs "goauthentik.io/website"
|
2021-05-02 23:49:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ws *WebServer) configureStatic() {
|
2021-05-18 09:15:52 +01:00
|
|
|
statRouter := ws.lh.NewRoute().Subrouter()
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter := statRouter.NewRoute().Subrouter()
|
|
|
|
indexLessRouter.Use(disableIndex)
|
2021-07-03 20:43:37 +01:00
|
|
|
// Media files, always local
|
|
|
|
fs := http.FileServer(http.Dir(config.G.Paths.Media))
|
2021-09-04 12:29:44 +01:00
|
|
|
var distHandler http.Handler
|
2021-09-04 12:50:29 +01:00
|
|
|
var distFs http.Handler
|
2021-09-04 12:29:44 +01:00
|
|
|
var authentikHandler http.Handler
|
|
|
|
var helpHandler http.Handler
|
2021-06-21 20:21:35 +01:00
|
|
|
if config.G.Debug || config.G.Web.LoadLocalFiles {
|
2021-05-02 23:49:16 +01:00
|
|
|
ws.log.Debug("Using local static files")
|
2021-09-04 12:50:29 +01:00
|
|
|
distFs = http.FileServer(http.Dir("./web/dist"))
|
|
|
|
distHandler = http.StripPrefix("/static/dist/", distFs)
|
2021-09-04 12:29:44 +01:00
|
|
|
authentikHandler = http.StripPrefix("/static/authentik/", http.FileServer(http.Dir("./web/authentik")))
|
2021-11-15 15:13:02 +00:00
|
|
|
helpHandler = http.FileServer(http.Dir("./website/help/"))
|
2021-05-02 23:49:16 +01:00
|
|
|
} else {
|
2021-05-18 09:15:52 +01:00
|
|
|
statRouter.Use(ws.staticHeaderMiddleware)
|
|
|
|
ws.log.Debug("Using packaged static files with aggressive caching")
|
2021-09-04 12:50:29 +01:00
|
|
|
distFs = http.FileServer(http.FS(staticWeb.StaticDist))
|
|
|
|
distHandler = http.StripPrefix("/static", distFs)
|
2021-09-04 12:29:44 +01:00
|
|
|
authentikHandler = http.StripPrefix("/static", http.FileServer(http.FS(staticWeb.StaticAuthentik)))
|
|
|
|
helpHandler = http.FileServer(http.FS(staticDocs.Help))
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter.PathPrefix("/static/dist/").Handler(distHandler)
|
|
|
|
indexLessRouter.PathPrefix("/static/authentik/").Handler(authentikHandler)
|
2021-09-23 08:57:24 +01:00
|
|
|
|
2021-09-04 12:50:29 +01:00
|
|
|
// Prevent font-loading issues on safari, which loads fonts relatively to the URL the browser is on
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter.PathPrefix("/if/flow/{flow_slug}/assets").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2021-09-04 12:50:29 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2021-09-23 08:57:24 +01:00
|
|
|
disableIndex(http.StripPrefix(fmt.Sprintf("/if/flow/%s", vars["flow_slug"]), distFs)).ServeHTTP(rw, r)
|
2021-09-04 12:50:29 +01:00
|
|
|
})
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter.PathPrefix("/if/admin/assets").Handler(http.StripPrefix("/if/admin", distFs))
|
|
|
|
indexLessRouter.PathPrefix("/if/user/assets").Handler(http.StripPrefix("/if/user", distFs))
|
2021-09-04 12:29:44 +01:00
|
|
|
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter.PathPrefix("/media/").Handler(http.StripPrefix("/media", fs))
|
2021-09-04 12:29:44 +01:00
|
|
|
|
2021-11-15 15:13:02 +00:00
|
|
|
statRouter.PathPrefix("/if/help/").Handler(http.StripPrefix("/if/help/", helpHandler))
|
2021-09-04 12:29:44 +01:00
|
|
|
statRouter.PathPrefix("/help").Handler(http.RedirectHandler("/if/help/", http.StatusMovedPermanently))
|
|
|
|
|
2021-05-02 23:49:16 +01:00
|
|
|
ws.lh.Path("/robots.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header()["Content-Type"] = []string{"text/plain"}
|
|
|
|
rw.WriteHeader(200)
|
2021-07-18 15:12:57 +01:00
|
|
|
_, err := rw.Write(staticWeb.RobotsTxt)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to write response")
|
|
|
|
}
|
2021-05-02 23:49:16 +01:00
|
|
|
})
|
|
|
|
ws.lh.Path("/.well-known/security.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header()["Content-Type"] = []string{"text/plain"}
|
|
|
|
rw.WriteHeader(200)
|
2021-07-18 15:12:57 +01:00
|
|
|
_, err := rw.Write(staticWeb.SecurityTxt)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to write response")
|
|
|
|
}
|
2021-05-02 23:49:16 +01:00
|
|
|
})
|
|
|
|
}
|
2021-05-18 09:15:52 +01:00
|
|
|
|
|
|
|
func (ws *WebServer) staticHeaderMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Cache-Control", "\"public, no-transform\"")
|
|
|
|
w.Header().Set("X-authentik-version", constants.VERSION)
|
|
|
|
w.Header().Set("Vary", "X-authentik-version")
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|