Extract MakeCookie()

This commit is contained in:
Mike Bland 2015-05-08 11:51:11 -04:00
parent 1bd90cefe7
commit beed9fb9a2

View File

@ -223,7 +223,7 @@ func (p *OauthProxy) redeemCode(host, code string) (string, string, error) {
return access_token, email, nil return access_token, email, nil
} }
func (p *OauthProxy) ClearCookie(rw http.ResponseWriter, req *http.Request) { func (p *OauthProxy) MakeCookie(req *http.Request, value string, expiration time.Duration) *http.Cookie {
domain := req.Host domain := req.Host
if h, _, err := net.SplitHostPort(domain); err == nil { if h, _, err := net.SplitHostPort(domain); err == nil {
domain = h domain = h
@ -234,40 +234,28 @@ func (p *OauthProxy) ClearCookie(rw http.ResponseWriter, req *http.Request) {
} }
domain = p.CookieDomain domain = p.CookieDomain
} }
cookie := &http.Cookie{
if value != "" {
value = signedCookieValue(p.CookieSeed, p.CookieKey, value)
}
return &http.Cookie{
Name: p.CookieKey, Name: p.CookieKey,
Value: "", Value: value,
Path: "/", Path: "/",
Domain: domain, Domain: domain,
HttpOnly: p.CookieHttpOnly, HttpOnly: p.CookieHttpOnly,
Secure: p.CookieSecure, Secure: p.CookieSecure,
Expires: time.Now().Add(time.Duration(1) * time.Hour * -1), Expires: time.Now().Add(expiration),
} }
http.SetCookie(rw, cookie) }
func (p *OauthProxy) ClearCookie(rw http.ResponseWriter, req *http.Request) {
http.SetCookie(rw, p.MakeCookie(req, "", time.Duration(1)*time.Hour*-1))
} }
func (p *OauthProxy) SetCookie(rw http.ResponseWriter, req *http.Request, val string) { func (p *OauthProxy) SetCookie(rw http.ResponseWriter, req *http.Request, val string) {
http.SetCookie(rw, p.MakeCookie(req, val, p.CookieExpire))
domain := req.Host
if h, _, err := net.SplitHostPort(domain); err == nil {
domain = h
}
if p.CookieDomain != "" {
if !strings.HasSuffix(domain, p.CookieDomain) {
log.Printf("Warning: request host is %q but using configured cookie domain of %q", domain, p.CookieDomain)
}
domain = p.CookieDomain
}
cookie := &http.Cookie{
Name: p.CookieKey,
Value: signedCookieValue(p.CookieSeed, p.CookieKey, val),
Path: "/",
Domain: domain,
HttpOnly: p.CookieHttpOnly,
Secure: p.CookieSecure,
Expires: time.Now().Add(p.CookieExpire),
}
http.SetCookie(rw, cookie)
} }
func (p *OauthProxy) ProcessCookie(rw http.ResponseWriter, req *http.Request) (email, user, access_token string, ok bool) { func (p *OauthProxy) ProcessCookie(rw http.ResponseWriter, req *http.Request) (email, user, access_token string, ok bool) {