From a0677f23e557fa9884cae20704077427bef4b5b4 Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 8 Sep 2020 11:38:38 +0200 Subject: [PATCH] Allow remember be duration to be customized via config --- internal/config/config.go | 16 +++++++++++++--- internal/route/verify.go | 9 ++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 8769d5c..26deabb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,9 +12,10 @@ import ( ) type Config struct { - HTTP HTTPConfig `yaml:"http"` - SMTP SMTPConfig `yaml:"smtp"` - Hydra HydraConfig `yaml:"hydra"` + HTTP HTTPConfig `yaml:"http"` + SMTP SMTPConfig `yaml:"smtp"` + Hydra HydraConfig `yaml:"hydra"` + Session SessionConfig `yaml:"session"` } // NewFromFile retrieves the configuration from the given file @@ -67,6 +68,11 @@ type HydraConfig struct { HTTPClientTimeout time.Duration `yaml:"httpClientTimeout" env:"HYDRA_HTTP_CLIENT_TIMEOUT"` } +type SessionConfig struct { + DefaultDuration int `yaml:"defaultDuration" env:"HYDRA_SESSION_DEFAULT_DURATION"` + RememberMeDuration int `yaml:"rememberMeDuration" env:"HYDRA_SESSION_REMEMBER_ME_DURATION"` +} + func NewDumpDefault() *Config { config := NewDefault() return config @@ -99,6 +105,10 @@ func NewDefault() *Config { FakeSSLTermination: false, HTTPClientTimeout: time.Second * 30, //nolint: gomnb }, + Session: SessionConfig{ + DefaultDuration: int((time.Hour * 1).Seconds()), // 1 hour + RememberMeDuration: int((time.Hour * 24 * 30).Seconds()), // 30 days + }, } } diff --git a/internal/route/verify.go b/internal/route/verify.go index 149b788..d80e477 100644 --- a/internal/route/verify.go +++ b/internal/route/verify.go @@ -3,6 +3,7 @@ package route import ( "net/http" + "forge.cadoles.com/wpetit/hydra-passwordless/internal/config" "forge.cadoles.com/wpetit/hydra-passwordless/internal/hydra" "forge.cadoles.com/wpetit/hydra-passwordless/internal/query" "github.com/pkg/errors" @@ -14,6 +15,7 @@ import ( func handleVerification(w http.ResponseWriter, r *http.Request) { ctn := container.Must(r.Context()) bus := cqrs.Must(ctn) + conf := config.Must(ctn) token := r.URL.Query().Get("token") if token == "" { @@ -52,10 +54,15 @@ func handleVerification(w http.ResponseWriter, r *http.Request) { hydr := hydra.Must(ctn) + rememberFor := conf.Session.DefaultDuration + if verifyUserData.RememberMe { + rememberFor = conf.Session.RememberMeDuration + } + accept := &hydra.AcceptLoginRequest{ Subject: verifyUserData.Email, Remember: verifyUserData.RememberMe, - RememberFor: 3600, + RememberFor: rememberFor, Context: map[string]interface{}{ "email": verifyUserData.Email, },