1package identity
2
3import (
4 "context"
5 "sync"
6)
7
8type BackingCache interface {
9 GetDoc(did string) (*DidDoc, bool)
10 PutDoc(did string, doc *DidDoc) error
11 BustDoc(did string) error
12
13 GetDid(handle string) (string, bool)
14 PutDid(handle string, did string) error
15 BustDid(handle string) error
16}
17
18type Passport struct {
19 bc BackingCache
20 lk sync.Mutex
21}
22
23func NewPassport(bc BackingCache) *Passport {
24 return &Passport{
25 bc: bc,
26 lk: sync.Mutex{},
27 }
28}
29
30func (p *Passport) FetchDoc(ctx context.Context, did string) (*DidDoc, error) {
31 skipCache, _ := ctx.Value("skip-cache").(bool)
32
33 if !skipCache {
34 cached, ok := p.bc.GetDoc(did)
35 if ok {
36 return cached, nil
37 }
38 }
39
40 p.lk.Lock() // this is pretty pathetic, and i should rethink this. but for now, fuck it
41 defer p.lk.Unlock()
42
43 doc, err := FetchDidDoc(ctx, did)
44 if err != nil {
45 return nil, err
46 }
47
48 p.bc.PutDoc(did, doc)
49
50 return doc, nil
51}
52
53func (p *Passport) ResolveHandle(ctx context.Context, handle string) (string, error) {
54 skipCache, _ := ctx.Value("skip-cache").(bool)
55
56 if !skipCache {
57 cached, ok := p.bc.GetDid(handle)
58 if ok {
59 return cached, nil
60 }
61 }
62
63 did, err := ResolveHandle(ctx, handle)
64 if err != nil {
65 return "", err
66 }
67
68 p.bc.PutDid(handle, did)
69
70 return did, nil
71}
72
73func (p *Passport) BustDoc(ctx context.Context, did string) error {
74 return p.bc.BustDoc(did)
75}
76
77func (p *Passport) BustDid(ctx context.Context, handle string) error {
78 return p.bc.BustDid(handle)
79}