this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

update BaseDirectory to match interface

+46 -24
+2 -2
atproto/identity/base_directory.go
··· 39 39 if err != nil { 40 40 return nil, err 41 41 } 42 - doc, err := d.ResolveDID(ctx, did) 42 + doc, err := d.ResolveDIDDoc(ctx, did) 43 43 if err != nil { 44 44 return nil, err 45 45 } ··· 57 57 } 58 58 59 59 func (d *BaseDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) { 60 - doc, err := d.ResolveDID(ctx, did) 60 + doc, err := d.ResolveDIDDoc(ctx, did) 61 61 if err != nil { 62 62 return nil, err 63 63 }
+44 -22
atproto/identity/did.go
··· 5 5 "encoding/json" 6 6 "errors" 7 7 "fmt" 8 + "io" 8 9 "log/slog" 9 10 "net" 10 11 "net/http" ··· 13 14 "github.com/bluesky-social/indigo/atproto/syntax" 14 15 ) 15 16 16 - // WARNING: this does *not* bi-directionally verify account metadata; it only implements direct DID-to-DID-document lookup for the supported DID methods, and parses the resulting DID Doc into an Identity struct 17 - func (d *BaseDirectory) ResolveDID(ctx context.Context, did syntax.DID) (*DIDDocument, error) { 17 + // This is a low-level method for resolving a DID to a raw JSON document. 18 + // 19 + // This method does not parse the DID document into an atproto-specific format, and does not bi-directionally verify handles. Most atproto-specific code should use the "Lookup*" methods, which do implement that functionality. 20 + func (d *BaseDirectory) ResolveDID(ctx context.Context, did syntax.DID) (map[string]any, error) { 21 + raw, err := d.resolveDIDBytes(ctx, did) 22 + if err != nil { 23 + return nil, err 24 + } 25 + 26 + var doc map[string]any 27 + if err := json.Unmarshal(raw, &doc); err != nil { 28 + return nil, fmt.Errorf("%w: JSON DID document parse: %w", ErrDIDResolutionFailed, err) 29 + } 30 + return doc, nil 31 + } 32 + 33 + // Variant of ResolveDID which parses in to a DIDDocument struct. 34 + func (d *BaseDirectory) ResolveDIDDoc(ctx context.Context, did syntax.DID) (*DIDDocument, error) { 35 + raw, err := d.resolveDIDBytes(ctx, did) 36 + if err != nil { 37 + return nil, err 38 + } 39 + 40 + var doc DIDDocument 41 + if err := json.Unmarshal(raw, &doc); err != nil { 42 + return nil, fmt.Errorf("%w: JSON DID document parse: %w", ErrDIDResolutionFailed, err) 43 + } 44 + return &doc, nil 45 + } 46 + 47 + // Package-internal helper which resolves a DID document to the response bytes. 48 + func (d *BaseDirectory) resolveDIDBytes(ctx context.Context, did syntax.DID) ([]byte, error) { 49 + var b []byte 50 + var err error 18 51 start := time.Now() 19 52 switch did.Method() { 20 53 case "web": 21 - doc, err := d.ResolveDIDWeb(ctx, did) 22 - elapsed := time.Since(start) 23 - slog.Debug("resolve DID", "did", did, "err", err, "duration_ms", elapsed.Milliseconds()) 24 - return doc, err 54 + b, err = d.resolveDIDWeb(ctx, did) 25 55 case "plc": 26 - doc, err := d.ResolveDIDPLC(ctx, did) 27 - elapsed := time.Since(start) 28 - slog.Debug("resolve DID", "did", did, "err", err, "duration_ms", elapsed.Milliseconds()) 29 - return doc, err 56 + b, err = d.resolveDIDPLC(ctx, did) 30 57 default: 31 58 return nil, fmt.Errorf("DID method not supported: %s", did.Method()) 32 59 } 60 + elapsed := time.Since(start) 61 + slog.Debug("resolve DID", "did", did, "err", err, "duration_ms", elapsed.Milliseconds()) 62 + return b, err 33 63 } 34 64 35 - func (d *BaseDirectory) ResolveDIDWeb(ctx context.Context, did syntax.DID) (*DIDDocument, error) { 65 + func (d *BaseDirectory) resolveDIDWeb(ctx context.Context, did syntax.DID) ([]byte, error) { 36 66 if did.Method() != "web" { 37 67 return nil, fmt.Errorf("expected a did:web, got: %s", did) 38 68 } ··· 77 107 return nil, fmt.Errorf("%w: did:web HTTP status %d", ErrDIDResolutionFailed, resp.StatusCode) 78 108 } 79 109 80 - var doc DIDDocument 81 - if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil { 82 - return nil, fmt.Errorf("%w: JSON DID document parse: %w", ErrDIDResolutionFailed, err) 83 - } 84 - return &doc, nil 110 + return io.ReadAll(resp.Body) 85 111 } 86 112 87 - func (d *BaseDirectory) ResolveDIDPLC(ctx context.Context, did syntax.DID) (*DIDDocument, error) { 113 + func (d *BaseDirectory) resolveDIDPLC(ctx context.Context, did syntax.DID) ([]byte, error) { 88 114 if did.Method() != "plc" { 89 115 return nil, fmt.Errorf("expected a did:plc, got: %s", did) 90 116 } ··· 116 142 return nil, fmt.Errorf("%w: PLC directory status %d", ErrDIDResolutionFailed, resp.StatusCode) 117 143 } 118 144 119 - var doc DIDDocument 120 - if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil { 121 - return nil, fmt.Errorf("%w: JSON DID document parse: %w", ErrDIDResolutionFailed, err) 122 - } 123 - return &doc, nil 145 + return io.ReadAll(resp.Body) 124 146 }