Exemplo n.º 1
0
Arquivo: out.go Projeto: moshee/gas
// Recover will try to recover the reroute info stored in the cookie and decode
// it into dest. If there is no reroute cookie, an error is returned.
func Recover(g *gas.Gas, dest interface{}) error {
	blob := g.Data("_reroute")
	if blob == nil {
		return ErrNoReroute
	}
	dec := gob.NewDecoder(bytes.NewReader(blob.([]byte)))
	return dec.Decode(dest)
}
Exemplo n.º 2
0
Arquivo: auth.go Projeto: moshee/gas
// GetSession figures out the session from the session cookie in the request, or
// just return the session if that's been done already.
func GetSession(g *gas.Gas) (*Session, error) {
	if store == nil {
		return nil, ErrNoStore
	}
	const sessKey = "_gas_session"
	if sessBox := g.Data(sessKey); sessBox != nil {
		if sess, ok := sessBox.(*Session); ok {
			return sess, nil
		}
	}

	cookie, err := g.Cookie("s")
	if err != nil {
		if err == http.ErrNoCookie {
			return nil, nil
		}
		SignOut(g)
		return nil, err
	}

	if err = VerifyCookie(cookie); err != nil {
		return nil, err
	}

	id, err := base64.StdEncoding.DecodeString(cookie.Value)
	if err != nil {
		// this means invalid session
		SignOut(g)
		return nil, err
	}
	//id := []byte(cookie.Value)

	sess, err := store.Read(id)

	if err != nil {
		if err == sql.ErrNoRows {
			SignOut(g)
			return nil, nil
		}
		return nil, err
	}

	if time.Now().After(sess.Expires) {
		SignOut(g)
		return nil, ErrCookieExpired
	}

	g.SetData(sessKey, sess)

	return sess, nil
}