diff options
Diffstat (limited to 'backend/fetchers/profiles.go')
| -rw-r--r-- | backend/fetchers/profiles.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/backend/fetchers/profiles.go b/backend/fetchers/profiles.go index 038a07f..01a27c0 100644 --- a/backend/fetchers/profiles.go +++ b/backend/fetchers/profiles.go @@ -3,7 +3,9 @@ package fetchers import ( "encoding/json" "fmt" + "io" "llostre/backend/general" + "net" "net/http" "strings" ) @@ -17,6 +19,42 @@ type DIDDocument struct { } `json:"service"` } +func FetchDID(handle string) (string, error) { + // return if it's a DID already + if strings.HasPrefix(handle, "did:") { + return handle, nil + } + + // resolve DNS record first + txtRecords, err := net.LookupTXT("_atproto." + handle) + if err == nil { + for _, txt := range txtRecords { + if after, ok := strings.CutPrefix(txt, "did="); ok { + return after, nil + } + } + } + + // fallback to HTTPS well-known + resp, err := http.Get(fmt.Sprintf("https://%s/.well-known/atproto-did", handle)) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + did := strings.TrimSpace(string(body)) + if !strings.HasPrefix(did, "did:") { + return "", fmt.Errorf("invalid DID returned: %s", did) + } + + return did, nil +} + func FetchDidDoc(did string) (*DIDDocument, error) { didDocURL := fmt.Sprintf("https://plc.directory/%s", did) |
