Skip to content

Commit

Permalink
Pass access-token explicitly
Browse files Browse the repository at this point in the history
Reading from storage requires fetching the session-id from navigation
segment. This is not a good idea since there's no guarantee of such
segments existing when hydrogen is used as an SDK.
  • Loading branch information
MidhunSureshR committed Oct 20, 2024
1 parent 3803386 commit 7d726ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/matrix/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export class Client {
platform: this._platform,
serverVersions: lastVersionsResponse.versions,
});

// Let the serviceWorkerHandler know of this access-token
this._platform.updateService.setAccessToken(sessionInfo.accessToken);

this._session = new Session({
storage: this._storage,
sessionInfo: filteredSessionInfo,
Expand Down Expand Up @@ -378,6 +382,7 @@ export class Client {
throw Error("No session loaded, cannot update access token");
}
this._session.updateAccessToken(token);
await this._platform.updateService.setAccessToken(token);
await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token);
}

Expand Down
24 changes: 10 additions & 14 deletions src/platform/web/dom/ServiceWorkerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ export class ServiceWorkerHandler {
this._currentController = null;
this._sessionInfoStorage = sessionInfoStorage;
this.haltRequests = false;
this._accessToken = null;
}

setNavigation(navigation) {
this._navigation = navigation;
}

/**
* Set the access-token to be used within the service worker.
* @param token an access-token
*/
setAccessToken(token) {
this._accessToken = token;
}

registerAndStart(path) {
this._registrationPromise = (async () => {
navigator.serviceWorker.addEventListener("message", this);
Expand Down Expand Up @@ -88,24 +97,11 @@ export class ServiceWorkerHandler {
} else if (data.type === "openRoom") {
this._navigation.push("room", data.payload.roomId);
} else if (data.type === "getAccessToken") {
const token = await this._getLatestAccessToken();
const token = await this._accessToken;
event.source.postMessage({ replyTo: data.id, payload: token });
}
}

/**
* Fetch access-token from the storage
* @returns access token as string
*/
async _getLatestAccessToken() {
const currentSessionId = this._navigation?.path.get("session")?.value;
if (!currentSessionId) return null;
const { accessToken } = await this._sessionInfoStorage.get(
currentSessionId
);
return accessToken;
}

_closeSessionIfNeeded(sessionId) {
const currentSession = this._navigation?.path.get("session");
if (sessionId && currentSession?.value === sessionId) {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/web/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ async function handleRequest({ request, clientId }) {
"getAccessToken",
{}
);
if (!accessToken) {
throw new Error(
"token returned from getAccessToken message in sw.js is null"
);
}
headers.set("authorization", `Bearer ${accessToken}`);
request = new Request(request, {
mode: "cors",
Expand Down

0 comments on commit 7d726ad

Please sign in to comment.