示例#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
}
示例#2
0
func (sh *SessionHolder) ResetTTL(c web.C, session *base.Session) error {
	sessionId := session.Id()
	conn := c.Env["redis"].(redigo.Conn)
	_, err := conn.Do("EXPIRE", sessionKey(sessionId), sh.Timeout)

	return err
}
示例#3
0
/*
Destroy deletes a session from redis
*/
func (sh *SessionHolder) Destroy(c web.C, session *base.Session) error {
	sessionId := session.Id()
	delete(c.Env, "session")
	conn := c.Env["redis"].(redigo.Conn)

	_, err := conn.Do("DEL", sessionKey(sessionId))
	return err
}
示例#4
0
/*
Save a session to postgres
*/
func (sh *SessionHolder) Save(c web.C, session *base.Session) error {
	sessionId := session.Id()

	// There is a potential race here if the insert fails because the session exists, but it is deleted
	// before we can update it.
	_, err := sh.db.Exec("INSERT INTO sessions (id, content, expires) VALUES ($1, $2, now() + $3 * interval '1 second')", sessionId, sessionValues(session.Values), sh.Timeout)
	if err != nil && isAlreadyExists(err, "sessions") {
		_, err = sh.db.Exec("UPDATE sessions SET content=$2, expires=now() + $3 * interval '1 second' WHERE id=$1", sessionId, sessionValues(session.Values), sh.Timeout)
	}
	return err
}
示例#5
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
}
示例#6
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
}
示例#7
0
/*
Save a session to redis
*/
func (sh *SessionHolder) Save(c web.C, session *base.Session) error {
	sessionId := session.Id()
	conn := c.Env["redis"].(redigo.Conn)

	var b bytes.Buffer
	enc := gob.NewEncoder(&b)

	err := enc.Encode(session)
	if err != nil {
		return err
	}

	_, err = conn.Do("SET", sessionKey(sessionId), b.String(), "EX", sh.Timeout)

	return err
}
示例#8
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
}
示例#9
0
/*
Destroy deletes a session from postgres
*/
func (sh *SessionHolder) Destroy(c web.C, session *base.Session) error {
	sessionId := session.Id()
	delete(c.Env, "session")
	_, err := sh.db.Exec("DELETE FROM sessions WHERE id=$1", sessionId)
	return err
}
示例#10
0
func (sh *SessionHolder) ResetTTL(c web.C, session *base.Session) error {
	// Need to implement a TTL...
	_, err := sh.db.Exec("UPDATE sessions SET expires=now()+$2 * interval '1 second' WHERE id=$1", session.Id(), sh.Timeout)
	return err
}