···
"github.com/bluesky-social/indigo/util"
16
-
func ResolveHandle(ctx context.Context, cli *http.Client, handle string) (string, error) {
18
-
cli = util.RobustHTTPClient()
23
-
_, err := syntax.ParseHandle(handle)
16
+
func ResolveHandleFromTXT(ctx context.Context, handle string) (string, error) {
17
+
name := fmt.Sprintf("_atproto.%s", handle)
18
+
recs, err := net.LookupTXT(name)
20
+
return "", fmt.Errorf("handle could not be resolved via txt: %w", err)
28
-
recs, err := net.LookupTXT(fmt.Sprintf("_atproto.%s", handle))
30
-
for _, rec := range recs {
31
-
if strings.HasPrefix(rec, "did=") {
32
-
did = strings.Split(rec, "did=")[1]
23
+
for _, rec := range recs {
24
+
if strings.HasPrefix(rec, "did=") {
25
+
maybeDid := strings.Split(rec, "did=")[1]
26
+
if _, err := syntax.ParseDID(maybeDid); err == nil {
27
+
return maybeDid, nil
37
-
fmt.Printf("erorr getting txt records: %v\n", err)
41
-
req, err := http.NewRequestWithContext(
44
-
fmt.Sprintf("https://%s/.well-known/atproto-did", handle),
32
+
return "", fmt.Errorf("handle could not be resolved via txt: no record found")
51
-
resp, err := http.DefaultClient.Do(req)
55
-
defer resp.Body.Close()
35
+
func ResolveHandleFromWellKnown(ctx context.Context, cli *http.Client, handle string) (string, error) {
36
+
ustr := fmt.Sprintf("https://%s/.well=known/atproto-did", handle)
37
+
req, err := http.NewRequestWithContext(
44
+
return "", fmt.Errorf("handle could not be resolved via web: %w", err)
57
-
if resp.StatusCode != http.StatusOK {
58
-
io.Copy(io.Discard, resp.Body)
59
-
return "", fmt.Errorf("unable to resolve handle")
47
+
resp, err := cli.Do(req)
49
+
return "", fmt.Errorf("handle could not be resolved via web: %w", err)
51
+
defer resp.Body.Close()
62
-
b, err := io.ReadAll(resp.Body)
53
+
b, err := io.ReadAll(resp.Body)
55
+
return "", fmt.Errorf("handle could not be resolved via web: %w", err)
67
-
maybeDid := string(b)
58
+
if resp.StatusCode != http.StatusOK {
59
+
return "", fmt.Errorf("handle could not be resolved via web: invalid status code %d", resp.StatusCode)
69
-
if _, err := syntax.ParseDID(maybeDid); err != nil {
70
-
return "", fmt.Errorf("unable to resolve handle")
62
+
maybeDid := string(b)
64
+
if _, err := syntax.ParseDID(maybeDid); err != nil {
65
+
return "", fmt.Errorf("handle could not be resolved via web: invalid did in document")
68
+
return maybeDid, nil
79
-
func FetchDidDoc(ctx context.Context, cli *http.Client, did string) (*DidDoc, error) {
71
+
func ResolveHandle(ctx context.Context, cli *http.Client, handle string) (string, error) {
cli = util.RobustHTTPClient()
76
+
_, err := syntax.ParseHandle(handle)
81
+
if maybeDidFromTxt, err := ResolveHandleFromTXT(ctx, handle); err == nil {
82
+
return maybeDidFromTxt, nil
85
+
if maybeDidFromWeb, err := ResolveHandleFromWellKnown(ctx, cli, handle); err == nil {
86
+
return maybeDidFromWeb, nil
89
+
return "", fmt.Errorf("handle could not be resolved")
92
+
func DidToDocUrl(did string) (string, error) {
if strings.HasPrefix(did, "did:plc:") {
86
-
ustr = fmt.Sprintf("https://plc.directory/%s", did)
94
+
return fmt.Sprintf("https://plc.directory/%s", did), nil
} else if strings.HasPrefix(did, "did:web:") {
88
-
ustr = fmt.Sprintf("https://%s/.well-known/did.json", strings.TrimPrefix(did, "did:web:"))
96
+
return fmt.Sprintf("https://%s/.well-known/did.json", strings.TrimPrefix(did, "did:web:")), nil
90
-
return nil, fmt.Errorf("did was not a supported did type")
98
+
return "", fmt.Errorf("did was not a supported did type")
102
+
func FetchDidDoc(ctx context.Context, cli *http.Client, did string) (*DidDoc, error) {
104
+
cli = util.RobustHTTPClient()
107
+
ustr, err := DidToDocUrl(did)
req, err := http.NewRequestWithContext(ctx, "GET", ustr, nil)
···
98
-
resp, err := http.DefaultClient.Do(req)
117
+
resp, err := cli.Do(req)
···
if resp.StatusCode != 200 {
io.Copy(io.Discard, resp.Body)
106
-
return nil, fmt.Errorf("could not find identity in plc registry")
125
+
return nil, fmt.Errorf("unable to find did doc at url. did: %s. url: %s", did, ustr)
···
130
-
resp, err := http.DefaultClient.Do(req)
149
+
resp, err := cli.Do(req)