2021-05-02 22:49:16 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2021-09-04 11:50:29 +00:00
|
|
|
"fmt"
|
2021-05-02 22:49:16 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-09-11 21:18:31 +00:00
|
|
|
"github.com/go-http-utils/etag"
|
2021-09-04 11:50:29 +00:00
|
|
|
"github.com/gorilla/mux"
|
2023-12-12 10:23:50 +00:00
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-05-18 08:15:52 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-11-19 09:50:56 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2021-05-02 22:49:16 +00:00
|
|
|
staticWeb "goauthentik.io/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ws *WebServer) configureStatic() {
|
2021-05-18 08:15:52 +00:00
|
|
|
statRouter := ws.lh.NewRoute().Subrouter()
|
2021-12-01 19:41:55 +00:00
|
|
|
statRouter.Use(ws.staticHeaderMiddleware)
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter := statRouter.NewRoute().Subrouter()
|
2021-11-19 09:50:56 +00:00
|
|
|
indexLessRouter.Use(web.DisableIndex)
|
2021-12-01 19:35:28 +00:00
|
|
|
distFs := http.FileServer(http.Dir("./web/dist"))
|
|
|
|
distHandler := http.StripPrefix("/static/dist/", distFs)
|
|
|
|
authentikHandler := http.StripPrefix("/static/authentik/", http.FileServer(http.Dir("./web/authentik")))
|
|
|
|
helpHandler := http.FileServer(http.Dir("./website/help/"))
|
2021-11-15 15:13:02 +00:00
|
|
|
indexLessRouter.PathPrefix("/static/dist/").Handler(distHandler)
|
|
|
|
indexLessRouter.PathPrefix("/static/authentik/").Handler(authentikHandler)
|
2021-09-23 07:57:24 +00:00
|
|
|
|
2021-09-04 11:50:29 +00: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 11:50:29 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2021-11-19 09:50:56 +00:00
|
|
|
web.DisableIndex(http.StripPrefix(fmt.Sprintf("/if/flow/%s", vars["flow_slug"]), distFs)).ServeHTTP(rw, r)
|
2021-09-04 11:50:29 +00: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))
|
2023-12-30 20:33:14 +00:00
|
|
|
indexLessRouter.PathPrefix("/if/rac/{app_slug}/assets").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
web.DisableIndex(http.StripPrefix(fmt.Sprintf("/if/rac/%s", vars["app_slug"]), distFs)).ServeHTTP(rw, r)
|
|
|
|
})
|
2021-09-04 11:29:44 +00:00
|
|
|
|
2023-12-12 10:23:50 +00:00
|
|
|
// Media files, if backend is file
|
|
|
|
if config.Get().Storage.Media.Backend == "file" {
|
|
|
|
fsMedia := http.FileServer(http.Dir(config.Get().Storage.Media.File.Path))
|
|
|
|
indexLessRouter.PathPrefix("/media/").Handler(http.StripPrefix("/media", fsMedia))
|
|
|
|
}
|
2021-09-04 11:29:44 +00:00
|
|
|
|
2021-11-15 15:13:02 +00:00
|
|
|
statRouter.PathPrefix("/if/help/").Handler(http.StripPrefix("/if/help/", helpHandler))
|
2021-09-04 11:29:44 +00:00
|
|
|
statRouter.PathPrefix("/help").Handler(http.RedirectHandler("/if/help/", http.StatusMovedPermanently))
|
|
|
|
|
2021-05-02 22:49:16 +00: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 14:12:57 +00:00
|
|
|
_, err := rw.Write(staticWeb.RobotsTxt)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to write response")
|
|
|
|
}
|
2021-05-02 22:49:16 +00: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 14:12:57 +00:00
|
|
|
_, err := rw.Write(staticWeb.SecurityTxt)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to write response")
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-18 08:15:52 +00:00
|
|
|
|
|
|
|
func (ws *WebServer) staticHeaderMiddleware(h http.Handler) http.Handler {
|
2022-09-11 21:18:31 +00:00
|
|
|
etagHandler := etag.Handler(h, false)
|
2021-05-18 08:15:52 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-01-25 20:15:22 +00:00
|
|
|
w.Header().Set("Cache-Control", "public, no-transform")
|
2021-05-18 08:15:52 +00:00
|
|
|
w.Header().Set("X-authentik-version", constants.VERSION)
|
2022-09-11 21:18:31 +00:00
|
|
|
w.Header().Set("Vary", "X-authentik-version, Etag")
|
|
|
|
etagHandler.ServeHTTP(w, r)
|
2021-05-18 08:15:52 +00:00
|
|
|
})
|
|
|
|
}
|