2020-04-21 07:23:28 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-11-19 08:50:34 +07:00
|
|
|
"os"
|
2020-05-21 09:20:55 +07:00
|
|
|
"text/template"
|
2020-04-21 07:23:28 +07:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/logging"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var alphaNumericRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
|
|
|
|
// ServeIndex serve the index.html file with nonce generated for supporting
|
2020-04-28 21:07:20 +07:00
|
|
|
// restrictive CSP while using material-ui from the embedded virtual filesystem.
|
|
|
|
//go:generate broccoli -src ../../public_html -o public_html
|
2020-06-21 20:40:37 +07:00
|
|
|
func ServeIndex(publicDir, base, rememberMe, resetPassword string) fasthttp.RequestHandler {
|
2020-04-28 21:07:20 +07:00
|
|
|
f, err := br.Open(publicDir + "/index.html")
|
2020-04-21 07:23:28 +07:00
|
|
|
if err != nil {
|
|
|
|
logging.Logger().Fatalf("Unable to open index.html: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
logging.Logger().Fatalf("Unable to read index.html: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("index").Parse(string(b))
|
|
|
|
if err != nil {
|
|
|
|
logging.Logger().Fatalf("Unable to parse index.html template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
nonce := utils.RandomString(32, alphaNumericRunes)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-21 07:23:28 +07:00
|
|
|
ctx.SetContentType("text/html; charset=utf-8")
|
2020-11-19 08:50:34 +07:00
|
|
|
|
|
|
|
if os.Getenv("ENVIRONMENT") == dev {
|
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf("default-src 'self' 'unsafe-eval'; object-src 'none'; style-src 'self' 'nonce-%s'", nonce))
|
|
|
|
} else {
|
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf("default-src 'self'; object-src 'none'; style-src 'self' 'nonce-%s'", nonce))
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-06-21 20:40:37 +07:00
|
|
|
err := tmpl.Execute(ctx.Response.BodyWriter(), struct{ Base, CSPNonce, RememberMe, ResetPassword string }{Base: base, CSPNonce: nonce, RememberMe: rememberMe, ResetPassword: resetPassword})
|
2020-04-21 07:23:28 +07:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error("An error occurred", 503)
|
|
|
|
logging.Logger().Errorf("Unable to execute template: %v", err)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-04-21 07:23:28 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-04-22 10:33:14 +07:00
|
|
|
}
|