insomnia/packages/insomnia-app/app/network/o-auth-2/misc.js
Gregory Schier 93c91ebdbe
Upgrade Electron (#2403)
* Upgrade Electron to 8, bump Node version, fix font-manager

* Specify nodeIntegration as true

* Get <webview> working again

* Get <webview> working again

* Electron 9.0

* Escape parens in plugin install exec path (newer Electron added them)

* Bump versions for first alpha

* Electron 9.1

* Convert all Electron APIs that switched from callback to Promise

* Fix send-and-download feature

* Remove user-agent override hack for OAuth 2 login window

* Bump alpha version

* Fix issue regarding chokidar

* Add package-lock.json

* Upgrade chokidar because @babel/cli uses an older incompatible version of fsevents

* Fix source maps

* Read .nvmrc in GitHub actions

* Address remaining PR feedback
2020-07-27 22:18:26 -07:00

107 lines
2.9 KiB
JavaScript

import electron from 'electron';
import * as uuid from 'uuid';
import querystring from 'querystring';
const AUTH_WINDOW_SESSION_ID = uuid.v4();
export function responseToObject(body, keys, defaults = {}) {
let data = null;
try {
data = JSON.parse(body);
} catch (err) {}
if (!data) {
try {
// NOTE: parse does not return a JS Object, so
// we cannot use hasOwnProperty on it
data = querystring.parse(body);
} catch (err) {}
}
// Shouldn't happen but we'll check anyway
if (!data) {
data = {};
}
const results = {};
for (const key of keys) {
if (data[key] !== undefined) {
results[key] = data[key];
} else if (defaults && defaults.hasOwnProperty(key)) {
results[key] = defaults[key];
} else {
results[key] = null;
}
}
return results;
}
export function authorizeUserInWindow(
url,
urlSuccessRegex = /(code=).*/,
urlFailureRegex = /(error=).*/,
) {
return new Promise((resolve, reject) => {
let finalUrl = null;
function _parseUrl(currentUrl) {
if (currentUrl.match(urlSuccessRegex)) {
console.log(
`[oauth2] Matched success redirect to "${currentUrl}" with ${urlSuccessRegex.toString()}`,
);
finalUrl = currentUrl;
child.close();
} else if (currentUrl.match(urlFailureRegex)) {
console.log(
`[oauth2] Matched error redirect to "${currentUrl}" with ${urlFailureRegex.toString()}`,
);
finalUrl = currentUrl;
child.close();
} else if (currentUrl === url) {
// It's the first one, so it's not a redirect
console.log(`[oauth2] Loaded "${currentUrl}"`);
} else {
console.log(
`[oauth2] Ignoring URL "${currentUrl}". Didn't match ${urlSuccessRegex.toString()}`,
);
}
}
// Create a child window
const child = new electron.remote.BrowserWindow({
webPreferences: {
nodeIntegration: false,
partition: `oauth2_${AUTH_WINDOW_SESSION_ID}`,
},
show: false,
});
// Finish on close
child.on('close', () => {
if (finalUrl) {
resolve(finalUrl);
} else {
const errorDescription = 'Authorization window closed';
reject(new Error(errorDescription));
}
});
// Catch the redirect after login
child.webContents.on('did-navigate', () => {
// Be sure to resolve URL so that we can handle redirects with no host like /foo/bar
const currentUrl = child.webContents.getURL();
_parseUrl(currentUrl);
});
child.webContents.on('did-fail-load', (e, errorCode, errorDescription, url) => {
// Listen for did-fail-load to be able to parse the URL even when the callback server is unreachable
_parseUrl(url);
});
// Show the window to the user after it loads
child.on('ready-to-show', child.show.bind(child));
child.loadURL(url);
});
}