Skip to content

Commit

Permalink
MSC2918: save tokens in session on refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed May 20, 2021
1 parent 3f2eb3d commit b41b31d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/matrix/SessionContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ export class SessionContainer {
reconnector: this._reconnector,
});
if (this._tokenRefresher) {
this._tokenRefresher.accessToken.subscribe(token => {
this._platform.sessionInfoStorage.updateAccessToken(sessionInfo.id, token);
});

this._tokenRefresher.accessTokenExpiresAt.subscribe(expiresAt => {
this._platform.sessionInfoStorage.updateAccessTokenExpiresAt(sessionInfo.id, expiresAt);
});

this._tokenRefresher.refreshToken.subscribe(token => {
this._platform.sessionInfoStorage.updateRefreshToken(sessionInfo.id, token);
});

await this._tokenRefresher.start(hsApi);
}
this._sessionId = sessionInfo.id;
Expand Down Expand Up @@ -331,6 +343,9 @@ export class SessionContainer {
this._storage.close();
this._storage = null;
}
if (this._tokenRefresher) {
this._tokenRefresher.stop();
}
}

async deleteSession() {
Expand Down
8 changes: 8 additions & 0 deletions src/matrix/net/TokenRefresher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class TokenRefresher {
this._renewingLoop();
}

stop() {
// TODO
}

get needsRenewing() {
const remaining = this._accessTokenExpiresAt.get() - this._clock.now();
const anticipated = remaining - this._anticipation;
Expand Down Expand Up @@ -64,6 +68,10 @@ export class TokenRefresher {
return this._accessToken;
}

get accessTokenExpiresAt() {
return this._accessTokenExpiresAt;
}

get refreshToken() {
return this._refreshToken;
}
Expand Down
44 changes: 41 additions & 3 deletions src/matrix/sessioninfo/localstorage/SessionInfoStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ export class SessionInfoStorage {
this._name = name;
}

getAll() {
_getAllSync() {
const sessionsJson = localStorage.getItem(this._name);
if (sessionsJson) {
const sessions = JSON.parse(sessionsJson);
if (Array.isArray(sessions)) {
return Promise.resolve(sessions);
return sessions;
}
}
return Promise.resolve([]);
return [];
}

async getAll() {
return this._getAllSync();
}

async updateLastUsed(id, timestamp) {
Expand All @@ -41,6 +45,40 @@ export class SessionInfoStorage {
}
}

// Update to the session tokens are all done synchronousely to avoid data races
updateAccessToken(id, accessToken) {
const sessions = this._getAllSync();
if (sessions) {
const session = sessions.find(session => session.id === id);
if (session) {
session.accessToken = accessToken;
localStorage.setItem(this._name, JSON.stringify(sessions));
}
}
}

updateAccessTokenExpiresAt(id, accessTokenExpiresAt) {
const sessions = this._getAllSync();
if (sessions) {
const session = sessions.find(session => session.id === id);
if (session) {
session.accessTokenExpiresAt = accessTokenExpiresAt;
localStorage.setItem(this._name, JSON.stringify(sessions));
}
}
}

updateRefreshToken(id, refreshToken) {
const sessions = this._getAllSync();
if (sessions) {
const session = sessions.find(session => session.id === id);
if (session) {
session.refreshToken = refreshToken;
localStorage.setItem(this._name, JSON.stringify(sessions));
}
}
}

async get(id) {
const sessions = await this.getAll();
if (sessions) {
Expand Down

0 comments on commit b41b31d

Please sign in to comment.