Exemplo n.º 1
0
func (s *SessionManager) Set(session map[string]interface{}, rw http.ResponseWriter, req *http.Request) {
	origCookie, err := req.Cookie(s.CookieName)
	var origCookieVal string
	if err != nil {
		origCookieVal = ""
	} else {
		origCookieVal = origCookie.Value
	}

	if len(session) == 0 {
		if origCookieVal != "" {
			utils.SetCookie(rw, nil, s.CookieName, "", -3600)
		}
	} else {
		var cookieExpires int
		if ce, ok := session["__cookieExpires"].(int); ok {
			cookieExpires = ce
		}

		if encoded, err := encodeCookie(session, s.key, s.iv); err == nil {
			if encoded != origCookieVal {
				utils.SetCookie(rw, nil, s.CookieName, encoded, cookieExpires, "/", s.CookieDomain, true)
			}
		}
	}
}
Exemplo n.º 2
0
func (s *SessionManager) Set(rw http.ResponseWriter, req *http.Request) {
	s.rmutex.RLock()
	cookieName := s.CookieName
	s.rmutex.RUnlock()

	if c, err := req.Cookie(cookieName); err == nil {
		sessionSign := c.Value
		s.rmutex.RLock()
		lsess := len(s.sessions[sessionSign][1])
		s.rmutex.RUnlock()

		if lsess == 0 {
			s.Clear(sessionSign)
			utils.SetCookie(rw, nil, cookieName, "", -3600)
		}
	}
}
Exemplo n.º 3
0
func (s *SessionManager) new(rw http.ResponseWriter) string {
	timeNano := time.Now().UnixNano()
	s.rmutex.RLock()
	cookieName := s.CookieName
	cookieDomain := s.CookieDomain
	sessionSign := s.sessionSign()
	s.rmutex.RUnlock()

	s.mutex.Lock()
	s.sessions[sessionSign] = [2]map[string]interface{}{
		map[string]interface{}{
			"create": timeNano,
		},
		map[string]interface{}{},
	}
	s.mutex.Unlock()

	utils.SetCookie(rw, nil, cookieName, sessionSign, 0, "/", cookieDomain, true)

	return sessionSign
}
Exemplo n.º 4
0
func (s *SessionManager) new(rw http.ResponseWriter) string {
	sessionSign := getSessionSign()
	utils.SetCookie(rw, nil, s.CookieName, sessionSign, 0, "/", s.CookieDomain, true)

	return sessionSign
}