Skip to content

Module API for doing arbitrary authenticated requests. - #35744

Open
Half-Shot wants to merge 2 commits into
developfrom
hs/module-api-authenticated-request
Open

Half-Shot wants to merge 2 commits into
developfrom
hs/module-api-authenticated-request

Conversation

@Half-Shot

Copy link
Copy Markdown
Member

Alternative take on getAccessToken / what #35741 originally did which avoids exposing the access token directly to the module.

This exposes a function that passes in the user's current access token into fetch. There are two variants doAuthenticatedRequest and doUnsafeAuthenticatedRequest which allows the module to either make a request at the homeserver, or (with a large warning block) make a request to any domain. The latter is necessary for resources not hosted on the same domain as the homeserver.

Checklist

  • I have read through review guidelines and CONTRIBUTING.md.
  • I have linked the PR to an issue that describes what needs changing.
  • I have written tests for new code (and old code if feasible).
  • I have ensured new or updated public/exported symbols have accurate TSDoc documentation.
  • I have confirmed linter and other CI checks pass.
  • I have have included screenshots if what the user sees will change
  • I have licensed the changes to Element by completing the Contributor License Agreement (CLA)
  • I will no longer force push to this branch

AI assistance

  • I used AI tools to help create this PR.

Comment on lines +40 to +51
const token = MatrixClientPeg.safeGet().getAccessToken();
if (!token) {
throw new Error("Client has no token. Refusing to make request");
}

const request = new Request(input, init);
const headers = new Headers(request.headers);
if (headers.has("Authorization")) {
throw new Error("Request already has an Authorization header. Refusing to make request");
}
headers.set("Authorization", `Bearer ${token}`);
return fetch(new Request(request, { headers }));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The point of doing this on this side of the fence was to reuse the js-sdk's support for token refresh - otherwise you run into the same issue with the 5 minute default TTL in MAS

This branch was successfully deployed

4 active deployments
EndToEndTests — 42d166af Deployed Nov 9, 2026 by github-actions[bot]
VitestReport — 42d166af Deployed Nov 9, 2026 by github-actions[bot]
Netlify — 42d166af Deployed Nov 9, 2026 by github-actions[bot]
SharedComponentStorybook — 42d166af Deployed Nov 9, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants