Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for getting authenticated federation media #437

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions fclient/federationclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type FederationClient interface {
ctx context.Context, origin, s spec.ServerName, userID string, field string,
) (res RespProfile, err error)

DownloadMedia(ctx context.Context, origin, destination spec.ServerName, mediaID string) (res *http.Response, err error)

P2PSendTransactionToRelay(ctx context.Context, u spec.UserID, t gomatrixserverlib.Transaction, forwardingServer spec.ServerName) (res EmptyResp, err error)
P2PGetTransactionFromRelay(ctx context.Context, u spec.UserID, prev RelayEntry, relayServer spec.ServerName) (res RespGetRelayTransaction, err error)
}
Expand Down Expand Up @@ -736,3 +738,33 @@ func (ac *federationClient) RoomHierarchy(
}
return
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think documentation in the same spirit as the other methods would be appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, and good to document that the caller of this method is responsible to close the returned response body, if any.

// DownloadMedia performs an authenticated federation request for a given mediaID.
// The caller is responsible to close the returned response body.
func (ac *federationClient) DownloadMedia(
ctx context.Context, origin, destination spec.ServerName, mediaID string,
) (*http.Response, error) {
var identity *SigningIdentity
for _, id := range ac.identities {
if id.ServerName == origin {
identity = id
break
}
}
if identity == nil {
return nil, fmt.Errorf("no signing identity for server name %q", origin)
}

path := federationPathPrefixV1 + "/media/download/" + url.PathEscape(mediaID)
req := NewFederationRequest("GET", origin, destination, path)

if err := req.Sign(identity.ServerName, identity.KeyID, identity.PrivateKey); err != nil {
return nil, err
}

httpReq, err := req.HTTPRequest()
if err != nil {
return nil, err
}
return ac.DoHTTPRequest(ctx, httpReq)
}
Loading