-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentifier.go
More file actions
639 lines (546 loc) · 16.8 KB
/
identifier.go
File metadata and controls
639 lines (546 loc) · 16.8 KB
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package did
import (
"fmt"
"net/url"
"strings"
"time"
"go.bryk.io/pkg/errors"
)
// Base prefix according to the specification.
// https://w3c.github.io/did-core/#identifier
const prefix = "did:"
// Common context values used for all DID instances.
var defaultContexts = []interface{}{
defaultContext,
ed25519Context,
x25519Context,
}
// Identifier instance based on the DID specification.
type Identifier struct {
data *identifierData
}
// Internal identifier state.
type identifierData struct {
// DID Method
// https://w3c.github.io/did-core/#method-specific-syntax
Method string
// The method-specific-id component of a DID
// method-specific-id = *idchar *( ":" *idchar )
ID string
// method-specific-id may be composed of multiple `:` separated idstrings
IDStrings []string
// DID URL
// did-url = did *( ";" param ) path-abempty [ "?" query ] [ "#" fragment ]
// did-url may contain multiple params, a path, query, and fragment
Params []Param
// DID Path, the portion of a DID reference that follows the first forward
// slash character.
// https://w3c.github.io/did-core/#path
Path string
// Path may be composed of multiple `/` separated segments
// path-abempty = *( "/" segment )
PathSegments []string
// DID Query
// https://w3c.github.io/did-core/#query
// query = *( pchar / "/" / "?" )
Query string
// DID Fragment, the portion of a DID reference that follows the first hash
// sign character ("#")
// https://w3c.github.io/did-core/#fragment
Fragment string
// Indicates that there are DID controller(s) other than the DID subject.
// https://w3c.github.io/did-core/#authorization-and-delegation
Controller string
// JSON-LD context statement for the document.
// https://w3c-ccg.github.io/did-spec/#context
Context []interface{} `json:"@context" yaml:"-"`
// Cryptographic keys associated with the subject.
VerificationMethods []*VerificationKey
// Enabled authentication mechanisms.
// https://w3c.github.io/did-core/#authentication
AuthenticationMethod []string
// Enabled assertion mechanisms.
// https://w3c.github.io/did-core/#assertionmethod
AssertionMethod []string
// Enabled key agreement mechanisms.
// https://w3c.github.io/did-core/#keyagreement
KeyAgreement []string
// Enabled capability invocation mechanisms.
// https://w3c.github.io/did-core/#capabilityinvocation
CapabilityInvocation []string
// Enabled capability delegation mechanisms.
// https://w3c.github.io/did-core/#capabilitydelegation
CapabilityDelegation []string
// Service endpoints enabled.
Services []*ServiceEndpoint
// Time of original creation normalized to UTC 00:00.
Created *time.Time
// Time of the latest update normalized to UTC 00:00.
Updated *time.Time
// Indicates if an identifier is deactivated
Deactivated bool
}
// NewIdentifier provides a helper factory method to generate a free-form identifier
// instance using the provided method and id string.
func NewIdentifier(method string, idString string) (*Identifier, error) {
if strings.TrimSpace(method) == "" {
return nil, errors.New("no method specified")
}
if strings.TrimSpace(idString) == "" {
return nil, errors.New("no id string specified")
}
now := time.Now().UTC()
return &Identifier{
data: &identifierData{
ID: idString,
Method: method,
Created: &now,
Context: defaultContexts,
},
}, nil
}
// NewIdentifierWithMode provides a helper factory method to generate new random
// identifier instances using one of the modes described in the "bryk" DID Method
// specification.
func NewIdentifierWithMode(method string, tag string, mode idStringMode) (*Identifier, error) {
// Get id string based on the selected method
id := ""
switch mode {
case ModeUUID:
id = randomUUID()
case ModeHash:
id = randomHash()
}
// Append tag to the id string if provided
if tag != "" {
id = fmt.Sprintf("%s:%s", tag, id)
}
// Return identifier
return NewIdentifier(method, id)
}
// FromDocument restores an identifier instance from a previously generated DID Document.
func FromDocument(doc *Document) (*Identifier, error) {
id, err := Parse(doc.Subject)
if err != nil {
return nil, err
}
// Restore public keys
for _, k := range doc.VerificationMethod {
rk := &VerificationKey{}
*rk = k
id.data.VerificationMethods = append(id.data.VerificationMethods, rk)
}
// Restore service endpoints
for _, s := range doc.Services {
rs := &ServiceEndpoint{}
*rs = s
id.data.Services = append(id.data.Services, rs)
}
// Restore verification relationships
id.data.Context = doc.Context
id.data.Controller = doc.Controller
id.data.AuthenticationMethod = append(id.data.AuthenticationMethod, doc.Authentication...)
id.data.AssertionMethod = append(id.data.AssertionMethod, doc.AssertionMethod...)
id.data.KeyAgreement = append(id.data.KeyAgreement, doc.KeyAgreement...)
id.data.CapabilityInvocation = append(id.data.CapabilityInvocation, doc.CapabilityInvocation...)
id.data.CapabilityDelegation = append(id.data.CapabilityDelegation, doc.CapabilityDelegation...)
return id, nil
}
// IsURL returns true if a DID has a Path, a Query or a Fragment
// https://w3c.github.io/did-core/#did-url-syntax
func (d *Identifier) IsURL() bool {
dd := d.data
return (len(dd.Params) > 0 || dd.Path != "" || len(dd.PathSegments) > 0 || dd.Query != "" || dd.Fragment != "")
}
// GetReference returns a valid DID with the provided fragment appended.
func (d *Identifier) GetReference(fragment string) string {
return fmt.Sprintf("%s#%s", d.DID(), fragment)
}
// Method returns the method segment of the identifier instance.
func (d *Identifier) Method() string {
return strings.ToLower(d.data.Method)
}
// Path returns the path segment of the identifier instance.
func (d *Identifier) Path() string {
return d.path()
}
// Fragment returns the fragment segment of the identifier instance.
func (d *Identifier) Fragment() string {
if d.data.Fragment == "" {
return ""
}
return fmt.Sprintf("#%s", d.data.Fragment)
}
// RawQuery returns the query portion of the identifier instance as a string.
func (d *Identifier) RawQuery() string {
return d.data.Query
}
// Query returns the URL-decoded contents of the query segment of the identifier instance.
func (d *Identifier) Query() (url.Values, error) {
if d.data.Query == "" {
return nil, errors.New("no query values")
}
q, err := url.ParseQuery(d.data.Query)
if err != nil {
return nil, wrap(err, "failed to parse query segment")
}
return q, nil
}
// DID returns the DID segment of the identifier instance.
func (d *Identifier) DID() string {
return fmt.Sprintf("%s%s:%s", prefix, d.data.Method, d.idString())
}
// Subject returns the specific ID segment of the identifier instance.
func (d *Identifier) Subject() string {
return d.idString()
}
// Verify search for common errors in the identifier structure.
func (d *Identifier) Verify(c IDStringVerifier) error {
// Method is required
if d.data.Method == "" {
return errors.New("no method specified")
}
// Specific ID string is required
if d.idString() == "" {
return errors.New("no id string specified")
}
// Custom verification of the specific id string
if c != nil {
if err := c(d.idString()); err != nil {
return err
}
}
return nil
}
// String encodes a DID instance into a valid DID string.
func (d *Identifier) String() string {
// base identifier structure verification
if err := d.Verify(nil); err != nil {
return ""
}
var buf strings.Builder
// write base did segment
buf.WriteString(d.DID())
// write params
buf.WriteString(d.params())
// write path
buf.WriteString(d.path())
if d.data.Query != "" {
// write a leading ? and then Query
buf.WriteByte('?')
buf.WriteString(d.data.Query)
}
if d.data.Fragment != "" {
// write a leading # and then the fragment value
buf.WriteByte('#')
buf.WriteString(d.data.Fragment)
}
return buf.String()
}
// Document returns the DID document for the identifier instance. If 'safe'
// is true, the returned document remove any private key material present,
// making the document safe to be published and shared.
func (d *Identifier) Document(safe bool) *Document {
doc := &Document{
Context: d.data.Context,
Subject: d.String(),
Controller: d.data.Controller,
VerificationMethod: d.VerificationMethods(),
Services: d.Services(),
Authentication: d.data.AuthenticationMethod,
AssertionMethod: d.data.AssertionMethod,
KeyAgreement: d.data.KeyAgreement,
CapabilityInvocation: d.data.CapabilityInvocation,
CapabilityDelegation: d.data.CapabilityDelegation,
}
// Remove private keys on safe representations.
if safe {
for i := range doc.VerificationMethod {
doc.VerificationMethod[i].Private = nil
}
}
return doc
}
// RegisterContext adds a new context entry to the document. Useful when
// adding new data entries.
// https://w3c.github.io/json-ld-syntax/#the-context
func (d *Identifier) RegisterContext(el interface{}) {
for _, v := range d.data.Context {
if el == v {
return
}
}
d.data.Context = append(d.data.Context, el)
}
// Controller returns the DID currently set as controller for the identifier
// instance.
func (d *Identifier) Controller() string {
return d.data.Controller
}
// SetController updates the DID set as controller for the identifier instance.
func (d *Identifier) SetController(did string) error {
if _, err := Parse(did); err != nil {
return err
}
d.data.Controller = did
return nil
}
// AddNewVerificationMethod generates and registers a new cryptographic key for
// the identifier instance.
func (d *Identifier) AddNewVerificationMethod(id string, kt KeyType) error {
if !strings.HasPrefix(id, prefix) {
id = d.GetReference(id)
}
for _, k := range d.data.VerificationMethods {
if k.ID == id {
return errors.New("duplicated key identifier")
}
}
pk, err := newCryptoKey(kt)
if err != nil {
return err
}
pk.Controller = d.DID()
pk.ID = id
d.data.VerificationMethods = append(d.data.VerificationMethods, pk)
d.update()
return nil
}
// AddVerificationMethod attach an existing cryptographic key to the identifier.
func (d *Identifier) AddVerificationMethod(id string, private []byte, kt KeyType) error {
if !strings.HasPrefix(id, prefix) {
id = d.GetReference(id)
}
for _, k := range d.data.VerificationMethods {
if k.ID == id {
return errors.New("duplicated key identifier")
}
}
pk, err := loadExistingKey(private, kt)
if err != nil {
return err
}
pk.Controller = d.DID()
pk.ID = id
d.data.VerificationMethods = append(d.data.VerificationMethods, pk)
d.update()
return nil
}
// RemoveVerificationMethod will permanently eliminate a registered key from the
// instance. An error will be produced if the key you're trying to remove is the
// only enabled authentication key.
func (d *Identifier) RemoveVerificationMethod(id string) error {
if !strings.HasPrefix(id, prefix) {
id = d.GetReference(id)
}
for i, k := range d.data.VerificationMethods {
if k.ID == id {
if len(d.data.AuthenticationMethod) == 1 && d.data.AuthenticationMethod[0] == id {
return errors.New("can't remove only authentication key")
}
d.data.VerificationMethods = append(d.data.VerificationMethods[:i], d.data.VerificationMethods[i+1:]...)
d.update()
return nil
}
}
return errors.New("invalid key identifier")
}
// VerificationMethod retrieve a key based on it's id (fragment value), "nil"
// is returned if the identifier is invalid.
func (d *Identifier) VerificationMethod(id string) *VerificationKey {
if !strings.HasPrefix(id, prefix) {
id = d.GetReference(id)
}
for _, k := range d.data.VerificationMethods {
if k.ID == id {
return k
}
}
return nil
}
// AddService set a new service endpoint for the identifier instance.
func (d *Identifier) AddService(se *ServiceEndpoint) error {
// Set proper service identifier
if !strings.Contains(se.ID, d.DID()) {
se.ID = d.GetReference(se.ID)
}
// Verify the service is not already registered
check := false
for _, s := range d.data.Services {
if s.ID == se.ID {
check = true
break
}
}
if check {
return errors.New("duplicated service ID")
}
d.data.Services = append(d.data.Services, se)
d.update()
return nil
}
// RemoveService will eliminate a previously registered service endpoint for the instance.
func (d *Identifier) RemoveService(name string) error {
// Set proper service identifier
if !strings.Contains(name, d.DID()) {
name = d.GetReference(name)
}
// Verify the service is registered
index := 0
check := false
for i, s := range d.data.Services {
if s.ID == name {
index = i
check = true
break
}
}
if !check {
return errors.New("service is not registered")
}
d.data.Services = append(d.data.Services[:index], d.data.Services[index+1:]...)
d.update()
return nil
}
// Service retrieve a service endpoint based on it's id, "nil" is returned if
// the identifier is invalid.
func (d *Identifier) Service(id string) *ServiceEndpoint {
if !strings.HasPrefix(id, prefix) {
id = d.GetReference(id)
}
for _, s := range d.data.Services {
if s.ID == id {
return s
}
}
return nil
}
// GetProof generates a cryptographically verifiable proof of integrity for
// the identifier's document.
// https://w3c.github.io/did-core//#proof-optional
func (d *Identifier) GetProof(keyID, domain string) (*ProofLD, error) {
// Retrieve key
pk := d.VerificationMethod(keyID)
if pk == nil {
return nil, errors.New("invalid key identifier")
}
// Use normalized DID document as base input
data, err := d.Document(true).NormalizedLD()
if err != nil {
return nil, wrap(err, "failed to normalize DID document")
}
// Generate proof instance
return pk.ProduceProof(data, "authentication", domain)
}
// VerificationMethods returns the registered verification methods on
// the identifier instance.
func (d *Identifier) VerificationMethods() []VerificationKey {
keys := make([]VerificationKey, len(d.data.VerificationMethods))
for i, k := range d.data.VerificationMethods {
keys[i] = *k
}
return keys
}
// Services returns the registered service endpoints on the identifier.
func (d *Identifier) Services() []ServiceEndpoint {
srv := make([]ServiceEndpoint, len(d.data.Services))
for i, s := range d.data.Services {
srv[i] = *s
}
return srv
}
// Created returns the creation date for the instance, will return an error if no
// date is currently set.
func (d *Identifier) Created() (time.Time, error) {
if d.data.Created != nil {
return *d.data.Created, nil
}
return time.Now(), errors.New("no creation date set")
}
// Updated returns the date of the last update for the instance, will return an error
// if no date is currently set.
func (d *Identifier) Updated() (time.Time, error) {
if d.data.Updated != nil {
return *d.data.Updated, nil
}
return time.Now(), errors.New("no update date set")
}
// Returns the "specific-idstring" portion of the identifier instance.
func (d *Identifier) idString() string {
if d.data.ID != "" {
return d.data.ID
} else if len(d.data.IDStrings) > 0 {
return strings.Join(d.data.IDStrings, ":")
}
return ""
}
// Returns the "path" portion of the identifier instance.
func (d *Identifier) path() string {
p := ""
if d.data.Path != "" {
p = "/" + d.data.Path
} else if len(d.data.PathSegments) > 0 {
p = "/" + strings.Join(d.data.PathSegments[:], "/")
}
return p
}
// Returns the "params" portion of the identifier instance.
func (d *Identifier) params() string {
if len(d.data.Params) == 0 {
return ""
}
// write a leading ; for each param
var buf strings.Builder
for _, p := range d.data.Params {
if param := p.String(); param != "" {
buf.WriteByte(';')
buf.WriteString(param)
}
}
return buf.String()
}
// Adjust the timestamp for last update on the identifier instance.
func (d *Identifier) update() {
t := time.Now().UTC()
d.data.Updated = &t
}
// AddMetadata updates the identifier Created, Updated, and Deactivated
// properties based on a document metadata input.
func (d *Identifier) AddMetadata(metadata *DocumentMetadata) error {
if d.data == nil {
d.data = &identifierData{}
}
d.data.Deactivated = metadata.Deactivated
if metadata.Created != "" {
created, err := time.ParseInLocation(time.RFC3339, metadata.Created, time.UTC)
if err != nil {
return err
}
d.data.Created = &created
}
if metadata.Updated != "" {
updated, err := time.ParseInLocation(time.RFC3339, metadata.Updated, time.UTC)
if err != nil {
return err
}
d.data.Updated = &updated
}
return nil
}
// GetMetadata returns the DocumentMetadata for the identifier instance.
func (d *Identifier) GetMetadata() *DocumentMetadata {
metadata := &DocumentMetadata{
Deactivated: d.data.Deactivated,
}
if d.data.Created != nil {
metadata.Created = d.data.Created.UTC().Format(time.RFC3339)
}
if d.data.Updated != nil {
metadata.Updated = d.data.Updated.UTC().Format(time.RFC3339)
}
return metadata
}
// Deactivated returns a bool value indicating if the identifier is deactivated.
func (d *Identifier) Deactivated() bool {
return d.data.Deactivated
}