2022-07-18 00:56:09 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-12-23 05:06:49 +00:00
|
|
|
th "html/template"
|
2022-07-18 00:56:09 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-12-23 10:58:54 +00:00
|
|
|
"strings"
|
2022-12-23 05:06:49 +00:00
|
|
|
tt "text/template"
|
2022-07-18 00:56:09 +00:00
|
|
|
)
|
|
|
|
|
2022-12-23 10:58:54 +00:00
|
|
|
const (
|
|
|
|
envPrefix = "AUTHELIA_"
|
|
|
|
envXPrefix = "X_AUTHELIA_"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IMPORTANT: This is a copy of github.com/authelia/authelia/internal/configuration's secretSuffixes except all uppercase.
|
|
|
|
// Make sure you update these at the same time.
|
|
|
|
var envSecretSuffixes = []string{
|
|
|
|
"KEY", "SECRET", "PASSWORD", "TOKEN", "CERTIFICATE_CHAIN",
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSecretEnvKey(key string) (isSecretEnvKey bool) {
|
|
|
|
key = strings.ToUpper(key)
|
|
|
|
|
|
|
|
if !strings.HasPrefix(key, envPrefix) && !strings.HasPrefix(key, envXPrefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range envSecretSuffixes {
|
|
|
|
suffix := strings.ToUpper(s)
|
|
|
|
|
|
|
|
if strings.HasSuffix(key, suffix) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
func templateExists(path string) (exists bool) {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
func readTemplate(name, ext, category, overridePath string) (tPath string, embed bool, data []byte, err error) {
|
2022-07-18 00:56:09 +00:00
|
|
|
if overridePath != "" {
|
2022-12-23 05:06:49 +00:00
|
|
|
tPath = filepath.Join(overridePath, name+ext)
|
2022-07-18 00:56:09 +00:00
|
|
|
|
|
|
|
if templateExists(tPath) {
|
2022-08-26 21:39:20 +00:00
|
|
|
if data, err = os.ReadFile(tPath); err != nil {
|
|
|
|
return tPath, false, nil, fmt.Errorf("failed to read template override at path '%s': %w", tPath, err)
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 21:39:20 +00:00
|
|
|
return tPath, false, data, nil
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
tPath = path.Join("src", category, name+ext)
|
2022-08-26 21:39:20 +00:00
|
|
|
|
|
|
|
if data, err = embedFS.ReadFile(tPath); err != nil {
|
|
|
|
return tPath, true, nil, fmt.Errorf("failed to read embedded template '%s': %w", tPath, err)
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 21:39:20 +00:00
|
|
|
return tPath, true, data, nil
|
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
func parseTextTemplate(name, tPath string, embed bool, data []byte) (t *tt.Template, err error) {
|
2022-12-23 10:58:54 +00:00
|
|
|
if t, err = tt.New(name + extText).Funcs(FuncMap()).Parse(string(data)); err != nil {
|
2022-12-23 05:06:49 +00:00
|
|
|
if embed {
|
|
|
|
return nil, fmt.Errorf("failed to parse embedded template '%s': %w", tPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to parse template override at path '%s': %w", tPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseHTMLTemplate(name, tPath string, embed bool, data []byte) (t *th.Template, err error) {
|
2022-12-23 10:58:54 +00:00
|
|
|
if t, err = th.New(name + extHTML).Funcs(FuncMap()).Parse(string(data)); err != nil {
|
2022-08-26 21:39:20 +00:00
|
|
|
if embed {
|
|
|
|
return nil, fmt.Errorf("failed to parse embedded template '%s': %w", tPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to parse template override at path '%s': %w", tPath, err)
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
2022-08-26 21:39:20 +00:00
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
func loadEmailTemplate(name, overridePath string) (t *EmailTemplate, err error) {
|
2022-08-26 21:39:20 +00:00
|
|
|
var (
|
|
|
|
embed bool
|
2022-12-23 05:06:49 +00:00
|
|
|
tpath string
|
2022-08-26 21:39:20 +00:00
|
|
|
data []byte
|
|
|
|
)
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
t = &EmailTemplate{}
|
|
|
|
|
|
|
|
if tpath, embed, data, err = readTemplate(name, extText, TemplateCategoryNotifications, overridePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Text, err = parseTextTemplate(name, tpath, embed, data); err != nil {
|
2022-08-26 21:39:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-23 05:06:49 +00:00
|
|
|
if tpath, embed, data, err = readTemplate(name, extHTML, TemplateCategoryNotifications, overridePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.HTML, err = parseHTMLTemplate(name, tpath, embed, data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
2022-08-26 21:39:20 +00:00
|
|
|
}
|