2020-05-27 09:19:24 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-06-04 14:40:23 +00:00
|
|
|
"time"
|
2020-05-27 09:19:24 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/serverkeyapi/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerKeyAPI struct {
|
|
|
|
api.ServerKeyInternalAPI
|
|
|
|
|
2020-06-04 15:26:35 +00:00
|
|
|
OurKeyRing gomatrixserverlib.KeyRing
|
|
|
|
FedClient *gomatrixserverlib.FederationClient
|
2020-05-27 09:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) KeyRing() *gomatrixserverlib.KeyRing {
|
2020-06-10 10:02:03 +00:00
|
|
|
// Return a keyring that forces requests to be proxied through the
|
|
|
|
// below functions. That way we can enforce things like validity
|
|
|
|
// and keeping the cache up-to-date.
|
|
|
|
return &gomatrixserverlib.KeyRing{
|
|
|
|
KeyDatabase: s,
|
2020-06-12 10:07:26 +00:00
|
|
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{},
|
2020-06-10 10:02:03 +00:00
|
|
|
}
|
2020-05-27 09:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) StoreKeys(
|
2020-06-04 14:40:23 +00:00
|
|
|
_ context.Context,
|
2020-05-27 09:19:24 +00:00
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) error {
|
2020-06-04 14:40:23 +00:00
|
|
|
// Run in a background context - we don't want to stop this work just
|
|
|
|
// because the caller gives up waiting.
|
|
|
|
ctx := context.Background()
|
2020-05-27 09:19:24 +00:00
|
|
|
// Store any keys that we were given in our database.
|
|
|
|
return s.OurKeyRing.KeyDatabase.StoreKeys(ctx, results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) FetchKeys(
|
2020-06-04 14:40:23 +00:00
|
|
|
_ context.Context,
|
2020-05-27 09:19:24 +00:00
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
2020-06-04 14:40:23 +00:00
|
|
|
// Run in a background context - we don't want to stop this work just
|
|
|
|
// because the caller gives up waiting.
|
|
|
|
ctx := context.Background()
|
2020-05-27 09:19:24 +00:00
|
|
|
results := map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult{}
|
2020-06-12 10:07:26 +00:00
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
2020-05-27 09:19:24 +00:00
|
|
|
// First consult our local database and see if we have the requested
|
|
|
|
// keys. These might come from a cache, depending on the database
|
|
|
|
// implementation used.
|
|
|
|
if dbResults, err := s.OurKeyRing.KeyDatabase.FetchKeys(ctx, requests); err == nil {
|
|
|
|
// We successfully got some keys. Add them to the results and
|
|
|
|
// remove them from the request list.
|
|
|
|
for req, res := range dbResults {
|
2020-06-12 10:07:26 +00:00
|
|
|
if !res.WasValidAt(now, true) {
|
|
|
|
// We appear to be past the key validity. Don't return this
|
|
|
|
// key with the results.
|
2020-06-04 14:40:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-05-27 09:19:24 +00:00
|
|
|
results[req] = res
|
|
|
|
delete(requests, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For any key requests that we still have outstanding, next try to
|
|
|
|
// fetch them directly. We'll go through each of the key fetchers to
|
|
|
|
// ask for the remaining keys.
|
|
|
|
for _, fetcher := range s.OurKeyRing.KeyFetchers {
|
|
|
|
if len(requests) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if fetcherResults, err := fetcher.FetchKeys(ctx, requests); err == nil {
|
|
|
|
// We successfully got some keys. Add them to the results and
|
|
|
|
// remove them from the request list.
|
|
|
|
for req, res := range fetcherResults {
|
2020-06-12 10:07:26 +00:00
|
|
|
if !res.WasValidAt(now, true) {
|
|
|
|
// We appear to be past the key validity. Don't return this
|
|
|
|
// key with the results.
|
|
|
|
continue
|
|
|
|
}
|
2020-05-27 09:19:24 +00:00
|
|
|
results[req] = res
|
|
|
|
delete(requests, req)
|
|
|
|
}
|
2020-06-04 14:40:23 +00:00
|
|
|
if err = s.OurKeyRing.KeyDatabase.StoreKeys(ctx, fetcherResults); err != nil {
|
|
|
|
return nil, fmt.Errorf("server key API failed to store retrieved keys: %w", err)
|
|
|
|
}
|
2020-05-27 09:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we failed to fetch any keys then we should report an error.
|
|
|
|
if len(requests) > 0 {
|
|
|
|
return results, fmt.Errorf("server key API failed to fetch %d keys", len(requests))
|
|
|
|
}
|
|
|
|
// Return the keys.
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerKeyAPI) FetcherName() string {
|
2020-06-10 10:02:03 +00:00
|
|
|
return fmt.Sprintf("ServerKeyAPI (wrapping %q)", s.OurKeyRing.KeyDatabase.FetcherName())
|
2020-05-27 09:19:24 +00:00
|
|
|
}
|