mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-02-02 07:31:38 +01:00
Merge pull request #1188 from element-hq/midhun/fix-audit-media-auth
Fix authenticated media breaking in certain SDK scenarios
This commit is contained in:
commit
d3e3d5a2c8
@ -289,6 +289,10 @@ export class Client {
|
|||||||
platform: this._platform,
|
platform: this._platform,
|
||||||
serverVersions: lastVersionsResponse.versions,
|
serverVersions: lastVersionsResponse.versions,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Let the serviceWorkerHandler know of this access-token
|
||||||
|
this._platform.updateService.setAccessToken(sessionInfo.accessToken);
|
||||||
|
|
||||||
this._session = new Session({
|
this._session = new Session({
|
||||||
storage: this._storage,
|
storage: this._storage,
|
||||||
sessionInfo: filteredSessionInfo,
|
sessionInfo: filteredSessionInfo,
|
||||||
@ -378,6 +382,7 @@ export class Client {
|
|||||||
throw Error("No session loaded, cannot update access token");
|
throw Error("No session loaded, cannot update access token");
|
||||||
}
|
}
|
||||||
this._session.updateAccessToken(token);
|
this._session.updateAccessToken(token);
|
||||||
|
await this._platform.updateService.setAccessToken(token);
|
||||||
await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token);
|
await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,12 +28,21 @@ export class ServiceWorkerHandler {
|
|||||||
this._currentController = null;
|
this._currentController = null;
|
||||||
this._sessionInfoStorage = sessionInfoStorage;
|
this._sessionInfoStorage = sessionInfoStorage;
|
||||||
this.haltRequests = false;
|
this.haltRequests = false;
|
||||||
|
this._accessToken = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
setNavigation(navigation) {
|
setNavigation(navigation) {
|
||||||
this._navigation = 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) {
|
registerAndStart(path) {
|
||||||
this._registrationPromise = (async () => {
|
this._registrationPromise = (async () => {
|
||||||
navigator.serviceWorker.addEventListener("message", this);
|
navigator.serviceWorker.addEventListener("message", this);
|
||||||
@ -88,24 +97,13 @@ export class ServiceWorkerHandler {
|
|||||||
} else if (data.type === "openRoom") {
|
} else if (data.type === "openRoom") {
|
||||||
this._navigation.push("room", data.payload.roomId);
|
this._navigation.push("room", data.payload.roomId);
|
||||||
} else if (data.type === "getAccessToken") {
|
} else if (data.type === "getAccessToken") {
|
||||||
const token = await this._getLatestAccessToken();
|
event.source.postMessage({
|
||||||
event.source.postMessage({ replyTo: data.id, payload: token });
|
replyTo: data.id,
|
||||||
|
payload: this._accessToken,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
_closeSessionIfNeeded(sessionId) {
|
||||||
const currentSession = this._navigation?.path.get("session");
|
const currentSession = this._navigation?.path.get("session");
|
||||||
if (sessionId && currentSession?.value === sessionId) {
|
if (sessionId && currentSession?.value === sessionId) {
|
||||||
|
@ -132,6 +132,11 @@ async function handleRequest({ request, clientId }) {
|
|||||||
"getAccessToken",
|
"getAccessToken",
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
if (!accessToken) {
|
||||||
|
throw new Error(
|
||||||
|
"Token returned from getAccessToken message in sw.js is null"
|
||||||
|
);
|
||||||
|
}
|
||||||
headers.set("authorization", `Bearer ${accessToken}`);
|
headers.set("authorization", `Bearer ${accessToken}`);
|
||||||
request = new Request(request, {
|
request = new Request(request, {
|
||||||
mode: "cors",
|
mode: "cors",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user