Beispiel #1
0
// Get attempts to get session from local sessions map.
func (s *Sessions) Get(r *http.Request) (*Session, error) {

	// Getting SessID from cookie
	cookie, err := r.Cookie(cfg.SessionIDKey)
	if err != nil {
		return nil, errNoSessionID
	}
	sessionID := cookie.Value

	// Make it atomic
	s.RLock()
	defer s.RUnlock()

	// Check for session exist
	sess, ok := s.sessions[sessionID]
	if !ok {
		return nil, errNoSessionWithID
	}

	// Check session IP and client IP if StrictIP on
	if cfg.StrictIP {
		if sess.IP != strings.Split(r.RemoteAddr, ":")[0] {
			return nil, errIP
		}
	}

	return deepcopy.Iface(&sess).(*Session), nil
}
Beispiel #2
0
// StartNewSession Create new session for user, generate sessionID and write new cookie into response
// If session for user already exist it returns error
func (s *Sessions) StartNewSession(r *http.Request, w http.ResponseWriter, username string) *Session {

	sessionID := stringutils.GetRandomString(cfg.SessionIDKeyLength)
	http.SetCookie(w, &http.Cookie{Name: cfg.SessionIDKey, Value: sessionID, Path: "/"})

	sess := Session{
		ID:        sessionID,
		IP:        strings.Split(r.RemoteAddr, ":")[0],
		Username:  username,
		UserAgent: r.UserAgent(),
		// 	UserInfo:     users.Get(username),
		Created:      time.Now().Unix(),
		LastActivity: time.Now().Unix(),
		Actualizer: &ActualizeListener{
			MessageChan: make(chan interface{}, 10),
			CloseChan:   make(chan bool, 10),
			IsListening: false,
		},

		LastHandlers: make(map[string]int64),
	}
	// Append new session to map
	// Atomic
	s.Lock()
	defer s.Unlock()

	s.sessions[sessionID] = sess
	return deepcopy.Iface(&sess).(*Session)
}
Beispiel #3
0
// GetAll fetches copy of a map of current active sessions.
func (s *Sessions) GetAll() map[string]Session {
	s.Lock()
	defer s.Unlock()
	return deepcopy.Iface(s.sessions).(map[string]Session)
}