Skip to content

Commit b41b31d

Browse files
committed
MSC2918: save tokens in session on refresh
1 parent 3f2eb3d commit b41b31d

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/matrix/SessionContainer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ export class SessionContainer {
192192
reconnector: this._reconnector,
193193
});
194194
if (this._tokenRefresher) {
195+
this._tokenRefresher.accessToken.subscribe(token => {
196+
this._platform.sessionInfoStorage.updateAccessToken(sessionInfo.id, token);
197+
});
198+
199+
this._tokenRefresher.accessTokenExpiresAt.subscribe(expiresAt => {
200+
this._platform.sessionInfoStorage.updateAccessTokenExpiresAt(sessionInfo.id, expiresAt);
201+
});
202+
203+
this._tokenRefresher.refreshToken.subscribe(token => {
204+
this._platform.sessionInfoStorage.updateRefreshToken(sessionInfo.id, token);
205+
});
206+
195207
await this._tokenRefresher.start(hsApi);
196208
}
197209
this._sessionId = sessionInfo.id;
@@ -331,6 +343,9 @@ export class SessionContainer {
331343
this._storage.close();
332344
this._storage = null;
333345
}
346+
if (this._tokenRefresher) {
347+
this._tokenRefresher.stop();
348+
}
334349
}
335350

336351
async deleteSession() {

src/matrix/net/TokenRefresher.js

Lines changed: 8 additions & 0 deletions
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

Lines changed: 41 additions & 3 deletions
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)