This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
processor.go
495 lines (384 loc) · 12.6 KB
/
processor.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package processor
import (
"errors"
"fmt"
"strings"
"github.com/google/uuid"
"github.com/piprate/json-gold/ld"
"github.com/hyperledger/aries-framework-go/component/log"
"github.com/hyperledger/aries-framework-go/component/models/util/maphelpers"
)
const (
format = "application/n-quads"
defaultAlgorithm = "URDNA2015"
handleNormalizeErr = "error while parsing N-Quads; invalid quad. line:"
)
var logger = log.New("aries-framework/json-ld-processor")
// ErrInvalidRDFFound is returned when normalized view contains invalid RDF.
var ErrInvalidRDFFound = errors.New("invalid JSON-LD context")
// processorOpts holds options for canonicalization of JSON LD docs.
type processorOpts struct {
removeInvalidRDF bool
frameBlankNodes bool
validateRDF bool
documentLoader ld.DocumentLoader
externalContexts []string
}
// Opts are the options for JSON LD operations on docs (like canonicalization or compacting).
type Opts func(opts *processorOpts)
// WithRemoveAllInvalidRDF option for removing all invalid RDF dataset from normalize document.
func WithRemoveAllInvalidRDF() Opts {
return func(opts *processorOpts) {
opts.removeInvalidRDF = true
}
}
// WithFrameBlankNodes option for transforming blank node identifiers into nodes.
// For example, _:c14n0 is transformed into <urn:bnid:_:c14n0>.
func WithFrameBlankNodes() Opts {
return func(opts *processorOpts) {
opts.frameBlankNodes = true
}
}
// WithDocumentLoader option is for passing custom JSON-LD document loader.
func WithDocumentLoader(loader ld.DocumentLoader) Opts {
return func(opts *processorOpts) {
opts.documentLoader = loader
}
}
// WithExternalContext option is for definition of external context when doing JSON-LD operations.
func WithExternalContext(context ...string) Opts {
return func(opts *processorOpts) {
opts.externalContexts = context
}
}
// WithValidateRDF option validates result view and fails if any invalid RDF dataset found.
// This option will take precedence when used in conjunction with 'WithRemoveAllInvalidRDF' option.
func WithValidateRDF() Opts {
return func(opts *processorOpts) {
opts.validateRDF = true
}
}
// Processor is JSON-LD processor for aries.
// processing mode JSON-LD 1.0 {RFC: https://www.w3.org/TR/2014/REC-json-ld-20140116}
type Processor struct {
algorithm string
}
// NewProcessor returns new JSON-LD processor for aries.
func NewProcessor(algorithm string) *Processor {
if algorithm == "" {
return Default()
}
return &Processor{algorithm}
}
// Default returns new JSON-LD processor with default RDF dataset algorithm.
func Default() *Processor {
return &Processor{defaultAlgorithm}
}
// GetCanonicalDocument returns canonized document of given json ld.
func (p *Processor) GetCanonicalDocument(doc map[string]interface{}, opts ...Opts) ([]byte, error) {
procOptions := prepareOpts(opts)
ldOptions := ld.NewJsonLdOptions("")
ldOptions.ProcessingMode = ld.JsonLd_1_1
ldOptions.Algorithm = p.algorithm
ldOptions.Format = format
ldOptions.ProduceGeneralizedRdf = true
ldOptions.DocumentLoader = procOptions.documentLoader
if len(procOptions.externalContexts) > 0 {
doc["@context"] = AppendExternalContexts(doc["@context"], procOptions.externalContexts...)
}
proc := ld.NewJsonLdProcessor()
view, err := proc.Normalize(doc, ldOptions)
if err != nil {
return nil, fmt.Errorf("failed to normalize JSON-LD document: %w", err)
}
result, ok := view.(string)
if !ok {
return nil, fmt.Errorf("failed to normalize JSON-LD document, invalid view")
}
result, err = p.removeMatchingInvalidRDFs(result, procOptions)
if err != nil {
return nil, err
}
return []byte(result), nil
}
// AppendExternalContexts appends external context(s) to the JSON-LD context which can have one
// or several contexts already.
func AppendExternalContexts(context interface{}, extraContexts ...string) []interface{} {
var contexts []interface{}
switch c := context.(type) {
case string:
contexts = append(contexts, c)
case []interface{}:
contexts = append(contexts, c...)
}
for i := range extraContexts {
contexts = append(contexts, extraContexts[i])
}
return contexts
}
// Compact compacts given json ld object.
func (p *Processor) Compact(input, context map[string]interface{},
opts ...Opts) (map[string]interface{}, error) {
procOptions := prepareOpts(opts)
ldOptions := ld.NewJsonLdOptions("")
ldOptions.ProcessingMode = ld.JsonLd_1_1
ldOptions.Format = format
ldOptions.ProduceGeneralizedRdf = true
ldOptions.DocumentLoader = procOptions.documentLoader
if context == nil {
inputContext := input["@context"]
if len(procOptions.externalContexts) > 0 {
inputContext = AppendExternalContexts(inputContext, procOptions.externalContexts...)
input["@context"] = inputContext
}
context = map[string]interface{}{"@context": inputContext}
}
return ld.NewJsonLdProcessor().Compact(input, context, ldOptions)
}
// Frame makes a frame from the inputDoc using frameDoc.
func (p *Processor) Frame(inputDoc map[string]interface{}, frameDoc map[string]interface{},
opts ...Opts) (map[string]interface{}, error) {
procOptions := prepareOpts(opts)
ldOptions := ld.NewJsonLdOptions("")
ldOptions.ProcessingMode = ld.JsonLd_1_1
ldOptions.Format = format
ldOptions.ProduceGeneralizedRdf = true
ldOptions.OmitGraph = true
ldOptions.DocumentLoader = procOptions.documentLoader
proc := ld.NewJsonLdProcessor()
hasBlankBaseID := false
if checkID, ok := inputDoc["id"]; !ok || checkID == "" {
hasBlankBaseID = true
inputDoc["id"] = fmt.Sprintf("urn:uuid:%s", uuid.New().String())
frameDoc["id"] = inputDoc["id"]
}
// TODO Drop replacing duplicated IDs as soon as https://github.com/piprate/json-gold/issues/44 will be fixed.
inputDocCopy, randomIds, err := removeDuplicateIDs(inputDoc, proc, ldOptions)
if err != nil {
return nil, fmt.Errorf("removing duplicate ids failed: %w", err)
}
inputDocCopy, err = p.transformBlankNodes(inputDocCopy, opts...)
if err != nil {
return nil, fmt.Errorf("transforming frame input failed: %w", err)
}
framedInputDoc, err := proc.Frame(inputDocCopy, frameDoc, ldOptions)
if err != nil {
return nil, fmt.Errorf("framing failed: %w", err)
}
framedInputDoc["@context"] = frameDoc["@context"]
if hasBlankBaseID {
delete(framedInputDoc, "id")
delete(frameDoc, "id")
}
// TODO Drop replacing duplicated IDs as soon as https://github.com/piprate/json-gold/issues/44 will be fixed.
if len(randomIds) == 0 {
return framedInputDoc, nil
}
visitJSONMap(framedInputDoc, func(key, val string) (string, bool) {
v, ok := randomIds[val]
return v, ok
})
return framedInputDoc, nil
}
func removeDuplicateIDs(inputDoc map[string]interface{}, proc *ld.JsonLdProcessor,
options *ld.JsonLdOptions) (map[string]interface{}, map[string]string, error) {
duplicatedIDs, err := getDuplicatedIDs(inputDoc, proc, options)
if err != nil {
return nil, nil, fmt.Errorf("get duplicated ids: %w", err)
}
var inputDocCopy map[string]interface{}
var randomIds map[string]string
if len(duplicatedIDs) > 0 {
inputDocCopy = maphelpers.CopyMap(inputDoc)
randomIds = make(map[string]string)
visitJSONMap(inputDocCopy, func(key, id string) (string, bool) {
if _, ok := duplicatedIDs[id]; !ok {
return "", false
}
randomID := fmt.Sprintf("urn:uuid:%s", uuid.New().String())
randomIds[randomID] = id
return randomID, true
})
} else {
inputDocCopy = inputDoc
}
return inputDocCopy, randomIds, nil
}
func getDuplicatedIDs(doc map[string]interface{}, proc *ld.JsonLdProcessor, options *ld.JsonLdOptions) (
map[string]bool, error) {
expand, err := proc.Expand(doc, options)
if err != nil {
return nil, err
}
ids := make(map[string]bool)
visitJSONArray(expand, func(key, val string) (string, bool) {
if key == "@id" {
if _, ok := ids[val]; ok {
ids[val] = true
} else {
ids[val] = false
}
}
return "", false
})
for k, v := range ids {
if !v {
delete(ids, k)
}
}
return ids, nil
}
func visitJSONArray(a []interface{}, visitFunc func(key, val string) (string, bool)) {
for i, v := range a {
switch kv := v.(type) {
case string:
if newValue, ok := visitFunc("", kv); ok {
a[i] = newValue
}
case []interface{}:
visitJSONArray(kv, visitFunc)
case map[string]interface{}:
visitJSONMap(kv, visitFunc)
}
}
}
func visitJSONMap(m map[string]interface{}, visitFunc func(key, val string) (string, bool)) {
for k, v := range m {
switch kv := v.(type) {
case string:
if newID, ok := visitFunc(k, kv); ok {
m[k] = newID
}
case []interface{}:
visitJSONArray(kv, visitFunc)
case map[string]interface{}:
visitJSONMap(kv, visitFunc)
}
}
}
// removeMatchingInvalidRDFs validates normalized view to find any invalid RDF and
// returns filtered view after removing all invalid data except the ones given in rdfMatches argument.
// [Note : handling invalid RDF data, by following pattern https://github.com/digitalbazaar/jsonld.js/issues/199]
func (p *Processor) removeMatchingInvalidRDFs(view string, opts *processorOpts) (string, error) {
if !opts.removeInvalidRDF && !opts.validateRDF {
return view, nil
}
views := strings.Split(view, "\n")
var filteredViews []string
var foundInvalid bool
for _, v := range views {
_, err := ld.ParseNQuads(v)
if err != nil {
if !strings.Contains(err.Error(), handleNormalizeErr) {
return "", err
}
foundInvalid = true
continue
}
filteredViews = append(filteredViews, v)
}
if !foundInvalid {
// clean RDF view, no need to regenerate
return view, nil
} else if opts.validateRDF {
return "", ErrInvalidRDFFound
}
filteredView := strings.Join(filteredViews, "\n")
logger.Debugf("Found invalid RDF dataset, Canonicalizing JSON-LD again after removing invalid data ")
// all invalid RDF dataset from view are removed, re-generate
return p.normalizeFilteredDataset(filteredView)
}
// normalizeFilteredDataset recreates json-ld from RDF view and
// returns normalized RDF dataset from recreated json-ld.
func (p *Processor) normalizeFilteredDataset(view string) (string, error) {
ldOptions := ld.NewJsonLdOptions("")
ldOptions.ProcessingMode = ld.JsonLd_1_1
ldOptions.Algorithm = p.algorithm
ldOptions.Format = format
proc := ld.NewJsonLdProcessor()
filteredJSONLd, err := proc.FromRDF(view, ldOptions)
if err != nil {
return "", err
}
result, err := proc.Normalize(filteredJSONLd, ldOptions)
if err != nil {
return "", err
}
return result.(string), nil
}
func fromRDF(docStatements []string, context interface{},
opts ...Opts) (map[string]interface{}, error) {
procOptions := prepareOpts(opts)
ldOptions := ld.NewJsonLdOptions("")
ldOptions.ProcessingMode = ld.JsonLd_1_1
ldOptions.Format = format
ldOptions.ProduceGeneralizedRdf = true
ldOptions.DocumentLoader = procOptions.documentLoader
doc := strings.Join(docStatements, "\n")
proc := ld.NewJsonLdProcessor()
transformedDoc, err := proc.FromRDF(doc, ldOptions)
if err != nil {
return nil, fmt.Errorf("rdf processing failed: %w", err)
}
transformedDocMap, err := proc.Compact(transformedDoc, context, ldOptions)
if err != nil {
return nil, fmt.Errorf("compacting failed: %w", err)
}
return transformedDocMap, nil
}
// prepareOpts prepare processorOpts from given CanonicalizationOpts arguments.
func prepareOpts(opts []Opts) *processorOpts {
procOpts := &processorOpts{}
for _, opt := range opts {
opt(procOpts)
}
return procOpts
}
func (p *Processor) transformBlankNodes(docMap map[string]interface{},
opts ...Opts) (map[string]interface{}, error) {
procOptions := prepareOpts(opts)
if !procOptions.frameBlankNodes {
return docMap, nil
}
docBytes, err := p.GetCanonicalDocument(docMap, opts...)
if err != nil {
return nil, err
}
rows := splitMessageIntoLines(string(docBytes))
for i, row := range rows {
rows[i] = TransformBlankNode(row)
}
return fromRDF(rows, docMap["@context"], opts...)
}
func splitMessageIntoLines(msg string) []string {
rows := strings.Split(msg, "\n")
msgs := make([]string, 0, len(rows))
for i := range rows {
if strings.TrimSpace(rows[i]) != "" {
msgs = append(msgs, rows[i])
}
}
return msgs
}
// TransformBlankNode replaces blank node identifiers in the RDF statements.
// For example, transform from "_:c14n0" to "urn:bnid:_:c14n0".
func TransformBlankNode(row string) string {
prefixIndex := strings.Index(row, "_:c14n")
if prefixIndex < 0 {
return row
}
sepIndex := strings.Index(row[prefixIndex:], " ")
if sepIndex < 0 {
sepIndex = len(row)
} else {
sepIndex += prefixIndex
}
prefix := row[:prefixIndex]
blankNode := row[prefixIndex:sepIndex]
suffix := row[sepIndex:]
return fmt.Sprintf("%s<urn:bnid:%s>%s", prefix, blankNode, suffix)
}