2017-03-23 22:10:42 +00:00
|
|
|
import electron from 'electron';
|
2020-06-30 19:36:15 +00:00
|
|
|
import * as uuid from 'uuid';
|
2017-03-23 22:10:42 +00:00
|
|
|
import querystring from 'querystring';
|
|
|
|
|
2017-05-25 19:19:39 +00:00
|
|
|
const AUTH_WINDOW_SESSION_ID = uuid.v4();
|
|
|
|
|
2019-05-10 01:44:22 +00:00
|
|
|
export function responseToObject(body, keys, defaults = {}) {
|
2017-03-23 22:10:42 +00:00
|
|
|
let data = null;
|
|
|
|
try {
|
|
|
|
data = JSON.parse(body);
|
2018-06-25 17:42:50 +00:00
|
|
|
} catch (err) {}
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
try {
|
2019-05-10 01:44:22 +00:00
|
|
|
// NOTE: parse does not return a JS Object, so
|
|
|
|
// we cannot use hasOwnProperty on it
|
2017-03-23 22:10:42 +00:00
|
|
|
data = querystring.parse(body);
|
2018-06-25 17:42:50 +00:00
|
|
|
} catch (err) {}
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 01:44:22 +00:00
|
|
|
// Shouldn't happen but we'll check anyway
|
|
|
|
if (!data) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
2020-04-09 17:32:19 +00:00
|
|
|
const results = {};
|
2017-03-23 22:10:42 +00:00
|
|
|
for (const key of keys) {
|
2019-05-10 01:44:22 +00:00
|
|
|
if (data[key] !== undefined) {
|
|
|
|
results[key] = data[key];
|
|
|
|
} else if (defaults && defaults.hasOwnProperty(key)) {
|
|
|
|
results[key] = defaults[key];
|
|
|
|
} else {
|
|
|
|
results[key] = null;
|
|
|
|
}
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function authorizeUserInWindow(
|
2018-01-17 04:20:45 +00:00
|
|
|
url,
|
|
|
|
urlSuccessRegex = /(code=).*/,
|
2018-12-12 17:36:11 +00:00
|
|
|
urlFailureRegex = /(error=).*/,
|
2018-01-17 04:20:45 +00:00
|
|
|
) {
|
2017-03-23 22:10:42 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let finalUrl = null;
|
2018-01-16 06:08:46 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function _parseUrl(currentUrl) {
|
2018-01-16 06:08:46 +00:00
|
|
|
if (currentUrl.match(urlSuccessRegex)) {
|
2018-06-25 17:42:50 +00:00
|
|
|
console.log(
|
2018-12-12 17:36:11 +00:00
|
|
|
`[oauth2] Matched success redirect to "${currentUrl}" with ${urlSuccessRegex.toString()}`,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2018-01-16 06:08:46 +00:00
|
|
|
finalUrl = currentUrl;
|
|
|
|
child.close();
|
|
|
|
} else if (currentUrl.match(urlFailureRegex)) {
|
2018-06-25 17:42:50 +00:00
|
|
|
console.log(
|
2018-12-12 17:36:11 +00:00
|
|
|
`[oauth2] Matched error redirect to "${currentUrl}" with ${urlFailureRegex.toString()}`,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2018-01-17 06:04:47 +00:00
|
|
|
finalUrl = currentUrl;
|
2018-01-16 06:08:46 +00:00
|
|
|
child.close();
|
|
|
|
} else if (currentUrl === url) {
|
|
|
|
// It's the first one, so it's not a redirect
|
|
|
|
console.log(`[oauth2] Loaded "${currentUrl}"`);
|
|
|
|
} else {
|
2018-06-25 17:42:50 +00:00
|
|
|
console.log(
|
2018-12-12 17:36:11 +00:00
|
|
|
`[oauth2] Ignoring URL "${currentUrl}". Didn't match ${urlSuccessRegex.toString()}`,
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2018-01-16 06:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
// Create a child window
|
|
|
|
const child = new electron.remote.BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false,
|
2018-12-12 17:36:11 +00:00
|
|
|
partition: `oauth2_${AUTH_WINDOW_SESSION_ID}`,
|
2017-03-23 22:10:42 +00:00
|
|
|
},
|
2018-12-12 17:36:11 +00:00
|
|
|
show: false,
|
2017-03-23 22:10:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Finish on close
|
|
|
|
child.on('close', () => {
|
|
|
|
if (finalUrl) {
|
|
|
|
resolve(finalUrl);
|
|
|
|
} else {
|
2020-04-09 17:32:19 +00:00
|
|
|
const errorDescription = 'Authorization window closed';
|
2018-01-16 06:08:46 +00:00
|
|
|
reject(new Error(errorDescription));
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Catch the redirect after login
|
|
|
|
child.webContents.on('did-navigate', () => {
|
2017-11-03 18:43:40 +00:00
|
|
|
// Be sure to resolve URL so that we can handle redirects with no host like /foo/bar
|
2017-11-03 19:31:08 +00:00
|
|
|
const currentUrl = child.webContents.getURL();
|
2018-01-16 06:08:46 +00:00
|
|
|
_parseUrl(currentUrl);
|
|
|
|
});
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
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);
|
|
|
|
});
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2019-11-28 18:27:31 +00:00
|
|
|
const options = {};
|
|
|
|
|
|
|
|
// Force user-agent for GitHub until we update Chromium version. Note, we don't do this for
|
2019-12-17 17:16:08 +00:00
|
|
|
// everything because it breaks things. https://github.com/kong/insomnia/issues/1816
|
2019-11-28 18:27:31 +00:00
|
|
|
if (url.includes('github.com')) {
|
|
|
|
options.userAgent =
|
|
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, ' +
|
|
|
|
'like Gecko) Chrome/78.0.3904.108 Safari/537.36';
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
// Show the window to the user after it loads
|
|
|
|
child.on('ready-to-show', child.show.bind(child));
|
2019-11-28 18:27:31 +00:00
|
|
|
child.loadURL(url, options);
|
2017-03-23 22:10:42 +00:00
|
|
|
});
|
|
|
|
}
|