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"
|
2023-01-03 13:11:10 +00:00
|
|
|
"reflect"
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-03 03:49:02 +00:00
|
|
|
func fileExists(path string) (exists bool) {
|
2022-07-18 00:56:09 +00:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
|
2023-01-03 03:49:02 +00:00
|
|
|
return err == nil && !info.IsDir()
|
2022-07-18 00:56:09 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-01-03 03:49:02 +00:00
|
|
|
if fileExists(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
|
|
|
}
|
2023-01-03 13:11:10 +00:00
|
|
|
|
|
|
|
func strval(v any) string {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
return v
|
|
|
|
case []byte:
|
|
|
|
return string(v)
|
|
|
|
case fmt.Stringer:
|
|
|
|
return v.String()
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strslice(v any) []string {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case []string:
|
|
|
|
return v
|
|
|
|
case []any:
|
|
|
|
b := make([]string, 0, len(v))
|
|
|
|
|
|
|
|
for _, s := range v {
|
|
|
|
if s != nil {
|
|
|
|
b = append(b, strval(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
default:
|
|
|
|
val := reflect.ValueOf(v)
|
|
|
|
switch val.Kind() {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
l := val.Len()
|
|
|
|
b := make([]string, 0, l)
|
|
|
|
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
value := val.Index(i).Interface()
|
|
|
|
if value != nil {
|
|
|
|
b = append(b, strval(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
default:
|
|
|
|
if v == nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{strval(v)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|