From 15fd73ed9c1486c33ecac7061f86edbd2309ea64 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 9 Nov 2023 09:56:56 +0000 Subject: [PATCH 1/4] Add ability to adjust the token on the homeserver API. --- src/matrix/Session.js | 9 +++++++++ src/matrix/net/HomeServerApi.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 3fc3b00638..348b626b7b 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -1094,6 +1094,15 @@ export class Session { return body.room_id; }); } + + /** + * Updates the access token used by the API. Does NOT + * change the token in storage. + * @param {string} token + */ + updateAccessToken(token) { + this._hsApi.updateAccessToken(token); + } } import {FeatureSet} from "../features"; diff --git a/src/matrix/net/HomeServerApi.ts b/src/matrix/net/HomeServerApi.ts index eebc692acd..50e51c21f9 100644 --- a/src/matrix/net/HomeServerApi.ts +++ b/src/matrix/net/HomeServerApi.ts @@ -46,7 +46,7 @@ type BaseRequestOptions = { export class HomeServerApi { private readonly _homeserver: string; - private readonly _accessToken: string; + private _accessToken: string; private readonly _requestFn: RequestFunction; private readonly _reconnector: Reconnector; @@ -125,6 +125,14 @@ export class HomeServerApi { return this._authedRequest("GET", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options); } + /** + * Update the access token used by the API. + * @param token + */ + public updateAccessToken(token: string) { + this._accessToken = token; + } + sync(since: string, filter: string, timeout: number, options?: BaseRequestOptions): IHomeServerRequest { return this._get("/sync", {since, timeout, filter}, undefined, options); } From 44f20d54b083e3fec508611d6da29fdcae36bde1 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Thu, 9 Nov 2023 10:05:56 +0000 Subject: [PATCH 2/4] Actually store in storage --- src/matrix/Client.js | 10 +++++++++ .../localstorage/SessionInfoStorage.ts | 21 +++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 0eb9f6e0df..4d4cac00be 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -337,6 +337,16 @@ export class Client { } } + /** + * Update the access token in use by the client. + * Will also update the token in session storage. + * @param {string} token A Matrix Access Token + */ + async updateAccessToken(token) { + this._session.updateAccessToken(token); + await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token); + } + async _waitForFirstSync() { this._sync.start(); this._status.set(LoadStatus.FirstSync); diff --git a/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts b/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts index d6596a6147..795333bc63 100644 --- a/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts +++ b/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts @@ -37,6 +37,7 @@ interface ISessionInfo { interface ISessionInfoStorage { getAll(): Promise; updateLastUsed(id: string, timestamp: number): Promise; + updateAccessToken(id: string, token: string): Promise; get(id: string): Promise; add(sessionInfo: ISessionInfo): Promise; delete(sessionId: string): Promise; @@ -60,14 +61,22 @@ export class SessionInfoStorage implements ISessionInfoStorage { return Promise.resolve([]); } + async updateAccessToken(id: string, accessToken: string): Promise { + const sessions = await this.getAll(); + const session = sessions.find(session => session.id === id); + if (!session) { + throw Error('No session found'); + } + session.accessToken = accessToken; + localStorage.setItem(this._name, JSON.stringify(sessions)); + } + async updateLastUsed(id: string, timestamp: number): Promise { const sessions = await this.getAll(); - if (sessions) { - const session = sessions.find(session => session.id === id); - if (session) { - session.lastUsed = timestamp; - localStorage.setItem(this._name, JSON.stringify(sessions)); - } + const session = sessions.find(session => session.id === id); + if (session) { + session.lastUsed = timestamp; + localStorage.setItem(this._name, JSON.stringify(sessions)); } } From e5b87a85189ac98b2c57c65e4ae25058ea7b522f Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Tue, 14 Nov 2023 09:59:33 +0000 Subject: [PATCH 3/4] Throw if session hasn't been loaded --- src/matrix/Client.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 4d4cac00be..65a1074d7a 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -343,6 +343,9 @@ export class Client { * @param {string} token A Matrix Access Token */ async updateAccessToken(token) { + if (!_this.session) { + throw Error("No session loaded, cannot update access token"); + } this._session.updateAccessToken(token); await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token); } From f45e56ba732551052c9d4921a7e245b79dee18e9 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 22 Nov 2023 17:31:06 +0000 Subject: [PATCH 4/4] fix check --- src/matrix/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 65a1074d7a..7423b43112 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -343,7 +343,7 @@ export class Client { * @param {string} token A Matrix Access Token */ async updateAccessToken(token) { - if (!_this.session) { + if (!this._session) { throw Error("No session loaded, cannot update access token"); } this._session.updateAccessToken(token);