Exemplo n.º 1
0
// Save adds a single session to the response.
func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter,
	session *Session) error {
	encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
		s.Codecs...)
	if err != nil {
		return err
	}
	http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options))
	return nil
}
Exemplo n.º 2
0
// save writes encoded session.Values to a file.
func (s *FilesystemStore) save(session *Session) error {
	encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
		s.Codecs...)
	if err != nil {
		return err
	}
	filename := s.path + "session_" + session.ID
	fileMutex.Lock()
	defer fileMutex.Unlock()
	fp, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		return err
	}
	if _, err = fp.Write([]byte(encoded)); err != nil {
		return err
	}
	fp.Close()
	return nil
}
Exemplo n.º 3
0
// Save adds a single session to the response.
func (s *FilesystemStore) Save(r *http.Request, w http.ResponseWriter,
	session *Session) error {
	if session.ID == "" {
		// Because the ID is used in the filename, encode it to
		// use alphanumeric characters only.
		session.ID = strings.TrimRight(
			base32.StdEncoding.EncodeToString(
				securecookie.GenerateRandomKey(32)), "=")
	}
	if err := s.save(session); err != nil {
		return err
	}
	encoded, err := securecookie.EncodeMulti(session.Name(), session.ID,
		s.Codecs...)
	if err != nil {
		return err
	}
	http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options))
	return nil
}
Exemplo n.º 4
0
func SetCookie(name string, value interface{}, options *CookieOptions, w http.ResponseWriter) error {
	encoded, err := securecookie.EncodeMulti(name, value, codecs...)
	if err != nil {
		return err
	}

	// NOTE(sadovsky): If path is not "/", Chrome will not set cookies on a 302
	// redirect.
	cookie := &http.Cookie{
		Name:     name,
		Value:    encoded,
		Path:     "/",
		MaxAge:   options.MaxAge,
		HttpOnly: true, // see http://goo.gl/n4Bui
	}
	if options.MaxAge > 0 {
		cookie.Expires = time.Now().Add(time.Duration(options.MaxAge) * time.Second)
	} else if options.MaxAge < 0 {
		cookie.Expires = time.Unix(0, 0)
	}
	http.SetCookie(w, cookie)
	return nil
}