Skip to content

Commit

Permalink
SDK-2235 removed unused method, added panic controls, added error con…
Browse files Browse the repository at this point in the history
…texts
  • Loading branch information
mehmet-yoti committed Dec 12, 2023
1 parent 3da5372 commit 9d403b8
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions digitalidentity/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package digitalidentity
import (
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -221,18 +220,14 @@ func getReceiptItemKey(httpClient requests.HttpClient, receiptItemKeyId string,
func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdkId, apiUrl string, key *rsa.PrivateKey) (receipt SharedReceiptResponse, err error) {
receiptResponse, err := getReceipt(httpClient, receiptId, clientSdkId, apiUrl, key)
if err != nil {
return receipt, err
}

if err := validateReceiptResponse(receiptResponse); err != nil {
return receipt, err
return receipt, fmt.Errorf("failed to unmarshal attribute list: %v", err)
}

itemKeyId := receiptResponse.WrappedItemKeyId

encryptedItemKeyResponse, err := getReceiptItemKey(httpClient, itemKeyId, clientSdkId, apiUrl, key)
if err != nil {
return receipt, err
return receipt, fmt.Errorf("failed to unmarshal attribute list: %v", err)
}

receiptContentKey, err := cryptoutil.UnwrapReceiptKey(receiptResponse.WrappedKey, encryptedItemKeyResponse.Value, encryptedItemKeyResponse.Iv, key)
Expand All @@ -242,7 +237,7 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk

attrData, aextra, err := decryptReceiptContent(receiptResponse.Content, receiptContentKey)
if err != nil {
return receipt, err
return receipt, fmt.Errorf("failed to unmarshal attribute list: %v", err)
}

applicationProfile := newApplicationProfile(attrData)
Expand All @@ -253,7 +248,7 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk

uattrData, uextra, err := decryptReceiptContent(receiptResponse.OtherPartyContent, receiptContentKey)
if err != nil {
return receipt, err
return receipt, fmt.Errorf("failed to unmarshal attribute list: %v", err)
}

userProfile := newUserProfile(uattrData)
Expand All @@ -280,21 +275,19 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk
}, nil
}

func validateReceiptResponse(receiptResponse ReceiptResponse) error {
if receiptResponse.Content == nil || len(receiptResponse.Content.Profile) == 0 {
return errors.New("received unexpectedly empty content or profile")
}
return nil
}

func decryptReceiptContent(content *Content, key []byte) (attrData *yotiprotoattr.AttributeList, aextra []byte, err error) {
aattr := []byte{}

if content != nil {
if len(content.Profile) > 0 {
aattr, err = cryptoutil.DecryptReceiptContent(content.Profile, key)
aattr, err := cryptoutil.DecryptReceiptContent(content.Profile, key)
if err != nil {
return nil, nil, fmt.Errorf("failed to decrypt receipt content profile: %v", err)
}

attrData = &yotiprotoattr.AttributeList{}
if err := proto.Unmarshal(aattr, attrData); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal attribute list: %v", err)
}
}

if len(content.ExtraData) > 0 {
Expand All @@ -304,10 +297,6 @@ func decryptReceiptContent(content *Content, key []byte) (attrData *yotiprotoatt
}
}

attrData = &yotiprotoattr.AttributeList{}
if err := proto.Unmarshal(aattr, attrData); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal attribute list: %v", err)
}
}

return attrData, aextra, nil
Expand Down

0 comments on commit 9d403b8

Please sign in to comment.