mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-24 20:14:53 +01:00
Stop the token refresher when disposing the client
This commit is contained in:
parent
ace7ad7065
commit
b3e6f4b494
@ -269,8 +269,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,
|
||||
@ -279,13 +278,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);
|
||||
}
|
||||
@ -500,6 +499,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;
|
||||
|
@ -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";
|
||||
@ -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>;
|
||||
|
@ -32,6 +32,7 @@ export class TokenRefresher {
|
||||
private _clock: Clock;
|
||||
private _oidcApi: OidcApi;
|
||||
private _timeout: Timeout
|
||||
private _running: boolean;
|
||||
|
||||
constructor({
|
||||
oidcApi,
|
||||
@ -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() {
|
||||
@ -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);
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user