Skip to content

Commit

Permalink
feat: Upgrade to Electron 13 (#5549)
Browse files Browse the repository at this point in the history
* feat: Electron 13 with sso fix

* send close message

* use new webapp events
  • Loading branch information
thisisamir98 committed Feb 17, 2022
1 parent 3ef9f17 commit 0b7ab5e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 25 deletions.
48 changes: 40 additions & 8 deletions electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
OnHeadersReceivedListenerDetails,
WebContents,
} from 'electron';
import {WebAppEvents} from '@wireapp/webapp-events';
import * as fs from 'fs-extra';
import {getProxySettings} from 'get-proxy-settings';
import logdown from 'logdown';
Expand Down Expand Up @@ -117,7 +118,7 @@ if (argv[config.ARGUMENT.PROXY_SERVER] || fileBasedProxyConfig) {
throw new Error('No protocol for the proxy server specified.');
}
} catch (error) {
logger.error(`Could not parse authenticated proxy URL: "${error.message}"`);
logger.error(`Could not parse authenticated proxy URL: "${(error as any).message}"`);
}
}

Expand Down Expand Up @@ -323,7 +324,7 @@ const showMainWindow = async (mainWindowState: windowStateKeeper.State): Promise
try {
main.reload();
} catch (error) {
showErrorDialog(`Could not reload the window: ${error.message}`);
showErrorDialog(`Could not reload the window: ${(error as any).message}`);
logger.error('Could not reload the window:', error);
}
});
Expand All @@ -334,7 +335,7 @@ const showMainWindow = async (mainWindowState: windowStateKeeper.State): Promise
try {
main.reload();
} catch (error) {
showErrorDialog(`Could not reload the window: ${error.message}`);
showErrorDialog(`Could not reload the window: ${(error as any).message}`);
logger.error('Could not reload the window:', error);
}
});
Expand Down Expand Up @@ -419,7 +420,7 @@ const handleAppEvents = (): void => {
try {
main.reload();
} catch (error) {
showErrorDialog(`Could not reload the window: ${error.message}`);
showErrorDialog(`Could not reload the window: ${(error as any).message}`);
logger.error('Could not reload the window:', error);
}
});
Expand Down Expand Up @@ -454,7 +455,7 @@ const renameFileExtensions = (files: string[], oldExtension: string, newExtensio
fs.renameSync(file, file.replace(oldExtension, newExtension));
}
} catch (error) {
logger.error(`Failed to rename log file: "${error.message}"`);
logger.error(`Failed to rename log file: "${(error as any).message}"`);
}
}
};
Expand All @@ -465,7 +466,7 @@ const renameWebViewLogFiles = (): void => {
const logFiles = getLogFilenames(LOG_DIR, true);
renameFileExtensions(logFiles, '.log', '.old');
} catch (error) {
logger.log(`Failed to read log directory with error: ${error.message}`);
logger.log(`Failed to read log directory with error: ${(error as any).message}`);
}
};

Expand Down Expand Up @@ -509,16 +510,39 @@ const applyProxySettings = async (authenticatedProxyDetails: URL, webContents: E

class ElectronWrapperInit {
logger: logdown.Logger;
ssoWindow: SingleSignOn | null;

constructor() {
this.logger = getLogger('ElectronWrapperInit');
this.ssoWindow = null;
ipcMain.on(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSE, this.closeSSOWindow);
ipcMain.on(WebAppEvents.LIFECYCLE.SSO_WINDOW_FOCUS, this.focusSSOWindow);
}

async run(): Promise<void> {
this.logger.log('webviewProtection init');
this.webviewProtection();
}

closeSSOWindow = () => {
if (this.ssoWindow) {
this.ssoWindow?.close();
this.ssoWindow = null;
}
};

focusSSOWindow = () => {
if (this.ssoWindow) {
this.ssoWindow.focus();
}
};

sendSSOWindowCloseEvent = () => {
if (this.ssoWindow) {
main.webContents.send(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSED);
}
};

// <webview> hardening
webviewProtection(): void {
const openLinkInNewWindow = (
Expand All @@ -531,7 +555,15 @@ class ElectronWrapperInit {
event.preventDefault();

if (SingleSignOn.isSingleSignOnLoginWindow(frameName)) {
return new SingleSignOn(main, event, url, options).init();
const singleSignOn = new SingleSignOn(main, event, url, options).init();
return new Promise(() => {
singleSignOn
.then(sso => {
this.ssoWindow = sso;
this.ssoWindow.onClose = this.sendSSOWindowCloseEvent;
})
.catch(error => console.info(error));
});
}

this.logger.log('Opening an external window from a webview.');
Expand Down Expand Up @@ -601,7 +633,7 @@ class ElectronWrapperInit {
logFilePath,
);
} catch (error) {
logger.error(`Cannot write to log file "${logFilePath}": ${error.message}`, error);
logger.error(`Cannot write to log file "${logFilePath}": ${(error as any).message}`, error);
}
}
});
Expand Down
7 changes: 7 additions & 0 deletions electron/src/preload/preload-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ const subscribeToMainProcessEvents = (): void => {
}
});

