MCPcopy Create free account
hub / github.com/daodst/chat / FetchKeys

Method FetchKeys

federationapi/internal/keys.go:32–88  ·  view source on GitHub ↗
(
	_ context.Context,
	requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
)

Source from the content-addressed store, hash-verified

30}
31
32func (s *FederationInternalAPI) FetchKeys(
33 _ context.Context,
34 requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
35) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
36 // Run in a background context - we don't want to stop this work just
37 // because the caller gives up waiting.
38 ctx := context.Background()
39 now := gomatrixserverlib.AsTimestamp(time.Now())
40 results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
41 origRequests := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp{}
42 for k, v := range requests {
43 origRequests[k] = v
44 }
45
46 // First, check if any of these key checks are for our own keys. If
47 // they are then we will satisfy them directly.
48 s.handleLocalKeys(ctx, requests, results)
49
50 // Then consult our local database and see if we have the requested
51 // keys. These might come from a cache, depending on the database
52 // implementation used.
53 if err := s.handleDatabaseKeys(ctx, now, requests, results); err != nil {
54 return nil, err
55 }
56
57 // For any key requests that we still have outstanding, next try to
58 // fetch them directly. We'll go through each of the key fetchers to
59 // ask for the remaining keys
60 for _, fetcher := range s.keyRing.KeyFetchers {
61 // If there are no more keys to look up then stop.
62 if len(requests) == 0 {
63 break
64 }
65
66 // Ask the fetcher to look up our keys.
67 if err := s.handleFetcherKeys(ctx, now, fetcher, requests, results); err != nil {
68 logrus.WithError(err).WithFields(logrus.Fields{
69 "fetcher_name": fetcher.FetcherName(),
70 }).Errorf("Failed to retrieve %d key(s)", len(requests))
71 continue
72 }
73 }
74
75 // Check that we've actually satisfied all of the key requests that we
76 // were given. We should report an error if we didn't.
77 for req := range origRequests {
78 if _, ok := results[req]; !ok {
79 // The results don't contain anything for this specific request, so
80 // we've failed to satisfy it from local keys, database keys or from
81 // all of the fetchers. Report an error.
82 logrus.Warnf("Failed to retrieve key %q for server %q", req.KeyID, req.ServerName)
83 }
84 }
85
86 // Return the keys.
87 return results, nil
88}
89

Callers 2

handleDatabaseKeysMethod · 0.45
handleFetcherKeysMethod · 0.45

Calls 4

handleLocalKeysMethod · 0.95
handleDatabaseKeysMethod · 0.95
handleFetcherKeysMethod · 0.95
FetcherNameMethod · 0.45

Tested by

no test coverage detected