Skip to content

Commit

Permalink
(NOBIDS) frontend: derive cookie-domain from request if enabled via c…
Browse files Browse the repository at this point in the history
…onfig
  • Loading branch information
guybrush committed May 16, 2024
1 parent 79ca35a commit 955105e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
17 changes: 9 additions & 8 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ type Config struct {
Plankton string `yaml:"plankton" envconfig:"FRONTEND_STRIPE_PLANKTON"`
Webhook string `yaml:"webhook" envconfig:"FRONTEND_STRIPE_WEBHOOK"`
}
RatelimitUpdateInterval time.Duration `yaml:"ratelimitUpdateInterval" envconfig:"FRONTEND_RATELIMIT_UPDATE_INTERVAL"`
SessionSecret string `yaml:"sessionSecret" envconfig:"FRONTEND_SESSION_SECRET"`
SessionCookieDomain string `yaml:"sessionCookieDomain" envconfig:"FRONTEND_SESSION_COOKIE_DOMAIN"`
JwtSigningSecret string `yaml:"jwtSigningSecret" envconfig:"FRONTEND_JWT_SECRET"`
JwtIssuer string `yaml:"jwtIssuer" envconfig:"FRONTEND_JWT_ISSUER"`
JwtValidityInMinutes int `yaml:"jwtValidityInMinutes" envconfig:"FRONTEND_JWT_VALIDITY_INMINUTES"`
MaxMailsPerEmailPerDay int `yaml:"maxMailsPerEmailPerDay" envconfig:"FRONTEND_MAX_MAIL_PER_EMAIL_PER_DAY"`
Mail struct {
RatelimitUpdateInterval time.Duration `yaml:"ratelimitUpdateInterval" envconfig:"FRONTEND_RATELIMIT_UPDATE_INTERVAL"`
SessionSecret string `yaml:"sessionSecret" envconfig:"FRONTEND_SESSION_SECRET"`
SessionCookieDomain string `yaml:"sessionCookieDomain" envconfig:"FRONTEND_SESSION_COOKIE_DOMAIN"`
SessionCookieDeriveDomainFromRequest bool `yaml:"sessionCookieDeriveDomainFromRequest" envconfig:"FRONTEND_SESSION_COOKIE_DERIVE_DOMAIN_FROM_REQUEST"`
JwtSigningSecret string `yaml:"jwtSigningSecret" envconfig:"FRONTEND_JWT_SECRET"`
JwtIssuer string `yaml:"jwtIssuer" envconfig:"FRONTEND_JWT_ISSUER"`
JwtValidityInMinutes int `yaml:"jwtValidityInMinutes" envconfig:"FRONTEND_JWT_VALIDITY_INMINUTES"`
MaxMailsPerEmailPerDay int `yaml:"maxMailsPerEmailPerDay" envconfig:"FRONTEND_MAX_MAIL_PER_EMAIL_PER_DAY"`
Mail struct {
SMTP struct {
Server string `yaml:"server" envconfig:"FRONTEND_MAIL_SMTP_SERVER"`
Host string `yaml:"host" envconfig:"FRONTEND_MAIL_SMTP_HOST"`
Expand Down
13 changes: 11 additions & 2 deletions utils/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/alexedwards/scs/redisstore"
"github.com/gobitfly/scs/v2"
"github.com/gomodule/redigo/redis"
"golang.org/x/net/publicsuffix"
)

// SessionStore is a securecookie-based session-store.
Expand Down Expand Up @@ -101,8 +102,16 @@ func InitSessionStore(secret string) {
sessionManager.Cookie.Secure = true
sessionManager.Cookie.Domain = Config.Frontend.SessionCookieDomain

sessionManager.CookieFunc = func(r *http.Request, c *http.Cookie) {
// r.URL.
if Config.Frontend.SessionCookieDeriveDomainFromRequest {
logger.Infof("deriving cookie.domain from request")
sessionManager.CookieFunc = func(r *http.Request, c *http.Cookie) {
domainname, err := publicsuffix.EffectiveTLDPlusOne(r.Host)
if err != nil {
logger.Warnf("error deriving domain from request (host: %v): %v", r.Host, err)
return
}
c.Domain = "." + domainname
}
}

sessionManager.Store = redisstore.New(pool)
Expand Down

0 comments on commit 955105e

Please sign in to comment.