Skip to content

Commit 951abc8

Browse files
committed
MSC2918: save tokens in session on refresh
1 parent f278fcc commit 951abc8

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/matrix/SessionContainer.js

+15
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ export class SessionContainer {
185185
createTimeout: clock.createTimeout
186186
});
187187
if (this._tokenRefresher) {
188+
this._tokenRefresher.accessToken.subscribe(token => {
189+
this._platform.sessionInfoStorage.updateAccessToken(sessionInfo.id, token);
190+
});
191+
192+
this._tokenRefresher.accessTokenExpiresAt.subscribe(expiresAt => {
193+
this._platform.sessionInfoStorage.updateAccessTokenExpiresAt(sessionInfo.id, expiresAt);
194+
});
195+
196+
this._tokenRefresher.refreshToken.subscribe(token => {
197+
this._platform.sessionInfoStorage.updateRefreshToken(sessionInfo.id, token);
198+
});
199+
188200
await this._tokenRefresher.start(hsApi);
189201
}
190202
this._sessionId = sessionInfo.id;
@@ -325,6 +337,9 @@ export class SessionContainer {
325337
this._storage.close();
326338
this._storage = null;
327339
}
340+
if (this._tokenRefresher) {
341+
this._tokenRefresher.stop();
342+
}
328343
}
329344

330345
async deleteSession() {

src/matrix/net/TokenRefresher.js

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export class TokenRefresher {
2424
this._renewingLoop();
2525
}
2626

27+
stop() {
28+
// TODO
29+
}
30+
2731
get needsRenewing() {
2832
const remaining = this._accessTokenExpiresAt.get() - this._clock.now();
2933
const anticipated = remaining - this._anticipation;
@@ -64,6 +68,10 @@ export class TokenRefresher {
6468
return this._accessToken;
6569
}
6670

71+
get accessTokenExpiresAt() {
72+
return this._accessTokenExpiresAt;
73+
}
74+
6775
get refreshToken() {
6876
return this._refreshToken;
6977
}

src/matrix/sessioninfo/localstorage/SessionInfoStorage.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ export class SessionInfoStorage {
1919
this._name = name;
2020
}
2121

22-
getAll() {
22+
_getAllSync() {
2323
const sessionsJson = localStorage.getItem(this._name);
2424
if (sessionsJson) {
2525
const sessions = JSON.parse(sessionsJson);
2626
if (Array.isArray(sessions)) {
27-
return Promise.resolve(sessions);
27+
return sessions;
2828
}
2929
}
30-
return Promise.resolve([]);
30+
return [];
31+
}
32+
33+
async getAll() {
34+
return this._getAllSync();
3135
}
3236

3337
async updateLastUsed(id, timestamp) {
@@ -41,6 +45,40 @@ export class SessionInfoStorage {
4145
}
4246
}
4347

48+
// Update to the session tokens are all done synchronousely to avoid data races
49+
updateAccessToken(id, accessToken) {
50+
const sessions = this._getAllSync();
51+
if (sessions) {
52+
const session = sessions.find(session => session.id === id);
53+
if (session) {
54+
session.accessToken = accessToken;
55+
localStorage.setItem(this._name, JSON.stringify(sessions));
56+
}
57+
}
58+
}
59+
60+
updateAccessTokenExpiresAt(id, accessTokenExpiresAt) {
61+
const sessions = this._getAllSync();
62+
if (sessions) {
63+
const session = sessions.find(session => session.id === id);
64+
if (session) {
65+
session.accessTokenExpiresAt = accessTokenExpiresAt;
66+
localStorage.setItem(this._name, JSON.stringify(sessions));
67+
}
68+
}
69+
}
70+
71+
updateRefreshToken(id, refreshToken) {
72+
const sessions = this._getAllSync();
73+
if (sessions) {
74+
const session = sessions.find(session => session.id === id);
75+
if (session) {
76+
session.refreshToken = refreshToken;
77+
localStorage.setItem(this._name, JSON.stringify(sessions));
78+
}
79+
}
80+
}
81+
4482
async get(id) {
4583
const sessions = await this.getAll();
4684
if (sessions) {

0 commit comments

Comments
 (0)