Add whitelist for PDS hosts and update repo PDS pointer on appropriate occasions

This commit is contained in:
Max Ignatenko 2024-02-22 12:05:44 +00:00
parent 600dac7694
commit 8f32c494f7
8 changed files with 108 additions and 43 deletions

View file

@ -3,6 +3,8 @@ package resolver
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"github.com/bluesky-social/indigo/api"
@ -56,3 +58,29 @@ func (r *fallbackResolver) FlushCacheFor(did string) {
res.FlushCacheFor(did)
}
}
func GetPDSEndpoint(ctx context.Context, did string) (*url.URL, error) {
doc, err := GetDocument(ctx, did)
if err != nil {
return nil, fmt.Errorf("resolving did %q: %w", did, err)
}
pdsHost := ""
for _, srv := range doc.Service {
if srv.Type != "AtprotoPersonalDataServer" {
continue
}
pdsHost = srv.ServiceEndpoint
}
if pdsHost == "" {
return nil, fmt.Errorf("did not find any PDS in DID Document")
}
u, err := url.Parse(pdsHost)
if err != nil {
return nil, fmt.Errorf("PDS endpoint (%q) is an invalid URL: %w", pdsHost, err)
}
if u.Host == "" {
return nil, fmt.Errorf("PDS endpoint (%q) doesn't have a host part", pdsHost)
}
return u, nil
}