2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-15 12:00:08 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleweb
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console/consoleweb/consoleql"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2018-11-16 14:34:35 +00:00
|
|
|
// JSON request from graphql clients
|
|
|
|
type graphqlJSON struct {
|
|
|
|
Query string
|
|
|
|
OperationName string
|
|
|
|
Variables map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
// getToken retrieves token from request
|
|
|
|
func getToken(req *http.Request) string {
|
|
|
|
value := req.Header.Get(authorization)
|
|
|
|
if value == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(value, authorizationBearer) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return value[len(authorizationBearer):]
|
|
|
|
}
|
|
|
|
|
|
|
|
// getQuery retrieves graphql query from request
|
2018-11-16 14:34:35 +00:00
|
|
|
func getQuery(req *http.Request) (query graphqlJSON, err error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
2019-01-15 13:03:24 +00:00
|
|
|
query.Query = req.URL.Query().Get(consoleql.Query)
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
case http.MethodPost:
|
|
|
|
return queryPOST(req)
|
|
|
|
default:
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, errs.New("wrong http request type")
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 14:34:35 +00:00
|
|
|
// queryPOST retrieves graphql query from POST request
|
|
|
|
func queryPOST(req *http.Request) (query graphqlJSON, err error) {
|
2018-11-14 10:50:15 +00:00
|
|
|
switch typ := req.Header.Get(contentType); typ {
|
|
|
|
case applicationGraphql:
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
2018-11-16 14:34:35 +00:00
|
|
|
query.Query = string(body)
|
2019-03-29 12:30:23 +00:00
|
|
|
return query, errs.Combine(err, req.Body.Close())
|
2018-11-14 10:50:15 +00:00
|
|
|
case applicationJSON:
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&query)
|
2019-03-29 12:30:23 +00:00
|
|
|
return query, errs.Combine(err, req.Body.Close())
|
2018-11-14 10:50:15 +00:00
|
|
|
default:
|
2018-11-16 14:34:35 +00:00
|
|
|
return query, errs.New("can't parse request body of type %s", typ)
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
}
|