Skip to content

Commit

Permalink
host: Add JWT refetch for sessionRoom property (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace authored Jan 22, 2025
1 parent 2b25eb2 commit 9939a22
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/host/app/services/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,23 @@ class RealmResource {
return;
}

// token expiration is unix time (seconds)
let expirationMs = this.claims.exp * 1000;
let refreshMs = 0;

let refreshMs = Math.max(
expirationMs - Date.now() - tokenRefreshPeriodSec * 1000,
0,
);
if (!this.claims.sessionRoom) {
// Force JWT renewal to ensure presence of sessionRoom property
console.log(`JWT for realm ${this.url} has no session room, renewing`);
} else {
// token expiration is unix time (seconds)
let expirationMs = this.claims.exp * 1000;

refreshMs = Math.max(
expirationMs - Date.now() - tokenRefreshPeriodSec * 1000,
0,
);
}

await rawTimeout(refreshMs);

if (!this.loggingIn) {
this.loggingIn = this.loginTask.perform();
await this.loggingIn;
Expand Down
1 change: 1 addition & 0 deletions packages/host/tests/helpers/mock-matrix/_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class MockClient implements ExtendedClient {
exp: expires,
user: this.loggedInAs,
realm: realmURL.href,
sessionRoom: `test-session-room-for-${this.loggedInAs}`,
// adding a nonce to the test token so that we can tell the difference
// between different tokens created in the same second
nonce: nonce++,
Expand Down

0 comments on commit 9939a22

Please sign in to comment.