Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more meaningful API responses from client library #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions goImplement/goImplement.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ You need only encrypt once to send data anywhere within the ecosystem
@param apiKey the vendor's secret key
@param userKey the user's unique AES encryption key
*/
func Produce(data string, attribute string, supertypeID string, apiKey string, userKey string) error {
func Produce(data string, attribute string, supertypeID string, apiKey string, userKey string) (*ObservationRequest, error) {
// Encrypt data using basic AES encryption
ciphertext, iv, err := Encrypt(data, userKey)
if err != nil {
return err
return nil, err
}

obs := ObservationRequest{
Expand All @@ -37,25 +37,25 @@ func Produce(data string, attribute string, supertypeID string, apiKey string, u
// Produce (upload) data to DynamoDB
requestBody, err := json.Marshal(obs)
if err != nil {
return err
return nil, err
}

client := &http.Client{}
req, err := http.NewRequest("POST", "https://supertype.io/produce", bytes.NewReader(requestBody))
if err != nil {
fmt.Println(err)
return err
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-API-Key", apiKey)
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return err
return nil, err
}
defer resp.Body.Close()

return nil
return &obs, nil
}

/*
Expand All @@ -68,7 +68,7 @@ This data is source-agnostic, and encrypted end-to-end

@return plaintext the decrypted observation the vendor is requesting
*/
func Consume(attribute string, supertypeID string, apiKey string, userKey string) (*string, error) {
func Consume(attribute string, supertypeID string, apiKey string, userKey string) (*ConsumeResponse, error) {
requestBody, err := json.Marshal(map[string]string{
"attribute": attribute,
"supertypeID": supertypeID,
Expand All @@ -78,7 +78,8 @@ func Consume(attribute string, supertypeID string, apiKey string, userKey string
}

client := &http.Client{}
req, err := http.NewRequest("POST", "https://supertype.io/consume", bytes.NewReader(requestBody))
// req, err := http.NewRequest("POST", "https://supertype.io/consume", bytes.NewReader(requestBody))
req, err := http.NewRequest("POST", "http://localhost:5000/consume", bytes.NewReader(requestBody))
if err != nil {
return nil, err
}
Expand All @@ -98,7 +99,15 @@ func Consume(attribute string, supertypeID string, apiKey string, userKey string
var observation ObservationResponse
json.Unmarshal(body, &observation)
plaintext, _, err := Decrypt(observation.Ciphertext, userKey)
return plaintext, nil

response := ConsumeResponse{
Plaintext: *plaintext,
DateAdded: observation.DateAdded,
PublicKey: observation.PublicKey,
SupertypeID: observation.SupertypeID,
}

return &response, nil
}

/*
Expand Down
10 changes: 8 additions & 2 deletions goImplement/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ type ObservationRequest struct {
Attribute string `json:"attribute"`
Ciphertext string `json:"ciphertext"`
SupertypeID string `json:"supertypeID"`
PublicKey string `json:"pk"`
SkHash string `json:"skHash"`
IV string `json:"iv"`
}

Expand All @@ -17,3 +15,11 @@ type ObservationResponse struct {
PublicKey string `json:"pk"`
SupertypeID string `json:"supertypeID"`
}

// ConsumeResponse is the value returned to user
type ConsumeResponse struct {
Plaintext string `json:"plaintext"`
DateAdded string `json:"dateAdded"`
PublicKey string `json:"pk"`
SupertypeID string `json:"supertypeID"`
}