Stop the token refresher when disposing the client

This commit is contained in:
Quentin Gliech 2022-03-03 16:27:43 +01:00
parent ace7ad7065
commit b3e6f4b494
No known key found for this signature in database
GPG Key ID: 22D62B84552719FC
3 changed files with 24 additions and 9 deletions

View File

@ -269,8 +269,7 @@ export class Client {
crypto: this._platform.crypto, crypto: this._platform.crypto,
}); });
// TODO: stop/pause the refresher? this._tokenRefresher = new TokenRefresher({
const tokenRefresher = new TokenRefresher({
oidcApi, oidcApi,
clock: this._platform.clock, clock: this._platform.clock,
accessToken: sessionInfo.accessToken, accessToken: sessionInfo.accessToken,
@ -279,13 +278,13 @@ export class Client {
anticipation: 30 * 1000, anticipation: 30 * 1000,
}); });
tokenRefresher.token.subscribe(t => { this._tokenRefresher.token.subscribe(t => {
this._platform.sessionInfoStorage.updateToken(sessionInfo.id, t.accessToken, t.accessTokenExpiresAt, t.refreshToken); 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 { } else {
accessToken = new ObservableValue(sessionInfo.accessToken); accessToken = new ObservableValue(sessionInfo.accessToken);
} }
@ -500,6 +499,10 @@ export class Client {
this._sync.stop(); this._sync.stop();
this._sync = null; this._sync = null;
} }
if (this._tokenRefresher) {
this._tokenRefresher.stop();
this._tokenRefresher = null;
}
if (this._session) { if (this._session) {
this._session.dispose(); this._session.dispose();
this._session = null; this._session = null;

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import type {RequestFunction} from "../../platform/types/types";
const WELL_KNOWN = ".well-known/openid-configuration"; const WELL_KNOWN = ".well-known/openid-configuration";
const RANDOM_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const RANDOM_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@ -53,7 +55,7 @@ function assert(condition: any, message: string): asserts condition {
export class OidcApi { export class OidcApi {
_issuer: string; _issuer: string;
_clientId: string; _clientId: string;
_requestFn: any; _requestFn: RequestFunction;
_encoding: any; _encoding: any;
_crypto: any; _crypto: any;
_metadataPromise: Promise<any>; _metadataPromise: Promise<any>;

View File

@ -32,6 +32,7 @@ export class TokenRefresher {
private _clock: Clock; private _clock: Clock;
private _oidcApi: OidcApi; private _oidcApi: OidcApi;
private _timeout: Timeout private _timeout: Timeout
private _running: boolean;
constructor({ constructor({
oidcApi, oidcApi,
@ -65,11 +66,15 @@ export class TokenRefresher {
await this.renew(); await this.renew();
} }
this._running = true;
this._renewingLoop(); this._renewingLoop();
} }
stop() { stop() {
// TODO this._running = false;
if (this._timeout) {
this._timeout.dispose();
}
} }
get needsRenewing() { get needsRenewing() {
@ -79,14 +84,19 @@ export class TokenRefresher {
} }
async _renewingLoop() { async _renewingLoop() {
while (true) { while (this._running) {
const remaining = const remaining =
this._token.get().accessTokenExpiresAt - this._clock.now(); this._token.get().accessTokenExpiresAt - this._clock.now();
const anticipated = remaining - this._anticipation; const anticipated = remaining - this._anticipation;
if (anticipated > 0) { if (anticipated > 0) {
this._timeout = this._clock.createTimeout(anticipated); this._timeout = this._clock.createTimeout(anticipated);
try {
await this._timeout.elapsed(); await this._timeout.elapsed();
} catch {
// The timeout will throw when aborted, so stop the loop if it is the case
return;
}
} }
await this.renew(); await this.renew();