Exemplo n.º 1
0
/*
Get the session for this request from Redis
*/
func (sh *SessionHolder) Get(c web.C, r *http.Request) (*base.Session, error) {
	sessionId := sh.GetSessionId(r)
	if sessionId == "" {
		return nil, base.ErrorSessionNotFound
	}

	conn := c.Env["redis"].(redigo.Conn)

	sess, err := conn.Do("GET", sessionKey(sessionId))
	if err != nil {
		return nil, err
	}
	if sess == nil {
		return nil, base.ErrorSessionNotFound
	}
	sessionBytes, err := redigo.Bytes(sess, err)
	if err != nil {
		return nil, err
	}

	dec := gob.NewDecoder(bytes.NewReader(sessionBytes))
	var session base.Session
	err = dec.Decode(&session)
	if err == nil {
		session.SetId(sessionId)
	}

	return &session, err
}
Exemplo n.º 2
0
func (sh *SessionHolder) RegenerateId(c web.C, session *base.Session) (string, error) {
	sessionId := session.Id()
	newSessionId := sh.GenerateSessionId()
	conn := c.Env["redis"].(redigo.Conn)
	_, err := conn.Do("RENAME", sessionKey(sessionId), sessionKey(newSessionId))

	if err == nil {
		/* renaming of session key succeeded in backend... update sessionId in our struct */
		session.SetId(newSessionId)
	} else {
		newSessionId = sessionId
	}
	return newSessionId, err
}
Exemplo n.º 3
0
func (sh *SessionHolder) RegenerateId(c web.C, session *base.Session) (string, error) {
	sessionId := session.Id()
	newSessionId := sh.GenerateSessionId()

	_, err := sh.db.Exec("UPDATE sessions SET id=$2 WHERE id=$1", sessionId, newSessionId)

	if err == nil {
		// This all worked, use the new session Id
		session.SetId(newSessionId)
	} else {
		// Move back to the old session Id.  We hope this is a transitory error...
		newSessionId = sessionId
	}
	return newSessionId, err
}
Exemplo n.º 4
0
/*
Get the session for this request from Postgres
*/
func (sh *SessionHolder) Get(c web.C, r *http.Request) (*base.Session, error) {
	sessionId := sh.GetSessionId(r)
	if sessionId == "" {
		return nil, base.ErrorSessionNotFound
	}

	var session base.Session
	values := sessionValues{}

	err := sh.db.QueryRow("SELECT content FROM sessions WHERE id=$1", sessionId).Scan(values)
	if err == nil {
		session.Values = values
		session.SetId(sessionId)
	} else if err == sql.ErrNoRows {
		err = base.ErrorSessionNotFound
	}

	return &session, err
}