Use base url to generate template links/redirects

This commit is contained in:
wpetit 2022-08-02 11:48:42 +02:00
parent 5beae19d21
commit d786d3a26f
7 changed files with 29 additions and 8 deletions

View File

@ -3,16 +3,16 @@
<div class="column is-4-tablet is-8-mobile"> <div class="column is-4-tablet is-8-mobile">
<div class="columns is-mobile is-gapless"> <div class="columns is-mobile is-gapless">
<div class="column is-narrow"> <div class="column is-narrow">
<h1 class="is-size-3 title"><a href="/" rel="Homepage" class="has-text-black">OIDC Test App</a></h1> <h1 class="is-size-3 title"><a href="{{ .BaseURL }}" rel="Homepage" class="has-text-black">OIDC Test App</a></h1>
</div> </div>
</div> </div>
</div> </div>
<div class="column"> <div class="column">
<div class="buttons is-right"> <div class="buttons is-right">
{{if .IDToken}} {{if .IDToken}}
<a class="button" href="/logout">Logout</a> <a class="button" href="{{ .BaseURL }}/logout">Logout</a>
{{else}} {{else}}
<a class="button is-primary" href="/login">Login</a> <a class="button is-primary" href="{{ .BaseURL }}/login">Login</a>
{{end}} {{end}}
</div> </div>
</div> </div>

View File

@ -1,4 +1,4 @@
{{define "title"}}Accueil{{end}} {{define "title"}}Home | OIDC Test App{{end}}
{{define "body"}} {{define "body"}}
<section class="home is-fullheight section"> <section class="home is-fullheight section">
<div class="container"> <div class="container">

View File

@ -1,4 +1,4 @@
{{define "title"}}Accueil{{end}} {{define "title"}}Profile | OIDC Test App{{end}}
{{define "body"}} {{define "body"}}
<section class="home is-fullheight section"> <section class="home is-fullheight section">
<div class="container"> <div class="container">

View File

@ -37,6 +37,7 @@ func NewFromFile(filepath string) (*Config, error) {
type HTTPConfig struct { type HTTPConfig struct {
Address string `yaml:"address" env:"HTTP_ADDRESS"` 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"` CookieAuthenticationKey string `yaml:"cookieAuthenticationKey" env:"HTTP_COOKIE_AUTHENTICATION_KEY"`
CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"` CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"` CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
@ -73,6 +74,7 @@ func NewDefault() *Config {
}, },
HTTP: HTTPConfig{ HTTP: HTTPConfig{
Address: ":3002", Address: ":3002",
PublicBaseURL: "",
CookieAuthenticationKey: "", CookieAuthenticationKey: "",
CookieEncryptionKey: "", CookieEncryptionKey: "",
CookiePath: "/", CookiePath: "/",

View File

@ -3,8 +3,10 @@ package route
import ( import (
"net/http" "net/http"
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service"
"gitlab.com/wpetit/goweb/service/template" "gitlab.com/wpetit/goweb/service/template"
"gitlab.com/wpetit/goweb/template/html" "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, data, err := template.Extend(data,
html.WithFlashes(w, r, ctn), html.WithFlashes(w, r, ctn),
template.WithBuildInfo(w, r, ctn), template.WithBuildInfo(w, r, ctn),
withBaseURL(w, r, ctn),
) )
if err != nil { if err != nil {
panic(errors.Wrap(err, "could not extend template data")) panic(errors.Wrap(err, "could not extend template data"))
} }
return 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
}
}

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
oidc "forge.cadoles.com/wpetit/goweb-oidc" oidc "forge.cadoles.com/wpetit/goweb-oidc"
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/template" "gitlab.com/wpetit/goweb/service/template"
@ -12,11 +13,12 @@ import (
func serveHomePage(w http.ResponseWriter, r *http.Request) { func serveHomePage(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context()) ctn := container.Must(r.Context())
tmpl := template.Must(ctn) tmpl := template.Must(ctn)
conf := config.Must(ctn)
idToken, _ := oidc.IDToken(w, r) idToken, _ := oidc.IDToken(w, r)
if idToken != nil { if idToken != nil {
http.Redirect(w, r, "/profile", http.StatusSeeOther) http.Redirect(w, r, conf.HTTP.PublicBaseURL+"/profile", http.StatusSeeOther)
return return
} }

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
oidc "forge.cadoles.com/wpetit/goweb-oidc" oidc "forge.cadoles.com/wpetit/goweb-oidc"
"forge.cadoles.com/wpetit/goweb-oidc/internal/config"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
@ -27,7 +28,10 @@ func handleLoginCallback(w http.ResponseWriter, r *http.Request) {
return return
} }
ctn := container.Must(ctx)
conf := config.Must(ctn)
logger.Info(ctx, "user logged in", logger.F("sub", idToken.Subject)) 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)
} }