Merge pull request #143 from sosedoff/session-id-for-export

Session id for export
This commit is contained in:
Dan Sosedoff 2016-02-26 12:49:10 -08:00
commit 1d50e2f0bf
6 changed files with 25 additions and 11 deletions

View File

@ -22,7 +22,7 @@ var (
func DB(c *gin.Context) *client.Client {
if command.Opts.Sessions {
return DbSessions[getSessionId(c)]
return DbSessions[getSessionId(c.Request)]
} else {
return DbClient
}
@ -39,7 +39,7 @@ func setClient(c *gin.Context, newClient *client.Client) error {
return nil
}
sessionId := getSessionId(c)
sessionId := getSessionId(c.Request)
if sessionId == "" {
return errors.New("Session ID is required")
}

View File

@ -3,6 +3,7 @@ package api
import (
"fmt"
"mime"
"net/http"
"path/filepath"
"strconv"
"strings"
@ -70,10 +71,10 @@ func desanitize64(query string) string {
return query
}
func getSessionId(c *gin.Context) string {
id := c.Request.Header.Get("x-session-id")
func getSessionId(req *http.Request) string {
id := req.Header.Get("x-session-id")
if id == "" {
id = c.Request.URL.Query().Get("_session_id")
id = req.URL.Query().Get("_session_id")
}
return id
}

View File

@ -1,8 +1,11 @@
package api
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_desanitize64(t *testing.T) {
@ -23,3 +26,13 @@ func Test_cleanQuery(t *testing.T) {
assert.Equal(t, "", cleanQuery("--something"))
assert.Equal(t, "test", cleanQuery("--test\ntest\n -- test\n"))
}
func Test_getSessionId(t *testing.T) {
req := &http.Request{Header: http.Header{}}
req.Header.Add("x-session-id", "token")
assert.Equal(t, "token", getSessionId(req))
req = &http.Request{}
req.URL, _ = url.Parse("http://foobar/?_session_id=token")
assert.Equal(t, "token", getSessionId(req))
}

View File

@ -32,7 +32,7 @@ func dbCheckMiddleware() gin.HandlerFunc {
return
}
sessionId := getSessionId(c)
sessionId := getSessionId(c.Request)
if sessionId == "" {
c.JSON(400, Error{"Session ID is required"})
c.Abort()

File diff suppressed because one or more lines are too long

View File

@ -211,7 +211,7 @@ function performTableAction(table, action, el) {
var format = el.data("format");
var filename = table + "." + format;
var query = window.encodeURI("SELECT * FROM " + table);
var url = "http://" + window.location.host + "/api/query?format=" + format + "&filename=" + filename + "&query=" + query;
var url = "http://" + window.location.host + "/api/query?format=" + format + "&filename=" + filename + "&query=" + query + "&_session_id=" + getSessionId();
var win = window.open(url, "_blank");
win.focus();
break;
@ -542,7 +542,7 @@ function exportTo(format) {
return;
}
var url = "http://" + window.location.host + "/api/query?format=" + format + "&query=" + encodeQuery(query);
var url = "http://" + window.location.host + "/api/query?format=" + format + "&query=" + encodeQuery(query) + "&_session_id=" + getSessionId();
var win = window.open(url, '_blank');
setCurrentTab("table_query");