Skip to content

Commit

Permalink
Anticipate safeStorage to be polyfilled with async functions (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
GintV authored Feb 8, 2024
1 parent 8cdcec1 commit 678d220
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Anticipate safeStorage to be polyfilled with async functions",
"packageName": "@itwin/electron-authorization",
"email": "[email protected]",
"dependentChangeType": "patch"
}
10 changes: 6 additions & 4 deletions packages/electron/src/main/TokenStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ export class RefreshTokenStore {
return this._userName;
}

private encryptRefreshToken(token: string): Buffer {
// Note: this is intentionally made async in case this code doesn't run in electron's main process and safeStorage must be polyfilled with async function
private async encryptRefreshToken(token: string): Promise<Buffer> {
return safeStorage.encryptString(token);
}

private decryptRefreshToken(encryptedToken: Buffer): string {
// Note: this is intentionally made async in case this code doesn't run in electron's main process and safeStorage must be polyfilled with async function
private async decryptRefreshToken(encryptedToken: Buffer): Promise<string> {
return safeStorage.decryptString(Buffer.from(encryptedToken));
}

Expand All @@ -74,7 +76,7 @@ export class RefreshTokenStore {
return undefined;
}
const encryptedToken = this._store.get(key);
const refreshToken = this.decryptRefreshToken(encryptedToken);
const refreshToken = await this.decryptRefreshToken(encryptedToken);
return refreshToken;
}

Expand All @@ -83,7 +85,7 @@ export class RefreshTokenStore {
const userName = await this.getUserName();
if (!userName)
return;
const encryptedToken = this.encryptRefreshToken(refreshToken);
const encryptedToken = await this.encryptRefreshToken(refreshToken);
const key = await this.getKey();
this._store.set(key, encryptedToken);
}
Expand Down

0 comments on commit 678d220

Please sign in to comment.