-
Notifications
You must be signed in to change notification settings - Fork 5
/
receipt_parser.go
61 lines (50 loc) · 1.8 KB
/
receipt_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package profile
import (
"crypto/rsa"
"github.com/getyoti/yoti-go-sdk/v3/cryptoutil"
"github.com/getyoti/yoti-go-sdk/v3/util"
"github.com/getyoti/yoti-go-sdk/v3/yotiprotoattr"
"github.com/getyoti/yoti-go-sdk/v3/yotiprotocom"
"google.golang.org/protobuf/proto"
)
func parseApplicationProfile(receipt *receiptDO, key *rsa.PrivateKey) (result *yotiprotoattr.AttributeList, err error) {
decipheredBytes, err := parseEncryptedProto(receipt, receipt.ProfileContent, key)
if err != nil {
return
}
attributeList := &yotiprotoattr.AttributeList{}
if err := proto.Unmarshal(decipheredBytes, attributeList); err != nil {
return nil, err
}
return attributeList, nil
}
func parseUserProfile(receipt *receiptDO, key *rsa.PrivateKey) (result *yotiprotoattr.AttributeList, err error) {
decipheredBytes, err := parseEncryptedProto(receipt, receipt.OtherPartyProfileContent, key)
if err != nil {
return
}
attributeList := &yotiprotoattr.AttributeList{}
if err := proto.Unmarshal(decipheredBytes, attributeList); err != nil {
return nil, err
}
return attributeList, nil
}
func decryptExtraData(receipt *receiptDO, key *rsa.PrivateKey) (result []byte, err error) {
bytes, err := parseEncryptedProto(receipt, receipt.ExtraDataContent, key)
return bytes, err
}
func parseEncryptedProto(receipt *receiptDO, encryptedBase64 string, key *rsa.PrivateKey) (result []byte, err error) {
unwrappedKey, err := cryptoutil.UnwrapKey(receipt.WrappedReceiptKey, key)
if err != nil {
return
}
encryptedBytes, err := util.Base64ToBytes(encryptedBase64)
if err != nil || len(encryptedBytes) == 0 {
return
}
encryptedData := &yotiprotocom.EncryptedData{}
if err = proto.Unmarshal(encryptedBytes, encryptedData); err != nil {
return
}
return cryptoutil.DecipherAes(unwrappedKey, encryptedData.Iv, encryptedData.CipherText)
}