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
|
|
|
|
2020-09-15 05:01:57 +00:00
|
|
|
function _parseUrl(currentUrl, source) {
|
2018-01-16 06:08:46 +00:00
|
|
|
if (currentUrl.match(urlSuccessRegex)) {
|
2018-06-25 17:42:50 +00:00
|
|
|
console.log(
|
2020-09-15 05:01:57 +00:00
|
|
|
`[oauth2] ${source}: 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(
|
2020-09-15 05:01:57 +00:00
|
|
|
`[oauth2] ${source}: 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
|
2020-09-15 05:01:57 +00:00
|
|
|
console.log(`[oauth2] ${source}: Loaded "${currentUrl}"`);
|
2018-01-16 06:08:46 +00:00
|
|
|
} else {
|
2018-06-25 17:42:50 +00:00
|
|
|
console.log(
|
2020-09-15 05:01:57 +00:00
|
|
|
`[oauth2] ${source}: 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();
|
2020-09-15 05:01:57 +00:00
|
|
|
_parseUrl(currentUrl, 'did-navigate');
|
|
|
|
});
|
|
|
|
|
|
|
|
child.webContents.on('will-redirect', (e, url) => {
|
|
|
|
// Also listen for will-redirect, as some redirections do not trigger 'did-navigate'
|
|
|
|
// 'will-redirect' does not cover all cases that 'did-navigate' does, so both events are required
|
|
|
|
// GitHub's flow triggers only 'did-navigate', while Microsoft's only 'will-redirect'
|
|
|
|
_parseUrl(url, 'will-redirect');
|
2018-01-16 06:08:46 +00:00
|
|
|
});
|
|
|
|
|
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
|
2020-09-15 05:01:57 +00:00
|
|
|
_parseUrl(url, 'did-fail-load');
|
2018-10-17 16:42:33 +00:00
|
|
|
});
|
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));
|
2020-07-28 05:18:26 +00:00
|
|
|
child.loadURL(url);
|
2017-03-23 22:10:42 +00:00
|
|
|
});
|
|
|
|
}
|