BaseURL configuration variable generalization

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

View File

@ -1,7 +1,7 @@
DOKKU_HOST := dokku@dev.lookingfora.name DOKKU_HOST := dokku@dev.lookingfora.name
SHELL := /bin/bash SHELL := /bin/bash
DOCKER_IMAGE_NAME ?= bornholm/hydra-passwordless DOCKER_IMAGE_NAME ?= bornholm/hydra-passwordless
DOCKER_IMAGE_TAG ?= latest DOCKER_IMAGE_TAG ?= $(shell date +%Y%m%d%H%M)
build: build:
CGO_ENABLED=0 go build -v -o bin/server ./cmd/server CGO_ENABLED=0 go build -v -o bin/server ./cmd/server
@ -85,8 +85,10 @@ docker-run:
docker-release: docker-build docker-release: docker-build
docker image tag hydra-passwordless:latest $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) docker image tag hydra-passwordless:latest $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
docker image tag hydra-passwordless:latest $(DOCKER_IMAGE_NAME):latest
docker login docker login
docker push $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG) docker push $(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
docker push $(DOCKER_IMAGE_NAME):latest
clean: clean:
rm -rf release rm -rf release

View File

@ -7,7 +7,7 @@
<title>{{block "title" . -}}{{- end}}</title> <title>{{block "title" . -}}{{- end}}</title>
{{- block "head_style" . -}} {{- block "head_style" . -}}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.8.0/css/bulma.min.css" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.8.0/css/bulma.min.css" />
<link rel="stylesheet" href="/css/style.css" /> <link rel="stylesheet" href="{{ .BaseURL }}/css/style.css" />
{{end}} {{end}}
{{- block "head_script" . -}}{{end}} {{- block "head_script" . -}}{{end}}
</head> </head>

View File

@ -13,7 +13,7 @@
Autorisez vous l'application à utiliser ces informations vous concernant ? Autorisez vous l'application à utiliser ces informations vous concernant ?
</p> </p>
<div class="box"> <div class="box">
<form action="/consent" method="POST"> <form action="{{ .BaseURL }}/consent" method="POST">
{{range .RequestedScope}} {{range .RequestedScope}}
<div class=""> <div class="">
<label class="checkbox"> <label class="checkbox">

View File

@ -13,7 +13,7 @@
Veuillez entrer votre adresse courriel. Veuillez entrer votre adresse courriel.
</p> </p>
<div class="box"> <div class="box">
<form action="/login" method="POST"> <form action="{{ .BaseURL }}/login" method="POST">
<div class="field"> <div class="field">
<div class="control"> <div class="control">
<input class="input is-large" type="email" <input class="input is-large" type="email"

View File

@ -18,13 +18,12 @@ import (
) )
type SendConfirmationEmailRequest struct { type SendConfirmationEmailRequest struct {
Email string Email string
Challenge string Challenge string
DefaultScheme string BaseURL string
DefaultAddress string RememberMe bool
RememberMe bool ClientName string
ClientName string ClientURI string
ClientURI string
} }
func HandleSendConfirmationEmailRequest(ctx context.Context, cmd cqrs.Command) error { 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") return errors.Wrap(err, "could not generate jwt")
} }
address := req.DefaultAddress verificationLink := fmt.Sprintf("%s/verify?token=%s", req.BaseURL, token)
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)
data := template.Data{ data := template.Data{
"ClientName": req.ClientName, "ClientName": req.ClientName,

View File

@ -42,12 +42,10 @@ type HTTPConfig struct {
CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"` CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
TokenSigningKey string `yaml:"tokenSigningKey" env:"HTTP_TOKEN_SIGNING_KEY"` TokenSigningKey string `yaml:"tokenSigningKey" env:"HTTP_TOKEN_SIGNING_KEY"`
TokenEncryptionKey string `yaml:"tokenEncryptionKey" env:"HTTP_TOKEN_ENCRYPTION_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"` CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"` TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_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 { type SMTPConfig struct {
@ -100,8 +98,7 @@ func NewDefault() *Config {
CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour CookieMaxAge: int((time.Hour * 1).Seconds()), // 1 hour
TemplateDir: "template", TemplateDir: "template",
PublicDir: "public", PublicDir: "public",
PublicAddress: "", BaseURL: "/",
PublicScheme: "",
}, },
SMTP: SMTPConfig{ SMTP: SMTPConfig{
Host: "localhost", Host: "localhost",

View File

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

View File

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

View File

@ -1,4 +1,4 @@
FROM golang:1.14 AS build FROM golang:1.17 AS build
ARG HTTP_PROXY= ARG HTTP_PROXY=
ARG HTTPS_PROXY= ARG HTTPS_PROXY=