Skip to content

Commit f5f493d

Browse files
committed
Generate & pass an auth token to the server & ui
1 parent 80bed9b commit f5f493d

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

package-lock.json

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"electron-squirrel-startup": "^1.0.0",
2929
"electron-window-state": "^5.0.3",
3030
"tslib": "^1.10.0",
31-
"universal-analytics": "^0.4.20"
31+
"universal-analytics": "^0.4.20",
32+
"uuid": "^3.3.3"
3233
},
3334
"devDependencies": {
3435
"@types/electron-devtools-installer": "^2.0.2",
@@ -37,6 +38,7 @@
3738
"@types/node": "^12.12.14",
3839
"@types/node-fetch": "^2.1.4",
3940
"@types/universal-analytics": "^0.4.3",
41+
"@types/uuid": "^3.4.6",
4042
"babel-plugin-transform-async-to-generator": "^6.24.1",
4143
"babel-preset-env": "^1.7.0",
4244
"electron": "^4.1.4",

src/index.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ function reportError(error: Error | string) {
1313
import { spawn, exec, ChildProcess } from 'child_process';
1414
import * as os from 'os';
1515
import * as path from 'path';
16+
import * as querystring from 'querystring';
1617
import { app, BrowserWindow, shell, Menu, dialog } from 'electron';
18+
import * as uuid from 'uuid/v4';
1719

1820
import * as windowStateKeeper from 'electron-window-state';
1921

@@ -30,6 +32,8 @@ const packageJson = require('../package.json');
3032
const isWindows = os.platform() === 'win32';
3133

3234
const APP_URL = process.env.APP_URL || 'https://app.httptoolkit.tech';
35+
const AUTH_TOKEN = uuid();
36+
const DESKTOP_VERSION = packageJson.version;
3337

3438
// Keep a global reference of the window object, if you don't, the window will
3539
// be closed automatically when the JavaScript object is garbage collected.
@@ -72,7 +76,10 @@ const createWindow = () => {
7276

7377
windowState.manage(window);
7478

75-
window.loadURL(APP_URL);
79+
window.loadURL(APP_URL + '?' + querystring.stringify({
80+
authToken: AUTH_TOKEN,
81+
desktopVersion: DESKTOP_VERSION
82+
}));
7683

7784
window.on('ready-to-show', function () {
7885
window!.show();
@@ -152,14 +159,27 @@ if (!amMainInstance) {
152159
});
153160

154161
app.on('web-contents-created', (_event, contents) => {
155-
contents.on('dom-ready', () => {
156-
// Define & announce the desktop shell version to the app
157-
// Not used now, intended to allow us to detect and prompt for updates
158-
// in future, if a certain desktop shell version is required.
162+
function injectValue(name: string, value: string) {
163+
// Set a variable globally, and self-postmessage it too (to ping
164+
// anybody who's explicitly waiting for it).
159165
contents.executeJavaScript(`
160-
window.httpToolkitDesktopVersion = '${packageJson.version}';
161-
window.postMessage({ httpToolkitDesktopVersion: window.httpToolkitDesktopVersion }, '*');
166+
window.${name} = '${value}';
167+
window.postMessage({ ${name}: window.${name} }, '*');
162168
`);
169+
}
170+
171+
contents.on('dom-ready', () => {
172+
// Define & announce config values to the app.
173+
174+
// Desktop version isn't used yet. Intended to allow us to detect
175+
// and prompt for updates in future if a certain desktop version
176+
// is required, and for error reporting context when things go wrong.
177+
injectValue('httpToolkitDesktopVersion', DESKTOP_VERSION);
178+
179+
// Auth token is also injected into query string, but query string
180+
// gets replaced on first navigation (immediately), whilst global
181+
// vars like this are forever.
182+
injectValue('httpToolkitAuthToken', AUTH_TOKEN);
163183
});
164184

165185
// Redirect all navigations & new windows to the system browser
@@ -192,7 +212,7 @@ if (!amMainInstance) {
192212
const serverBinPath = path.join(__dirname, '..', 'httptoolkit-server', 'bin', binName);
193213
const serverBinCommand = isWindows ? `"${serverBinPath}"` : serverBinPath;
194214

195-
server = spawn(serverBinCommand, ['start'], {
215+
server = spawn(serverBinCommand, ['start', '--token', AUTH_TOKEN], {
196216
windowsHide: true,
197217
stdio: ['inherit', 'pipe', 'pipe'],
198218
shell: isWindows, // Required to spawn a .cmd script

0 commit comments

Comments
 (0)