2020-05-27 10:19:24 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2020-06-04 15:40:23 +01:00
|
|
|
"time"
|
2020-05-27 10:19:24 +01:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerKeyInternalAPI interface {
|
|
|
|
gomatrixserverlib.KeyDatabase
|
|
|
|
|
|
|
|
KeyRing() *gomatrixserverlib.KeyRing
|
|
|
|
|
|
|
|
InputPublicKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
request *InputPublicKeysRequest,
|
|
|
|
response *InputPublicKeysResponse,
|
|
|
|
) error
|
|
|
|
|
|
|
|
QueryPublicKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
request *QueryPublicKeysRequest,
|
|
|
|
response *QueryPublicKeysResponse,
|
|
|
|
) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRoomserverInputAPIHTTP creates a RoomserverInputAPI implemented by talking to a HTTP POST API.
|
|
|
|
// If httpClient is nil an error is returned
|
|
|
|
func NewServerKeyInternalAPIHTTP(
|
|
|
|
serverKeyAPIURL string,
|
|
|
|
httpClient *http.Client,
|
|
|
|
immutableCache caching.ImmutableCache,
|
|
|
|
) (ServerKeyInternalAPI, error) {
|
|
|
|
if httpClient == nil {
|
|
|
|
return nil, errors.New("NewRoomserverInternalAPIHTTP: httpClient is <nil>")
|
|
|
|
}
|
|
|
|
return &httpServerKeyInternalAPI{
|
|
|
|
serverKeyAPIURL: serverKeyAPIURL,
|
|
|
|
httpClient: httpClient,
|
|
|
|
immutableCache: immutableCache,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpServerKeyInternalAPI struct {
|
|
|
|
ServerKeyInternalAPI
|
|
|
|
|
|
|
|
serverKeyAPIURL string
|
|
|
|
httpClient *http.Client
|
|
|
|
immutableCache caching.ImmutableCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServerKeyInternalAPI) KeyRing() *gomatrixserverlib.KeyRing {
|
|
|
|
// This is a bit of a cheat - we tell gomatrixserverlib that this API is
|
|
|
|
// both the key database and the key fetcher. While this does have the
|
|
|
|
// rather unfortunate effect of preventing gomatrixserverlib from handling
|
|
|
|
// key fetchers directly, we can at least reimplement this behaviour on
|
|
|
|
// the other end of the API.
|
|
|
|
return &gomatrixserverlib.KeyRing{
|
|
|
|
KeyDatabase: s,
|
|
|
|
KeyFetchers: []gomatrixserverlib.KeyFetcher{s},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServerKeyInternalAPI) FetcherName() string {
|
|
|
|
return "httpServerKeyInternalAPI"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServerKeyInternalAPI) StoreKeys(
|
2020-06-04 15:40:23 +01:00
|
|
|
_ context.Context,
|
2020-05-27 10:19:24 +01:00
|
|
|
results map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
|
|
) error {
|
2020-06-04 15:40:23 +01: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 10:19:24 +01:00
|
|
|
request := InputPublicKeysRequest{
|
|
|
|
Keys: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
|
|
|
|
}
|
|
|
|
response := InputPublicKeysResponse{}
|
|
|
|
for req, res := range results {
|
|
|
|
request.Keys[req] = res
|
|
|
|
s.immutableCache.StoreServerKey(req, res)
|
|
|
|
}
|
|
|
|
return s.InputPublicKeys(ctx, &request, &response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServerKeyInternalAPI) FetchKeys(
|
2020-06-04 15:40:23 +01:00
|
|
|
_ context.Context,
|
2020-05-27 10:19:24 +01:00
|
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
|
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
2020-06-04 15:40:23 +01: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 10:19:24 +01:00
|
|
|
result := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
|
|
|
|
request := QueryPublicKeysRequest{
|
|
|
|
Requests: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp),
|
|
|
|
}
|
|
|
|
response := QueryPublicKeysResponse{
|
|
|
|
Results: make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult),
|
|
|
|
}
|
2020-06-04 15:40:23 +01:00
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
2020-05-27 10:19:24 +01:00
|
|
|
for req, ts := range requests {
|
|
|
|
if res, ok := s.immutableCache.GetServerKey(req); ok {
|
2020-06-04 15:40:23 +01:00
|
|
|
if now > res.ValidUntilTS && res.ExpiredTS == gomatrixserverlib.PublicKeyNotExpired {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-27 10:19:24 +01:00
|
|
|
result[req] = res
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
request.Requests[req] = ts
|
|
|
|
}
|
|
|
|
err := s.QueryPublicKeys(ctx, &request, &response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for req, res := range response.Results {
|
|
|
|
result[req] = res
|
|
|
|
s.immutableCache.StoreServerKey(req, res)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|