From 0cc0c73676f62dd6b67436018c7714df46eb9076 Mon Sep 17 00:00:00 2001 From: Lucas Serven Date: Fri, 22 Jul 2016 13:59:34 -0700 Subject: [PATCH] backend: add absolute path to template links fixes: #502 Add a new template function called `absPath` that converts relative paths to absolute paths based on the issuerURL. The template function can be used in templates like: ````html ```` --- server/config.go | 14 ++++++++++++-- server/testutil_test.go | 2 +- static/html/ldap-login.html | 4 ++-- static/html/local-login.html | 4 ++-- static/html/login.html | 8 ++++---- static/html/register.html | 6 +++--- static/html/send-reset-password.html | 2 +- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/server/config.go b/server/config.go index 35e19a96..87372468 100644 --- a/server/config.go +++ b/server/config.go @@ -8,6 +8,7 @@ import ( "io" "net/url" "os" + "path" "path/filepath" texttemplate "text/template" "time" @@ -63,7 +64,7 @@ func (cfg *ServerConfig) Server() (*Server, error) { return nil, err } - tpl, err := getTemplates(cfg.IssuerName, cfg.IssuerLogoURL, cfg.EnableRegistration, cfg.TemplateDir) + tpl, err := getTemplates(cfg.IssuerName, cfg.IssuerURL, cfg.IssuerLogoURL, cfg.EnableRegistration, cfg.TemplateDir) if err != nil { return nil, err } @@ -281,8 +282,14 @@ func (cfg *MultiServerConfig) Configure(srv *Server) error { return nil } -func getTemplates(issuerName, issuerLogoURL string, +func getTemplates(issuerName, issuerURL, issuerLogoURL string, enableRegister bool, dir string) (*template.Template, error) { + u, err := url.Parse(issuerURL) + if err != nil { + return nil, err + } + issuerPath := u.Path + tpl := template.New("").Funcs(map[string]interface{}{ "issuerName": func() string { return issuerName @@ -293,6 +300,9 @@ func getTemplates(issuerName, issuerLogoURL string, "enableRegister": func() bool { return enableRegister }, + "absPath": func(p string) string { + return path.Join(issuerPath, p) + }, }) return tpl.ParseGlob(dir + "/*.html") diff --git a/server/testutil_test.go b/server/testutil_test.go index 0ef7bfa0..e7c07060 100644 --- a/server/testutil_test.go +++ b/server/testutil_test.go @@ -230,7 +230,7 @@ func makeTestFixturesWithOptions(options testFixtureOptions) (*testFixtures, err return nil, err } - tpl, err := getTemplates("dex", + tpl, err := getTemplates("dex", "https://coreos.com", "https://coreos.com/assets/images/brand/coreos-mark-30px.png", true, templatesLocation) if err != nil { diff --git a/static/html/ldap-login.html b/static/html/ldap-login.html index 4ea4cd65..3a80c5af 100644 --- a/static/html/ldap-login.html +++ b/static/html/ldap-login.html @@ -2,7 +2,7 @@

Log in to Your Account

-
+
LDAP
@@ -13,7 +13,7 @@ diff --git a/static/html/local-login.html b/static/html/local-login.html index 65689a5f..3e2834b6 100644 --- a/static/html/local-login.html +++ b/static/html/local-login.html @@ -2,7 +2,7 @@

Log in to Your Account

- +
@@ -12,7 +12,7 @@
- Forgot? Reset Password + Forgot? Reset Password
diff --git a/static/html/login.html b/static/html/login.html index 2e9af292..39a17290 100644 --- a/static/html/login.html +++ b/static/html/login.html @@ -40,7 +40,7 @@ {{ if .Register }} {{ range $c := .Links }}
- +