@@ -29,10 +29,6 @@ const (
2929 httpScheme = "http"
3030 httpsScheme = "https"
3131
32− // Cookies are limited to 4kb including the length of the cookie name,
33− // the cookie name can be up to 256 bytes
34− maxCookieLength = 3840
35−
3632 applicationJSON = "application/json"
3733)
3834
@@ -318,104 +314,6 @@ func (p *OAuthProxy) redeemCode(host, code string) (s *sessionsapi.SessionState,
318314 return
319315}
320316
321−// MakeSessionCookie creates an http.Cookie containing the authenticated user's
322−// authentication details
323−func (p *OAuthProxy) MakeSessionCookie(req *http.Request, value string, expiration time.Duration, now time.Time) []*http.Cookie {
324− if value != "" {
325− value = cookie.SignedValue(p.CookieSeed, p.CookieName, value, now)
326− }
327− c := p.makeCookie(req, p.CookieName, value, expiration, now)
328− if len(c.Value) > 4096-len(p.CookieName) {
329− return splitCookie(c)
330− }
331− return []*http.Cookie{c}
332−}
333−
334−func copyCookie(c *http.Cookie) *http.Cookie {
335− return &http.Cookie{
336− Name: c.Name,
337− Value: c.Value,
338− Path: c.Path,
339− Domain: c.Domain,
340− Expires: c.Expires,
341− RawExpires: c.RawExpires,
342− MaxAge: c.MaxAge,
343− Secure: c.Secure,
344− HttpOnly: c.HttpOnly,
345− Raw: c.Raw,
346− Unparsed: c.Unparsed,
347− }
348−}
349−
350−// splitCookie reads the full cookie generated to store the session and splits
351−// it into a slice of cookies which fit within the 4kb cookie limit indexing
352−// the cookies from 0
353−func splitCookie(c *http.Cookie) []*http.Cookie {
354− if len(c.Value) < maxCookieLength {
355− return []*http.Cookie{c}
356− }
357− cookies := []*http.Cookie{}
358− valueBytes := []byte(c.Value)
359− count := 0
360− for len(valueBytes) > 0 {
361− new := copyCookie(c)
362− new.Name = fmt.Sprintf("%s_%d", c.Name, count)
363− count++
364− if len(valueBytes) < maxCookieLength {
365− new.Value = string(valueBytes)
366− valueBytes = []byte{}
367− } else {
368− newValue := valueBytes[:maxCookieLength]
369− valueBytes = valueBytes[maxCookieLength:]
370− new.Value = string(newValue)
371− }
372− cookies = append(cookies, new)
373− }
374− return cookies
375−}
376−
377−// joinCookies takes a slice of cookies from the request and reconstructs the
378−// full session cookie
379−func joinCookies(cookies []*http.Cookie) (*http.Cookie, error) {
380− if len(cookies) == 0 {
381− return nil, fmt.Errorf("list of cookies must be > 0")
382− }
383− if len(cookies) == 1 {
384− return cookies[0], nil
385− }
386− c := copyCookie(cookies[0])
387− for i := 1; i < len(cookies); i++ {
388− c.Value += cookies[i].Value
389− }
390− c.Name = strings.TrimRight(c.Name, "_0")
391− return c, nil
392−}
393−
394−// loadCookie retreieves the sessions state cookie from the http request.
395−// If a single cookie is present this will be returned, otherwise it attempts
396−// to reconstruct a cookie split up by splitCookie
397−func loadCookie(req *http.Request, cookieName string) (*http.Cookie, error) {
398− c, err := req.Cookie(cookieName)
399− if err == nil {
400− return c, nil
401− }
402− cookies := []*http.Cookie{}
403− err = nil
404− count := 0
405− for err == nil {
406− var c *http.Cookie
407− c, err = req.Cookie(fmt.Sprintf("%s_%d", cookieName, count))
408− if err == nil {
409− cookies = append(cookies, c)
410− count++
411− }
412− }
413− if len(cookies) == 0 {
414− return nil, fmt.Errorf("Could not find cookie %s", cookieName)
415− }
416− return joinCookies(cookies)
417−}
418−
419317// MakeCSRFCookie creates a cookie for CSRF
420318func (p *OAuthProxy) MakeCSRFCookie(req *http.Request, value string, expiration time.Duration, now time.Time) *http.Cookie {
421319 return p.makeCookie(req, p.CSRFCookieName, value, expiration, now)
@@ -460,13 +358,6 @@ func (p *OAuthProxy) ClearSessionCookie(rw http.ResponseWriter, req *http.Reques
460358 return p.sessionStore.Clear(rw, req)
461359}
462360
463−// SetSessionCookie adds the user's session cookie to the response
464−func (p *OAuthProxy) SetSessionCookie(rw http.ResponseWriter, req *http.Request, val string) {
465− for _, c := range p.MakeSessionCookie(req, val, p.CookieExpire, time.Now()) {
466− http.SetCookie(rw, c)
467− }
468−}
469−
470361// LoadCookiedSession reads the user's authentication details from the request
471362func (p *OAuthProxy) LoadCookiedSession(req *http.Request) (*sessionsapi.SessionState, error) {
472363 return p.sessionStore.Load(req)