BaseURL configuration variable generalization

This commit is contained in:
2022-03-25 15:11:29 +01:00
parent 41748363d1
commit 33dbb6ea47
9 changed files with 51 additions and 40 deletions

View File

@ -18,13 +18,12 @@ import (
)
type SendConfirmationEmailRequest struct {
Email string
Challenge string
DefaultScheme string
DefaultAddress string
RememberMe bool
ClientName string
ClientURI string
Email string
Challenge string
BaseURL string
RememberMe bool
ClientName string
ClientURI string
}
func HandleSendConfirmationEmailRequest(ctx context.Context, cmd cqrs.Command) error {
@ -56,21 +55,7 @@ func HandleSendConfirmationEmailRequest(ctx context.Context, cmd cqrs.Command) e
return errors.Wrap(err, "could not generate jwt")
}
address := req.DefaultAddress
if conf.HTTP.PublicAddress != "" {
address = conf.HTTP.PublicAddress
}
scheme := req.DefaultScheme
if scheme == "" {
scheme = "http:"
}
if conf.HTTP.PublicScheme != "" {
scheme = conf.HTTP.PublicScheme
}
verificationLink := fmt.Sprintf("%s//%s/verify?token=%s", scheme, address, token)
verificationLink := fmt.Sprintf("%s/verify?token=%s", req.BaseURL, token)
data := template.Data{
"ClientName": req.ClientName,

View File

@ -42,12 +42,10 @@ type HTTPConfig struct {
CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
TokenSigningKey string `yaml:"tokenSigningKey" env:"HTTP_TOKEN_SIGNING_KEY"`
TokenEncryptionKey string `yaml:"tokenEncryptionKey" env:"HTTP_TOKEN_ENCRYPTION_KEY"`
BasePublicURL string `yaml:"basePublicUrl" env:"HTTP_BASE_PUBLIC_URL"`
BaseURL string `yaml:"basePublicUrl" env:"HTTP_BASE_URL"`
CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_DIR"`
PublicAddress string `yaml:"publicAddress" env:"HTTP_PUBLIC_ADDRESS"`
PublicScheme string `yaml:"publicScheme" env:"HTTP_PUBLIC_SCHEME"`
}
type SMTPConfig struct {
@ -100,8 +98,7 @@ func NewDefault() *Config {
CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour
TemplateDir: "template",
PublicDir: "public",
PublicAddress: "",
PublicScheme: "",
BaseURL: "/",
},
SMTP: SMTPConfig{
Host: "localhost",

View File

@ -2,9 +2,12 @@ package route
import (
"net/http"
"strings"
"forge.cadoles.com/wpetit/hydra-passwordless/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,8 +17,8 @@ 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"))
}
@ -23,6 +26,19 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da
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, errors.WithStack(err)
}
data["BaseURL"] = strings.TrimSuffix(conf.HTTP.BaseURL, "/")
return data, nil
}
}
func renderErrorPage(w http.ResponseWriter, r *http.Request, statusCode int, title, description string) error {
ctn := container.Must(r.Context())
tmpl := template.Must(ctn)

View File

@ -1,10 +1,13 @@
package route
import (
"fmt"
"net/http"
netMail "net/mail"
"strings"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/command"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/config"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/hydra"
"github.com/gorilla/csrf"
"github.com/pkg/errors"
@ -81,6 +84,7 @@ func handleLoginForm(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(ctn)
hydr := hydra.Must(ctn)
bus := cqrs.Must(ctn)
conf := config.Must(ctn)
if err := r.ParseForm(); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
@ -143,14 +147,21 @@ func handleLoginForm(w http.ResponseWriter, r *http.Request) {
return
}
var baseURL string
if conf.HTTP.BaseURL != "" {
baseURL = strings.TrimSuffix(conf.HTTP.BaseURL, "/")
} else {
baseURL = fmt.Sprintf("%s//%s", r.Host, r.URL.Scheme)
}
cmd := &command.SendConfirmationEmailRequest{
Email: email,
Challenge: challenge,
DefaultScheme: r.URL.Scheme,
DefaultAddress: r.Host,
RememberMe: rememberMe == "on",
ClientName: res.Client.ClientName,
ClientURI: res.Client.ClientURI,
Email: email,
Challenge: challenge,
BaseURL: baseURL,
RememberMe: rememberMe == "on",
ClientName: res.Client.ClientName,
ClientURI: res.Client.ClientURI,
}
if _, err := bus.Exec(ctx, cmd); err != nil {
panic(errors.Wrap(err, "could not execute command"))