Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request authts#264 from authts/window-updates
Browse files Browse the repository at this point in the history
Additional `window` updates
  • Loading branch information
pamapa authored Dec 15, 2021
2 parents 2dfb04d + d034a25 commit c6c32a9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 127 deletions.
6 changes: 3 additions & 3 deletions src/CheckSessionIFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class CheckSessionIFrame {
private readonly _logger: Logger;
private _frame_origin: string;
private _frame: HTMLIFrameElement;
private _timer: number | null = null
private _timer: ReturnType<typeof setInterval> | null = null
private _session_state: string | null = null;

public constructor(
Expand Down Expand Up @@ -92,7 +92,7 @@ export class CheckSessionIFrame {
send();

// and setup timer
this._timer = window.setInterval(send, this._intervalInSeconds * 1000);
this._timer = setInterval(send, this._intervalInSeconds * 1000);
}

public stop(): void {
Expand All @@ -101,7 +101,7 @@ export class CheckSessionIFrame {
if (this._timer) {
this._logger.debug("stop");

window.clearInterval(this._timer);
clearInterval(this._timer);
this._timer = null;
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/SessionMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Logger, IntervalTimer, g_timer } from "./utils";
import { Logger } from "./utils";
import { CheckSessionIFrame } from "./CheckSessionIFrame";
import type { UserManager } from "./UserManager";
import type { User } from "./User";
Expand All @@ -13,7 +13,6 @@ export class SessionMonitor {
private readonly _logger: Logger;

private readonly _userManager: UserManager;
private readonly _timer: IntervalTimer;
private _sub: string | undefined;
private _sid: string | undefined;
private _checkSessionIFrame?: CheckSessionIFrame;
Expand All @@ -27,7 +26,6 @@ export class SessionMonitor {
}

this._userManager = userManager;
this._timer = g_timer;

this._userManager.events.addUserLoaded(this._start);
this._userManager.events.addUserUnloaded(this._stop);
Expand Down Expand Up @@ -125,8 +123,8 @@ export class SessionMonitor {
// using a timer to delay re-initialization to avoid race conditions during signout
// TODO rewrite to use promise correctly
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const timerHandle = this._timer.setInterval(async () => {
this._timer.clearInterval(timerHandle);
const timerHandle = setInterval(async () => {
clearInterval(timerHandle);

try {
const session = await this._userManager.querySessionStatus();
Expand Down
22 changes: 7 additions & 15 deletions src/utils/CryptoUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CryptoJS from "crypto-js/core.js";
import sha256 from "crypto-js/sha256.js";
import Base64 from "crypto-js/enc-base64.js";
import Utf8 from "crypto-js/enc-utf8.js";
Expand All @@ -10,26 +11,17 @@ const UUID_V4_TEMPLATE = "10000000-1000-4000-8000-100000000000";
* @internal
*/
export class CryptoUtils {

private static _cryptoUUIDv4(): string {
return UUID_V4_TEMPLATE.replace(/[018]/g, c =>
(+c ^ window.crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16),
);
}

private static _UUIDv4(): string {
return UUID_V4_TEMPLATE.replace(/[018]/g, c =>
(+c ^ Math.random() * 16 >> +c / 4).toString(16),
);
private static _randomWord(): number {
return CryptoJS.lib.WordArray.random(1).words[0];
}

/**
* Generates RFC4122 version 4 guid
*/
public static generateUUIDv4(): string {
const hasRandomValues = typeof window !== "undefined" && window.crypto &&
Object.prototype.hasOwnProperty.call(window.crypto, "getRandomValues");
const uuid = hasRandomValues ? CryptoUtils._cryptoUUIDv4() : CryptoUtils._UUIDv4();
const uuid = UUID_V4_TEMPLATE.replace(/[018]/g, c =>
(+c ^ CryptoUtils._randomWord() & 15 >> +c / 4).toString(16),
);
return uuid.replace(/-/g, "");
}

Expand All @@ -49,7 +41,7 @@ export class CryptoUtils {
return Base64.stringify(hashed).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}
catch (err) {
Logger.error("CryptoUtils", err instanceof Error ? err.message : err);
Logger.error("CryptoUtils", err);
throw err;
}
}
Expand Down
Loading

0 comments on commit c6c32a9

Please sign in to comment.