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 ability to adjust the token on the homeserver API. #1153

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions src/matrix/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Half-Shot marked this conversation as resolved.
Show resolved Hide resolved
await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token);
}

async _waitForFirstSync() {
this._sync.start();
this._status.set(LoadStatus.FirstSync);
Expand Down
9 changes: 9 additions & 0 deletions src/matrix/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 9 additions & 1 deletion src/matrix/net/HomeServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 15 additions & 6 deletions src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface ISessionInfo {
interface ISessionInfoStorage {
getAll(): Promise<ISessionInfo[]>;
updateLastUsed(id: string, timestamp: number): Promise<void>;
updateAccessToken(id: string, token: string): Promise<void>;
get(id: string): Promise<ISessionInfo | undefined>;
add(sessionInfo: ISessionInfo): Promise<void>;
delete(sessionId: string): Promise<void>;
Expand All @@ -60,14 +61,22 @@ export class SessionInfoStorage implements ISessionInfoStorage {
return Promise.resolve([]);
}

async updateAccessToken(id: string, accessToken: string): Promise<void> {
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<void> {
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));
}
}

Expand Down
Loading