Skip to content

Commit

Permalink
Stop the token refresher when disposing the client
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Mar 18, 2022
1 parent 9889234 commit 579ddd2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/matrix/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ export class Client {
crypto: this._platform.crypto,
});

// TODO: stop/pause the refresher?
const tokenRefresher = new TokenRefresher({
this._tokenRefresher = new TokenRefresher({
oidcApi,
clock: this._platform.clock,
accessToken: sessionInfo.accessToken,
Expand All @@ -278,13 +277,13 @@ export class Client {
anticipation: 30 * 1000,
});

tokenRefresher.token.subscribe(t => {
this._tokenRefresher.token.subscribe(t => {
this._platform.sessionInfoStorage.updateToken(sessionInfo.id, t.accessToken, t.accessTokenExpiresAt, t.refreshToken);
});

await tokenRefresher.start();
await this._tokenRefresher.start();

accessToken = tokenRefresher.accessToken;
accessToken = this._tokenRefresher.accessToken;
} else {
accessToken = new ObservableValue(sessionInfo.accessToken);
}
Expand Down Expand Up @@ -499,6 +498,10 @@ export class Client {
this._sync.stop();
this._sync = null;
}
if (this._tokenRefresher) {
this._tokenRefresher.stop();
this._tokenRefresher = null;
}
if (this._session) {
this._session.dispose();
this._session = null;
Expand Down
4 changes: 3 additions & 1 deletion src/matrix/net/OidcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import type {RequestFunction} from "../../platform/types/types";

const WELL_KNOWN = ".well-known/openid-configuration";

const RANDOM_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Expand Down Expand Up @@ -53,7 +55,7 @@ function assert(condition: any, message: string): asserts condition {
export class OidcApi {
_issuer: string;
_clientId: string;
_requestFn: any;
_requestFn: RequestFunction;
_encoding: any;
_crypto: any;
_metadataPromise: Promise<any>;
Expand Down
16 changes: 13 additions & 3 deletions src/matrix/net/TokenRefresher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class TokenRefresher {
private _clock: Clock;
private _oidcApi: OidcApi;
private _timeout: Timeout
private _running: boolean;

constructor({
oidcApi,
Expand Down Expand Up @@ -65,11 +66,15 @@ export class TokenRefresher {
await this.renew();
}

this._running = true;
this._renewingLoop();
}

stop() {
// TODO
this._running = false;
if (this._timeout) {
this._timeout.dispose();
}
}

get needsRenewing() {
Expand All @@ -79,14 +84,19 @@ export class TokenRefresher {
}

async _renewingLoop() {
while (true) {
while (this._running) {
const remaining =
this._token.get().accessTokenExpiresAt - this._clock.now();
const anticipated = remaining - this._anticipation;

if (anticipated > 0) {
this._timeout = this._clock.createTimeout(anticipated);
await this._timeout.elapsed();
try {
await this._timeout.elapsed();
} catch {
// The timeout will throw when aborted, so stop the loop if it is the case
return;
}
}

await this.renew();
Expand Down
2 changes: 1 addition & 1 deletion src/platform/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {ILogItem} from "../../logging/types";
export interface IRequestOptions {
uploadProgress?: (loadedBytes: number) => void;
timeout?: number;
body?: EncodedBody;
body?: EncodedBody["body"];
headers?: Map<string, string|number>;
cache?: boolean;
method?: string;
Expand Down

0 comments on commit 579ddd2

Please sign in to comment.