ipcRenderer.on(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSED, async () => {
const selectedWebview = getSelectedWebview();
if (selectedWebview) {
await selectedWebview.send(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSED);
}
});

ipcRenderer.on(EVENT_TYPE.WEBAPP.CHANGE_LOCATION_HASH, async (_event, hash: string) => {
const selectedWebview = getSelectedWebview();
if (selectedWebview) {
Expand Down
14 changes: 14 additions & 0 deletions electron/src/preload/preload-webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ const subscribeToWebappEvents = (): void => {
ipcRenderer.sendToHost(EVENT_TYPE.LIFECYCLE.SIGNED_OUT, clearData);
});

window.amplify.subscribe(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSE, () => {
logger.info(`Received amplify event "${WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSE}" event`);
ipcRenderer.send(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSE);
});

window.amplify.subscribe(WebAppEvents.LIFECYCLE.SSO_WINDOW_FOCUS, () => {
logger.info(`Received amplify event "${WebAppEvents.LIFECYCLE.SSO_WINDOW_FOCUS}" event`);
ipcRenderer.send(WebAppEvents.LIFECYCLE.SSO_WINDOW_FOCUS);
});

window.amplify.subscribe(WebAppEvents.LIFECYCLE.UNREAD_COUNT, (count: string) => {
logger.info(
`Received amplify event "${WebAppEvents.LIFECYCLE.UNREAD_COUNT}" (count: "${count}"), forwarding event ...`,
Expand Down Expand Up @@ -181,6 +191,10 @@ const subscribeToMainProcessEvents = (): void => {
logger.info(`Received event "${EVENT_TYPE.WRAPPER.UPDATE_AVAILABLE}", forwarding to amplify ...`);
window.amplify.publish(WebAppEvents.LIFECYCLE.UPDATE, window.z.lifecycle.UPDATE_SOURCE.DESKTOP);
});
ipcRenderer.on(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSED, () => {
logger.info(`Received event "${WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSED}", forwarding to window ...`);
window.amplify.publish(WebAppEvents.LIFECYCLE.SSO_WINDOW_CLOSED);
});
ipcRenderer.on(EVENT_TYPE.ACTION.JOIN_CONVERSATION, (_event, {code, key}: {code: string; key: string}) => {
logger.info(`Received event "${EVENT_TYPE.ACTION.JOIN_CONVERSATION}", forwarding to host ...`);
ipcRenderer.sendToHost(EVENT_TYPE.ACTION.JOIN_CONVERSATION, {code, key});
Expand Down
26 changes: 25 additions & 1 deletion electron/src/sso/SingleSignOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class SingleSignOn {
private readonly senderWebContents: WebContents;
private readonly windowOptions: BrowserWindowConstructorOptions;
private readonly windowOriginUrl: URL;
public onClose = () => {};

constructor(
mainBrowserWindow: BrowserWindow,
Expand All @@ -89,7 +90,7 @@ export class SingleSignOn {
this.windowOriginUrl = new URL(windowOriginURL);
}

public readonly init = async (): Promise<void> => {
public readonly init = async (): Promise<SingleSignOn> => {
// Create a ephemeral and isolated session
this.session = session.fromPartition(SingleSignOn.SSO_SESSION_NAME, {cache: false});

Expand All @@ -114,6 +115,7 @@ export class SingleSignOn {
if (typeof argv[config.ARGUMENT.DEVTOOLS] !== 'undefined') {
this.ssoWindow.webContents.openDevTools({mode: 'detach'});
}
return this;
};

private createBrowserWindow(): BrowserWindow {
Expand Down Expand Up @@ -177,6 +179,7 @@ export class SingleSignOn {
throw new Error('Failed to unregister protocol');
}
}
this.onClose();
this.session = undefined;
this.ssoWindow = undefined;
});
Expand Down Expand Up @@ -212,6 +215,27 @@ export class SingleSignOn {
return ssoWindow;
}

close = () => {
(async () => {
if (this.session) {
await this.wipeSessionData();
const unregisterSuccess = SingleSignOn.unregisterProtocol(this.session);
if (!unregisterSuccess) {
throw new Error('Failed to unregister protocol');
}
}
this.ssoWindow?.close();
this.session = undefined;
this.ssoWindow = undefined;
})()
.then(console.info)
.catch(console.info);
};

focus = () => {
this.ssoWindow?.focus();
};

// Ensure authenticity of the window from within the code
public static isSingleSignOnLoginWindow = (frameName: string) => SingleSignOn.SINGLE_SIGN_ON_FRAME_NAME === frameName;

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@wireapp/commons": "4.2.7",
"@wireapp/protocol-messaging": "1.35.0",
"@wireapp/react-ui-kit": "7.44.2",
"@wireapp/webapp-events": "0.11.0",
"@wireapp/webapp-events": "0.12.0",
"auto-launch": "5.0.5",
"axios": "0.21.1",
"content-type": "1.0.4",
Expand Down Expand Up @@ -70,7 +70,7 @@
"cspell": "5.6.6",
"css-loader": "5.2.6",
"dotenv": "10.0.0",
"electron": "11.5.0",
"electron": "13.6.7",
"electron-builder": "20.44.4",
"electron-mocha": "10.1.0",
"electron-osx-sign": "0.5.0",
Expand Down
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1943,16 +1943,16 @@
resolved "https://registry.npmjs.org/@types/node/-/node-14.14.20.tgz#f7974863edd21d1f8a494a73e8e2b3658615c340"
integrity sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A==

"@types/node@^12.0.12":
version "12.20.15"
resolved "https://registry.npmjs.org/@types/node/-/node-12.20.15.tgz#10ee6a6a3f971966fddfa3f6e89ef7a73ec622df"
integrity sha512-F6S4Chv4JicJmyrwlDkxUdGNSplsQdGwp1A0AJloEVDirWdZOAiRHhovDlsFkKUrquUXhz1imJhXHsf59auyAg==

"@types/node@^13.7.0":
version "13.13.34"
resolved "https://registry.npmjs.org/@types/node/-/node-13.13.34.tgz#c9300a1b6560d90817fb2bba650e250116a575f9"
integrity sha512-g8D1HF2dMDKYSDl5+79izRwRgNPsSynmWMbj50mj7GZ0b7Lv4p8EmZjbo3h0h+6iLr6YmVz9VnF6XVZ3O6V1Ug==

"@types/node@^14.6.2":
version "14.18.9"
resolved "https://registry.npmjs.org/@types/node/-/node-14.18.9.tgz#0e5944eefe2b287391279a19b407aa98bd14436d"
integrity sha512-j11XSuRuAlft6vLDEX4RvhqC0KxNxx6QIyMXNb0vHHSNPXTPeiy3algESWmOOIzEtiEL0qiowPU3ewW9hHVa7Q==

"@types/open-graph@0.2.1":
version "0.2.1"
resolved "https://registry.npmjs.org/@types/open-graph/-/open-graph-0.2.1.tgz#8df5b19fd989dcfaf585c8424b1045917e365cd1"
Expand Down Expand Up @@ -2383,10 +2383,10 @@
emotion-theming "10.0.27"
react-transition-group "4.4.1"

"@wireapp/webapp-events@0.11.0":
version "0.11.0"
resolved "https://registry.npmjs.org/@wireapp/webapp-events/-/webapp-events-0.11.0.tgz#c95dd482d194b4cef68c096a7aa440581d2ae189"
integrity sha512-CcEb4B1JRvr0Pqm7AWk4FzQKXZbISOffGchXkEDNcBj+nsixLkRQAfNqecDzvZogYRSfI4TvCC/gEFhybt6LWA==
"@wireapp/webapp-events@0.12.0":
version "0.12.0"
resolved "https://registry.npmjs.org/@wireapp/webapp-events/-/webapp-events-0.12.0.tgz#ba821bc9f77af67600a19b4102458d7d1c671060"
integrity sha512-cw1VmJpRPCl3AfaTZDvdBW+5y7kTa+xZFbRzA/FN3oFFq1iDzRdIVj9q1NWwTihym8gC2YUubcRAAMaOS6vZDw==

"@xtuc/ieee754@^1.2.0":
version "1.2.0"
Expand Down Expand Up @@ -4185,13 +4185,13 @@ electron-winstaller@4.0.1:
lodash.template "^4.2.2"
temp "^0.9.0"

electron@11.5.0:
version "11.5.0"
resolved "https://registry.npmjs.org/electron/-/electron-11.5.0.tgz#f1650543b9d8f2047d3807755bdb120153ed210f"
integrity sha512-WjNDd6lGpxyiNjE3LhnFCAk/D9GIj1rU3GSDealVShhkkkPR3Vh4q8ErXGDl1OAO/faomVa10KoFPUN/pLbNxg==
electron@13.6.7:
version "13.6.7"
resolved "https://registry.npmjs.org/electron/-/electron-13.6.7.tgz#7e28c54c67ef867426216bd8334bf79bc9ef7d2a"
integrity sha512-gzCru/TpyCZ3yRjR/TVKph6Q/rmFZnLszxoeESaiijHjobB0xxr2oTHQLGVhpali2ddrM+4Pz6+MyZwS6Us+xA==
dependencies:
"@electron/get" "^1.0.1"
"@types/node" "^12.0.12"
"@types/node" "^14.6.2"
extract-zip "^1.0.3"

emittery@^0.8.1:
Expand Down

0 comments on commit 0b7ab5e

Please sign in to comment.