Skip to content

Commit

Permalink
feat: implement max-age for session cookie
Browse files Browse the repository at this point in the history
Closes #326
  • Loading branch information
aeneasr committed Jul 27, 2020
1 parent aceeb49 commit 2e642ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .schema/config.schema.json
Expand Up @@ -855,10 +855,9 @@
"properties": {
"lifespan": {
"title": "Session Lifespan",
"description": "Defines how long a session is active. This value is ignored if the \"remember me\" feature is used.",
"description": "Defines how long a session is active. This value is ignored if the \"remember me\" feature is used. If unset (default), the cookie's `Max-Age` will not be set.",
"type": "string",
"pattern": "^[0-9]+(ns|us|ms|s|m|h)$",
"default": "1h",
"examples": [
"1h",
"1m",
Expand Down
10 changes: 8 additions & 2 deletions driver/configuration/provider_viper.go
Expand Up @@ -379,8 +379,14 @@ func (p *ViperProvider) SelfServiceFlowRecoveryUI() *url.URL {
return mustParseURLFromViper(p.l, ViperKeySelfServiceRecoveryUI)
}

func (p *ViperProvider) SessionLifespan() time.Duration {
return viperx.GetDuration(p.l, ViperKeySessionLifespan, time.Hour)
// SessionLifespan returns nil when the value is not set.
func (p *ViperProvider) SessionLifespan() *time.Duration {
if viper.Get(ViperKeySessionLifespan) == nil {
return nil
}

d := viper.GetDuration(ViperKeySessionLifespan)
return &d
}

func (p *ViperProvider) SelfServiceBrowserWhitelistedReturnToDomains() (us []url.URL) {
Expand Down
7 changes: 6 additions & 1 deletion session/manager_http.go
Expand Up @@ -23,7 +23,7 @@ type (
x.CSRFProvider
}
managerHTTPConfiguration interface {
SessionLifespan() time.Duration
SessionLifespan() *time.Duration
SecretsSession() [][]byte
SessionSameSiteMode() http.SameSite
SessionDomain() string
Expand Down Expand Up @@ -71,6 +71,11 @@ func (s *ManagerHTTP) SaveToRequest(ctx context.Context, w http.ResponseWriter,
if s.c.SessionSameSiteMode() != 0 {
cookie.Options.SameSite = s.c.SessionSameSiteMode()
}

if s.c.SessionLifespan() != nil {
cookie.Options.MaxAge = int(s.c.SessionLifespan().Seconds())
}

cookie.Values["sid"] = session.ID.String()
if err := cookie.Save(r, w); err != nil {
return errors.WithStack(err)
Expand Down

0 comments on commit 2e642ff

Please sign in to comment.