Skip to content

Commit d531860

Browse files
authored
Add support for getting authenticated federation media (#437)
1 parent c2391f7 commit d531860

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

fclient/federationclient.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type FederationClient interface {
6464
ctx context.Context, origin, s spec.ServerName, userID string, field string,
6565
) (res RespProfile, err error)
6666

67+
DownloadMedia(ctx context.Context, origin, destination spec.ServerName, mediaID string) (res *http.Response, err error)
68+
6769
P2PSendTransactionToRelay(ctx context.Context, u spec.UserID, t gomatrixserverlib.Transaction, forwardingServer spec.ServerName) (res EmptyResp, err error)
6870
P2PGetTransactionFromRelay(ctx context.Context, u spec.UserID, prev RelayEntry, relayServer spec.ServerName) (res RespGetRelayTransaction, err error)
6971
}
@@ -736,3 +738,33 @@ func (ac *federationClient) RoomHierarchy(
736738
}
737739
return
738740
}
741+
742+
// DownloadMedia performs an authenticated federation request for a given mediaID.
743+
// The caller is responsible to close the returned response body.
744+
func (ac *federationClient) DownloadMedia(
745+
ctx context.Context, origin, destination spec.ServerName, mediaID string,
746+
) (*http.Response, error) {
747+
var identity *SigningIdentity
748+
for _, id := range ac.identities {
749+
if id.ServerName == origin {
750+
identity = id
751+
break
752+
}
753+
}
754+
if identity == nil {
755+
return nil, fmt.Errorf("no signing identity for server name %q", origin)
756+
}
757+
758+
path := federationPathPrefixV1 + "/media/download/" + url.PathEscape(mediaID)
759+
req := NewFederationRequest("GET", origin, destination, path)
760+
761+
if err := req.Sign(identity.ServerName, identity.KeyID, identity.PrivateKey); err != nil {
762+
return nil, err
763+
}
764+
765+
httpReq, err := req.HTTPRequest()
766+
if err != nil {
767+
return nil, err
768+
}
769+
return ac.DoHTTPRequest(ctx, httpReq)
770+
}

0 commit comments

Comments
 (0)