forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra.go
62 lines (48 loc) · 1.45 KB
/
extra.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
package api
import (
"context"
"fmt"
"net/url"
comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/xrpc"
did "github.com/whyrusleeping/go-did"
otel "go.opentelemetry.io/otel"
)
func ResolveDidToHandle(ctx context.Context, xrpcc *xrpc.Client, pls *PLCServer, udid string) (string, string, error) {
ctx, span := otel.Tracer("gosky").Start(ctx, "resolveDidToHandle")
defer span.End()
doc, err := pls.GetDocument(ctx, udid)
if err != nil {
return "", "", err
}
if len(doc.AlsoKnownAs) == 0 {
return "", "", fmt.Errorf("users did document does not specify a handle")
}
aka := doc.AlsoKnownAs[0]
u, err := url.Parse(aka)
if err != nil {
return "", "", fmt.Errorf("aka field in doc was not a valid url: %w", err)
}
handle := u.Host
var svc *did.Service
for _, s := range doc.Service {
if s.ID.String() == "#atproto_pds" && s.Type == "AtprotoPersonalDataServer" {
svc = &s
break
}
}
if svc == nil {
return "", "", fmt.Errorf("users did document has no pds service set")
}
if svc.ServiceEndpoint != xrpcc.Host {
return "", "", fmt.Errorf("our XRPC client is authed for a different pds (%s != %s)", svc.ServiceEndpoint, xrpcc.Host)
}
verdid, err := comatproto.IdentityResolveHandle(ctx, xrpcc, handle)
if err != nil {
return "", "", err
}
if verdid.Did != udid {
return "", "", fmt.Errorf("pds server reported different did for claimed handle")
}
return handle, svc.ServiceEndpoint, nil
}