Skip to content

Commit 6cdf05e

Browse files
Igor Dolgiyjehiah
authored andcommitted
Added cookie settings
1 parent 23a89b0 commit 6cdf05e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var (
2323
htpasswdFile = flag.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
2424
cookieSecret = flag.String("cookie-secret", "", "the seed string for secure cookies")
2525
cookieDomain = flag.String("cookie-domain", "", "an optional cookie domain to force cookies to")
26+
cookieExpire = flag.Int("cookie-expire", 168 * 60, "expire time for cookie")
27+
cookieSecure = flag.Bool("cookie-secure", false, "HTTPS only cookie")
2628
authenticatedEmailsFile = flag.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
2729
googleAppsDomains = StringArray{}
2830
upstreams = StringArray{}

oauthproxy.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,27 @@ func (p *OauthProxy) SetCookie(rw http.ResponseWriter, req *http.Request, val st
184184
if *cookieDomain != "" && strings.HasSuffix(domain, *cookieDomain) {
185185
domain = *cookieDomain
186186
}
187+
need_expire := true
188+
expire := time.Now().Add(time.Duration(*cookieExpire))
189+
if *cookieExpire == 0 {
190+
need_expire = false
191+
}
192+
http_only := true
193+
secure := false
194+
if *cookieSecure {
195+
http_only = false
196+
secure = true
197+
}
187198
cookie := &http.Cookie{
188199
Name: p.CookieKey,
189200
Value: signedCookieValue(p.CookieSeed, p.CookieKey, val),
190201
Path: "/",
191202
Domain: domain,
192-
Expires: time.Now().Add(time.Duration(168) * time.Hour), // 7 days
193-
HttpOnly: true,
194-
// Secure: req. ... ? set if X-Scheme: https ?
203+
HttpOnly: http_only,
204+
Secure: secure,
205+
}
206+
if need_expire {
207+
cookie.Expires = expire
195208
}
196209
http.SetCookie(rw, cookie)
197210
}

0 commit comments

Comments
 (0)