Skip to content

Commit

Permalink
SDK-2235:reduced complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmet-yoti committed Dec 7, 2023
1 parent a4775d2 commit 3da5372
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
1 change: 1 addition & 0 deletions _examples/digitalidentity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func generateSession(w http.ResponseWriter, r *http.Request) {
didClient, err := initialiseDigitalIdentityClient()
if err != nil {
fmt.Fprintf(w, string("Client could't be generated"))
return
}

sessionReq, err := buildDigitalIdentitySessionReq()
Expand Down
2 changes: 1 addition & 1 deletion consts/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package consts

const (
SDKIdentifier = "Go"
SDKVersionIdentifier = "3.12.0"
SDKVersionIdentifier = "3.9.0"
)
2 changes: 1 addition & 1 deletion digitalidentity/requests/signed_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestRequestShouldBuildForValid(t *testing.T) {
assert.Check(t, urlCheck)
assert.Check(t, signed.Header.Get("X-Yoti-Auth-Digest") != "")
assert.Equal(t, signed.Header.Get("X-Yoti-SDK"), "Go")
assert.Equal(t, signed.Header.Get("X-Yoti-SDK-Version"), "3.12.0")
assert.Equal(t, signed.Header.Get("X-Yoti-SDK-Version"), "3.9.0")
}

func TestRequestShouldAddHeaders(t *testing.T) {
Expand Down
64 changes: 42 additions & 22 deletions digitalidentity/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package digitalidentity
import (
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -223,6 +224,10 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk
return receipt, err
}

if err := validateReceiptResponse(receiptResponse); err != nil {
return receipt, err
}

itemKeyId := receiptResponse.WrappedItemKeyId

encryptedItemKeyResponse, err := getReceiptItemKey(httpClient, itemKeyId, clientSdkId, apiUrl, key)
Expand All @@ -235,19 +240,9 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk
return receipt, fmt.Errorf("failed to unwrap receipt content key: %v", err)
}

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

aextra, err := cryptoutil.DecryptReceiptContent(receiptResponse.Content.ExtraData, receiptContentKey)
attrData, aextra, err := decryptReceiptContent(receiptResponse.Content, receiptContentKey)
if err != nil {
return receipt, fmt.Errorf("failed to decrypt receipt content extra data: %v", err)
}

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

applicationProfile := newApplicationProfile(attrData)
Expand All @@ -256,17 +251,9 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk
return receipt, fmt.Errorf("failed to build application extra data: %v", err)
}

uattr, err := cryptoutil.DecryptReceiptContent(receiptResponse.OtherPartyContent.Profile, receiptContentKey)
if err != nil {
return receipt, fmt.Errorf("failed to decrypt other party receipt content profile: %v", err)
}
uextra, err := cryptoutil.DecryptReceiptContent(receiptResponse.OtherPartyContent.ExtraData, receiptContentKey)
uattrData, uextra, err := decryptReceiptContent(receiptResponse.OtherPartyContent, receiptContentKey)
if err != nil {
return receipt, fmt.Errorf("failed to decrypt other party receipt content extra data: %v", err)
}
uattrData := &yotiprotoattr.AttributeList{}
if err := proto.Unmarshal(uattr, uattrData); err != nil {
return receipt, fmt.Errorf("failed to unmarshal attribute list: %v", err)
return receipt, err
}

userProfile := newUserProfile(uattrData)
Expand All @@ -292,3 +279,36 @@ func GetShareReceipt(httpClient requests.HttpClient, receiptId string, clientSdk
Error: receiptResponse.Error,
}, 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)
if err != nil {
return nil, nil, fmt.Errorf("failed to decrypt receipt content profile: %v", err)
}
}

if len(content.ExtraData) > 0 {
aextra, err = cryptoutil.DecryptReceiptContent(content.ExtraData, key)
if err != nil {
return nil, nil, fmt.Errorf("failed to decrypt receipt content extra data: %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)
}
}

return attrData, aextra, nil
}
2 changes: 1 addition & 1 deletion requests/signed_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestRequestShouldBuildForValid(t *testing.T) {
assert.Check(t, urlCheck)
assert.Check(t, signed.Header.Get("X-Yoti-Auth-Digest") != "")
assert.Equal(t, signed.Header.Get("X-Yoti-SDK"), "Go")
assert.Equal(t, signed.Header.Get("X-Yoti-SDK-Version"), "3.12.0")
assert.Equal(t, signed.Header.Get("X-Yoti-SDK-Version"), "3.9.0")
}

func TestRequestShouldAddHeaders(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.organization = getyoti
sonar.projectKey = getyoti:go
sonar.projectName = Go SDK
sonar.projectVersion = 3.12.0
sonar.projectVersion = 3.9.0
sonar.exclusions = **/yotiprotoattr/*.go,**/yotiprotocom/*.go,**/yotiprotoshare/*.go,**/**_test.go,_examples/**/*
sonar.links.scm = https://github.com/getyoti/yoti-go-sdk
sonar.host.url = https://sonarcloud.io
Expand Down

0 comments on commit 3da5372

Please sign in to comment.