Ejemplo n.º 1
0
func Parse(r *http.Request) (*Session, error) {
	c, err := r.Cookie(config.Get("session_cookie_name"))
	if err != nil {
		return nil, err
	}

	q := `
        SELECT
            id
            , key
        FROM user_session s
        WHERE s.key = ?
        AND s.valid_until > ?;
    `

	params := []interface{}{
		c.Value,
		time.Now().Unix(),
	}

	s := new(Session)

	bind := []interface{}{
		&s.Id,
		&s.Key,
	}

	err = dao.Row(q, params, bind)
	if err != nil {
		return nil, err
	}

	return s, nil
}
Ejemplo n.º 2
0
func Verify(vkey string) (*Session, error) {
	// Get id
	q := `
        SELECT
            id
        FROM user_verify
        WHERE key = ?
        AND valid_until > ?;
    `

	params := []interface{}{
		vkey,
		time.Now().Unix(),
	}

	var vid int64

	bind := []interface{}{
		&vid,
	}

	err := dao.Row(q, params, bind)
	if err != nil {
		return nil, err
	}

	// De-activate
	q = `
        UPDATE user_verify SET
            valid_until = 0
            , modified_date = ?
        WHERE id = ?;
    `

	params = []interface{}{
		time.Now().Unix(),
		vid,
	}

	_, err = dao.Exec(q, params)
	if err != nil {
		return nil, err
	}

	return New()
}