storj/cmd/storj-sim/console.go
Yingrong Zhao 6778caf846
satellite/console: add referral link logic (#2576)
* setup referral route

* referredBy

* add user id

* modify user query

* separate optional field from userInfo

* get current reward on init of satellite gui

* remove unsed code

* fix format

* only apply 0 credit on registration

* only pass required information for rewards

* fix time parsing

* fix test and linter

* rename method

* add todo

* remove user referral logic

* add null check and fix format

* get current offer

* remove partnerID on CreateUser struct

* fix storj-sim user creation

* only redeem credit when there's an offer

* fix default offer configuration

* fix migration

* Add helper function for get correct credit duration

* add comment

* only store userid into user_credit table

* add check for partner id to set correct offer type

* change free credit to use invitee credits

* remove unecessary code
2019-07-19 14:22:10 -04:00

273 lines
5.3 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"storj.io/storj/satellite/console/consoleauth"
)
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)
}
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
}
func addExampleProjectWithKey(key *string, createRegistrationTokenAddress, activationAddress, address string) error {
client := http.Client{}
var createTokenResponse struct {
Secret string
Error string
}
{
request, err := http.NewRequest(
http.MethodGet,
createRegistrationTokenAddress,
nil)
if err != nil {
return err
}
request.Header.Set("Authorization", "secure_token")
resp, err := client.Do(request)
if err != nil {
return err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err = json.NewDecoder(bytes.NewReader(b)).Decode(&createTokenResponse); err != nil {
return err
}
if createTokenResponse.Error != "" {
return errs.New(createTokenResponse.Error)
}
err = resp.Body.Close()
if err != nil {
return err
}
}
// create user
var user struct {
CreateUser struct {
Email string
CreatedAt time.Time
ID string
}
}
{
createUserQuery := fmt.Sprintf(
"mutation {createUser(input:{email:\"%s\",password:\"%s\",fullName:\"%s\", shortName:\"\", partnerId: \"\"}, secret:\"%s\" ){id,email,createdAt}}",
"alice@mail.test",
"123a123",
"Alice",
createTokenResponse.Secret)
request, err := http.NewRequest(
http.MethodPost,
address,
bytes.NewReader([]byte(createUserQuery)))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/graphql")
err = graphqlDo(&client, request, &user)
if err != nil {
return err
}
}
var token struct {
Token struct {
Token string
}
}
{
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
}
request, err := http.NewRequest(
http.MethodGet,
activationAddress+activationToken,
nil)
if err != nil {
return err
}
resp, err := client.Do(request)
if err != nil {
return err
}
err = resp.Body.Close()
if err != nil {
return err
}
tokenQuery := fmt.Sprintf(
"query {token(email:\"%s\",password:\"%s\"){token}}",
"alice@mail.test",
"123a123")
request, err = http.NewRequest(
http.MethodPost,
address,
bytes.NewReader([]byte(tokenQuery)))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/graphql")
err = graphqlDo(&client, request, &token)
if err != nil {
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")
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.Token.Token))
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")
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.Token.Token))
if err := graphqlDo(&client, request, &createAPIKey); err != nil {
return err
}
}
// return key to the caller
*key = createAPIKey.CreateAPIKey.Key
return nil
}