-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpresentation_parser.go
208 lines (167 loc) · 5.32 KB
/
presentation_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/samber/lo"
"github.com/veraison/go-cose"
"github.com/trustbloc/vc-go/jwt"
)
type parsePresentationResponse struct {
VPDataDecoded []byte
VPRaw rawPresentation
VPJwt string
VPCwt *VpCWT
}
// PresentationParser is an interface for parsing presentations.
type PresentationParser interface {
parse(vpData []byte, vpOpts *presentationOpts) (*parsePresentationResponse, error)
}
// PresentationJSONParser is a parser for JSON presentations.
type PresentationJSONParser struct {
}
//nolint:funlen,gocyclo // Old function
func (p *PresentationJSONParser) parse(vpData []byte, vpOpts *presentationOpts) (*parsePresentationResponse, error) {
vpStr := string(unQuote(vpData))
if jwt.IsJWS(vpStr) {
if !vpOpts.disabledProofCheck && vpOpts.proofChecker == nil {
return nil, errors.New("proof checker is not defined")
}
proofChecker := vpOpts.proofChecker
if vpOpts.disabledProofCheck {
proofChecker = nil
}
vcDataFromJwt, rawCred, err := decodeVPFromJWS(vpStr, proofChecker)
if err != nil {
return nil, fmt.Errorf("decoding of Verifiable Presentation from JWS: %w", err)
}
return &parsePresentationResponse{
VPDataDecoded: vcDataFromJwt,
VPRaw: rawCred,
VPJwt: vpStr,
}, nil
}
embeddedProofCheckOpts := &embeddedProofCheckOpts{
dataIntegrityOpts: vpOpts.verifyDataIntegrity,
proofChecker: vpOpts.proofChecker,
disabledProofCheck: vpOpts.disabledProofCheck,
jsonldCredentialOpts: vpOpts.jsonldCredentialOpts,
}
if jwt.IsJWTUnsecured(vpStr) {
rawBytes, rawPres, err := decodeVPFromUnsecuredJWT(vpStr)
if err != nil {
return nil, fmt.Errorf("decoding of Verifiable Presentation from unsecured JWT: %w", err)
}
if err := checkEmbeddedProofBytes(rawBytes, nil, embeddedProofCheckOpts); err != nil {
return nil, err
}
return &parsePresentationResponse{
VPDataDecoded: rawBytes,
VPRaw: rawPres,
VPJwt: "",
}, nil
}
vpRaw, err := decodeVPFromJSON(vpData)
if err != nil {
return nil, err
}
err = checkEmbeddedProofBytes(vpData, nil, embeddedProofCheckOpts)
if err != nil {
return nil, err
}
// check that embedded proof is present, if not, it's not a verifiable presentation
if vpOpts.requireProof && vpRaw[vpFldProof] == nil {
return nil, errors.New("embedded proof is missing")
}
return &parsePresentationResponse{
VPDataDecoded: vpData,
VPRaw: vpRaw,
VPJwt: "",
}, nil
}
type VpCWT struct {
Raw []byte
Message *cose.Sign1Message
VPMap map[string]interface{}
}
// PresentationCWTParser is a parser for CWT presentations.
type PresentationCWTParser struct {
}
func (p *PresentationCWTParser) parse(vpData []byte, _ *presentationOpts) (*parsePresentationResponse, error) {
var rawErr, hexRawErr, hexErr error
// todo proof checker !!
message, rawErr := p.parsePres(vpData)
if rawErr != nil {
vpData = unQuote(vpData)
vpData, hexErr = hex.DecodeString(string(vpData))
if hexErr != nil {
return nil, errors.Join(errors.New("vpData is not a valid hex string"), hexErr)
}
message, hexRawErr = p.parsePres(vpData)
if hexRawErr != nil {
return nil, errors.Join(errors.New("unmarshal cbor vp after hex failed"), hexRawErr)
}
}
if message == nil {
return nil, errors.Join(errors.New("parsed vp cbor message is nil"), rawErr, hexRawErr, hexErr)
}
var vpMap map[interface{}]interface{}
if err := cbor.Unmarshal(message.Payload, &vpMap); err != nil {
return nil, fmt.Errorf("unmarshal cbor vp payload: %w", err)
}
convertedMap := convertToStringMap(vpMap)
vpContent, ok := convertedMap["vp"].(map[string]interface{})
if !ok {
// do nothing
}
return &parsePresentationResponse{
VPDataDecoded: vpData,
VPRaw: vpContent,
VPJwt: "",
VPCwt: &VpCWT{
Raw: vpData,
Message: message,
VPMap: convertedMap,
},
}, nil
}
func (p *PresentationCWTParser) parsePres(data []byte) (*cose.Sign1Message, error) {
var message cose.Sign1Message
if err := cbor.Unmarshal(data, &message); err != nil {
return nil, fmt.Errorf("unmarshal cbor credential: %w", err)
}
return &message, nil
}
// presentationEnvelopedParser is a parser for presentations of type, EnvelopedVerifiablePresentation.
type presentationEnvelopedParser struct {
}
func (p *presentationEnvelopedParser) parse(vpData []byte, vpOpts *presentationOpts,
) (*parsePresentationResponse, error) {
vpEnveloped := &Envelope{}
if err := json.Unmarshal(vpData, vpEnveloped); err != nil {
return nil, fmt.Errorf("unmarshal envelopedCredential: %w", err)
}
if !lo.Contains(vpEnveloped.Type, VPEnvelopedType) {
return nil, errors.New("not a verifiable presentation envelopedCredential")
}
mediaType, _, data, err := ParseDataURL(vpEnveloped.ID)
if err != nil {
return nil, fmt.Errorf("enveloped presentation ID is not a valid data URL: %w", err)
}
switch mediaType {
case VPMediaTypeJWT:
parser := &PresentationJSONParser{}
return parser.parse([]byte(data), vpOpts)
case VPMediaTypeCOSE:
parser := &PresentationCWTParser{}
return parser.parse([]byte(data), vpOpts)
default:
return nil, fmt.Errorf("unsupported media type for enveloped presentation: %s", mediaType)
}
}