1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-02-21 19:48:46 +02:00

78 lines
2.2 KiB
Go
Raw Normal View History

package pagewriter
2012-12-10 20:59:23 -05:00
import (
// Import embed to allow importing default page templates
_ "embed"
2021-02-06 18:56:31 +00:00
"fmt"
2012-12-10 20:59:23 -05:00
"html/template"
2021-02-06 18:56:31 +00:00
"os"
"path/filepath"
"strings"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
2012-12-10 20:59:23 -05:00
)
2021-02-06 18:56:31 +00:00
const (
errorTemplateName = "error.html"
signInTemplateName = "sign_in.html"
)
2015-03-17 18:06:06 -04:00
//go:embed error.html
var defaultErrorTemplate string
2021-02-06 18:56:31 +00:00
//go:embed sign_in.html
var defaultSignInTemplate string
2021-02-06 18:56:31 +00:00
// loadTemplates adds the Sign In and Error templates from the custom template
2021-02-06 18:56:31 +00:00
// directory, or uses the defaults if they do not exist or the custom directory
// is not provided.
func loadTemplates(customDir string) (*template.Template, error) {
2021-02-06 18:56:31 +00:00
t := template.New("").Funcs(template.FuncMap{
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
})
var err error
t, err = addTemplate(t, customDir, signInTemplateName, defaultSignInTemplate)
if err != nil {
return nil, fmt.Errorf("could not add Sign In template: %v", err)
}
t, err = addTemplate(t, customDir, errorTemplateName, defaultErrorTemplate)
if err != nil {
return nil, fmt.Errorf("could not add Error template: %v", err)
}
return t, nil
}
// addTemplate will add the template from the custom directory if provided,
// else it will add the default template.
func addTemplate(t *template.Template, customDir, fileName, defaultTemplate string) (*template.Template, error) {
filePath := filepath.Join(customDir, fileName)
if customDir != "" && isFile(filePath) {
t, err := t.ParseFiles(filePath)
if err != nil {
return nil, fmt.Errorf("failed to parse template %s: %v", filePath, err)
}
return t, nil
}
t, err := t.Parse(defaultTemplate)
if err != nil {
// This should not happen.
// Default templates should be tested and so should never fail to parse.
logger.Panic("Could not parse defaultTemplate: ", err)
}
return t, nil
}
// isFile checks if the file exists and checks whether it is a regular file.
// If either of these fail then it cannot be used as a template file.
func isFile(fileName string) bool {
info, err := os.Stat(fileName)
2012-12-10 20:59:23 -05:00
if err != nil {
2021-02-06 18:56:31 +00:00
logger.Errorf("Could not load file %s: %v, will use default template", fileName, err)
return false
2012-12-10 20:59:23 -05:00
}
2021-02-06 18:56:31 +00:00
return info.Mode().IsRegular()
2012-12-10 20:59:23 -05:00
}