2019-02-05 17:22:17 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-11 10:33:56 +00:00
|
|
|
"encoding/base64"
|
2019-02-05 17:22:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-02-11 10:33:56 +00:00
|
|
|
"time"
|
2019-02-05 17:22:17 +00:00
|
|
|
|
2019-02-11 10:33:56 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-02-05 17:22:17 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-02-11 10:33:56 +00:00
|
|
|
|
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
2019-02-05 17:22:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func graphqlDo(client *http.Client, request *http.Request, jsonResponse interface{}) error {
|
|
|
|
resp, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = resp.Body.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response struct {
|
|
|
|
Data json.RawMessage
|
|
|
|
Errors []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.NewDecoder(bytes.NewReader(b)).Decode(&response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Errors != nil {
|
|
|
|
return errs.New("inner graphql error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsonResponse == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewDecoder(bytes.NewReader(response.Data)).Decode(jsonResponse)
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:33:56 +00:00
|
|
|
func generateActivationKey(userID uuid.UUID, email string, createdAt time.Time) (string, error) {
|
|
|
|
claims := consoleauth.Claims{
|
|
|
|
ID: userID,
|
|
|
|
Email: email,
|
|
|
|
Expiration: createdAt.Add(24 * time.Hour),
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: change it in future, when satellite/console secret will be changed
|
|
|
|
signer := &consoleauth.Hmac{Secret: []byte("my-suppa-secret-key")}
|
|
|
|
|
|
|
|
json, err := claims.JSON()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
token := consoleauth.Token{Payload: json}
|
|
|
|
encoded := base64.URLEncoding.EncodeToString(token.Payload)
|
|
|
|
|
|
|
|
signature, err := signer.Sign([]byte(encoded))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
token.Signature = signature
|
|
|
|
|
|
|
|
return token.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
func addExampleProjectWithKey(key *string, address string) error {
|
|
|
|
client := http.Client{}
|
|
|
|
|
|
|
|
// create user
|
2019-02-11 10:33:56 +00:00
|
|
|
var user struct {
|
|
|
|
CreateUser struct {
|
|
|
|
Email string
|
|
|
|
CreatedAt time.Time
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 17:22:17 +00:00
|
|
|
{
|
|
|
|
createUserQuery := fmt.Sprintf(
|
2019-02-11 10:33:56 +00:00
|
|
|
"mutation {createUser(input:{email:\"%s\",password:\"%s\",firstName:\"%s\",lastName:\"\"}){id,email,createdAt}}",
|
2019-02-05 17:22:17 +00:00
|
|
|
"example@mail.com",
|
|
|
|
"123a123",
|
|
|
|
"Alice")
|
|
|
|
|
|
|
|
request, err := http.NewRequest(
|
|
|
|
http.MethodPost,
|
|
|
|
address,
|
|
|
|
bytes.NewReader([]byte(createUserQuery)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/graphql")
|
|
|
|
|
2019-02-11 10:33:56 +00:00
|
|
|
err = graphqlDo(&client, request, &user)
|
|
|
|
if err != nil {
|
2019-02-05 17:22:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var token struct {
|
2019-02-11 10:33:56 +00:00
|
|
|
ActivateAccount string
|
2019-02-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
{
|
2019-02-11 10:33:56 +00:00
|
|
|
userID, err := uuid.Parse(user.CreateUser.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activationToken, err := generateActivationKey(*userID, user.CreateUser.Email, user.CreateUser.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
activateAccountQuery := fmt.Sprintf(
|
|
|
|
"mutation {activateAccount(input:\"%s\")}",
|
|
|
|
activationToken)
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
request, err := http.NewRequest(
|
|
|
|
http.MethodPost,
|
|
|
|
address,
|
2019-02-11 10:33:56 +00:00
|
|
|
bytes.NewReader([]byte(activateAccountQuery)))
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/graphql")
|
|
|
|
|
2019-02-11 10:33:56 +00:00
|
|
|
err = graphqlDo(&client, request, &token)
|
|
|
|
if err != nil {
|
2019-02-05 17:22:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create project
|
|
|
|
var createProject struct {
|
|
|
|
CreateProject struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
createProjectQuery := fmt.Sprintf(
|
|
|
|
"mutation {createProject(input:{name:\"%s\",description:\"\"}){id}}",
|
|
|
|
"TestProject")
|
|
|
|
|
|
|
|
request, err := http.NewRequest(
|
|
|
|
http.MethodPost,
|
|
|
|
address,
|
|
|
|
bytes.NewReader([]byte(createProjectQuery)))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/graphql")
|
2019-02-11 10:33:56 +00:00
|
|
|
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.ActivateAccount))
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
if err := graphqlDo(&client, request, &createProject); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create api key
|
|
|
|
var createAPIKey struct {
|
|
|
|
CreateAPIKey struct {
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
createAPIKeyQuery := fmt.Sprintf(
|
|
|
|
"mutation {createAPIKey(projectID:\"%s\",name:\"%s\"){key}}",
|
|
|
|
createProject.CreateProject.ID,
|
|
|
|
"testKey")
|
|
|
|
|
|
|
|
request, err := http.NewRequest(
|
|
|
|
http.MethodPost,
|
|
|
|
address,
|
|
|
|
bytes.NewReader([]byte(createAPIKeyQuery)))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/graphql")
|
2019-02-11 10:33:56 +00:00
|
|
|
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.ActivateAccount))
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
if err := graphqlDo(&client, request, &createAPIKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return key to the caller
|
|
|
|
*key = createAPIKey.CreateAPIKey.Key
|
|
|
|
return nil
|
|
|
|
}
|