From d786d3a26fb0e45b1dc3a5479cc7d1202b32f128 Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 2 Aug 2022 11:48:42 +0200 Subject: [PATCH] Use base url to generate template links/redirects --- cmd/server/template/blocks/header.html.tmpl | 6 +++--- cmd/server/template/layouts/home.html.tmpl | 2 +- cmd/server/template/layouts/profile.html.tmpl | 2 +- internal/config/config.go | 2 ++ internal/route/helper.go | 15 ++++++++++++++- internal/route/home.go | 4 +++- internal/route/login.go | 6 +++++- 7 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cmd/server/template/blocks/header.html.tmpl b/cmd/server/template/blocks/header.html.tmpl index 6af7913..b7eaeee 100644 --- a/cmd/server/template/blocks/header.html.tmpl +++ b/cmd/server/template/blocks/header.html.tmpl @@ -3,16 +3,16 @@
{{if .IDToken}} - Logout + Logout {{else}} - Login + Login {{end}}
diff --git a/cmd/server/template/layouts/home.html.tmpl b/cmd/server/template/layouts/home.html.tmpl index 78e2508..0c623e4 100644 --- a/cmd/server/template/layouts/home.html.tmpl +++ b/cmd/server/template/layouts/home.html.tmpl @@ -1,4 +1,4 @@ -{{define "title"}}Accueil{{end}} +{{define "title"}}Home | OIDC Test App{{end}} {{define "body"}}
diff --git a/cmd/server/template/layouts/profile.html.tmpl b/cmd/server/template/layouts/profile.html.tmpl index e26dc91..0c9c965 100644 --- a/cmd/server/template/layouts/profile.html.tmpl +++ b/cmd/server/template/layouts/profile.html.tmpl @@ -1,4 +1,4 @@ -{{define "title"}}Accueil{{end}} +{{define "title"}}Profile | OIDC Test App{{end}} {{define "body"}}
diff --git a/internal/config/config.go b/internal/config/config.go index a09102b..48f0a8c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -37,6 +37,7 @@ func NewFromFile(filepath string) (*Config, error) { type HTTPConfig struct { Address string `yaml:"address" env:"HTTP_ADDRESS"` + PublicBaseURL string `yaml:"publicBaseURL" env:"HTTP_PUBLIC_BASE_URL"` CookieAuthenticationKey string `yaml:"cookieAuthenticationKey" env:"HTTP_COOKIE_AUTHENTICATION_KEY"` CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"` CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"` @@ -73,6 +74,7 @@ func NewDefault() *Config { }, HTTP: HTTPConfig{ Address: ":3002", + PublicBaseURL: "", CookieAuthenticationKey: "", CookieEncryptionKey: "", CookiePath: "/", diff --git a/internal/route/helper.go b/internal/route/helper.go index cf7c774..64694da 100644 --- a/internal/route/helper.go +++ b/internal/route/helper.go @@ -3,8 +3,10 @@ package route import ( "net/http" + "forge.cadoles.com/wpetit/goweb-oidc/internal/config" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/middleware/container" + "gitlab.com/wpetit/goweb/service" "gitlab.com/wpetit/goweb/service/template" "gitlab.com/wpetit/goweb/template/html" ) @@ -14,11 +16,22 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da data, err := template.Extend(data, html.WithFlashes(w, r, ctn), template.WithBuildInfo(w, r, ctn), + withBaseURL(w, r, ctn), ) - if err != nil { panic(errors.Wrap(err, "could not extend template data")) } return data } + +func withBaseURL(w http.ResponseWriter, r *http.Request, ctn *service.Container) template.DataExtFunc { + return func(data template.Data) (template.Data, error) { + conf, err := config.From(ctn) + if err != nil { + return nil, err + } + data["BaseURL"] = conf.HTTP.PublicBaseURL + return data, nil + } +} diff --git a/internal/route/home.go b/internal/route/home.go index 2b0e17a..e164037 100644 --- a/internal/route/home.go +++ b/internal/route/home.go @@ -4,6 +4,7 @@ import ( "net/http" oidc "forge.cadoles.com/wpetit/goweb-oidc" + "forge.cadoles.com/wpetit/goweb-oidc/internal/config" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/service/template" @@ -12,11 +13,12 @@ import ( func serveHomePage(w http.ResponseWriter, r *http.Request) { ctn := container.Must(r.Context()) tmpl := template.Must(ctn) + conf := config.Must(ctn) idToken, _ := oidc.IDToken(w, r) if idToken != nil { - http.Redirect(w, r, "/profile", http.StatusSeeOther) + http.Redirect(w, r, conf.HTTP.PublicBaseURL+"/profile", http.StatusSeeOther) return } diff --git a/internal/route/login.go b/internal/route/login.go index 91dfe3d..2a0ab31 100644 --- a/internal/route/login.go +++ b/internal/route/login.go @@ -4,6 +4,7 @@ import ( "net/http" oidc "forge.cadoles.com/wpetit/goweb-oidc" + "forge.cadoles.com/wpetit/goweb-oidc/internal/config" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/middleware/container" @@ -27,7 +28,10 @@ func handleLoginCallback(w http.ResponseWriter, r *http.Request) { return } + ctn := container.Must(ctx) + conf := config.Must(ctn) + logger.Info(ctx, "user logged in", logger.F("sub", idToken.Subject)) - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, conf.HTTP.PublicBaseURL+"/", http.StatusSeeOther) }