2019-10-17 17:27:41 +02:00
|
|
|
// @flow
|
2019-08-28 04:35:07 +02:00
|
|
|
import { ipcRenderer } from 'electron';
|
2019-11-20 21:57:38 +01:00
|
|
|
import { DOMAIN } from 'config';
|
2019-08-20 14:29:59 +02:00
|
|
|
|
2019-11-27 16:18:43 +01:00
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
2020-01-15 05:34:28 +01:00
|
|
|
const maxExpiration = 2147483647;
|
2019-10-15 23:23:51 +02:00
|
|
|
let sessionPassword;
|
|
|
|
|
2020-01-16 23:03:41 +01:00
|
|
|
function setCookie(name: string, value: string, expirationDaysOnWeb: number) {
|
2019-10-17 17:27:41 +02:00
|
|
|
let expires = '';
|
2020-01-16 23:03:41 +01:00
|
|
|
if (expirationDaysOnWeb) {
|
2019-10-17 17:27:41 +02:00
|
|
|
let date = new Date();
|
2020-01-16 23:03:41 +01:00
|
|
|
date.setTime(date.getTime() + expirationDaysOnWeb * 24 * 60 * 60 * 1000);
|
2020-01-15 05:34:28 +01:00
|
|
|
// If on PC, set to not expire (max)
|
|
|
|
expires = `expires=${IS_WEB ? date.toUTCString() : maxExpiration};`;
|
2019-10-17 17:27:41 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 16:18:43 +01:00
|
|
|
let cookie = `${name}=${value || ''}; ${expires} path=/; SameSite=Lax;`;
|
|
|
|
if (isProduction) {
|
|
|
|
cookie += ` domain=.${DOMAIN}; Secure;`;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.cookie = cookie;
|
2019-10-17 17:27:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getCookie(name: string) {
|
|
|
|
const nameEQ = name + '=';
|
|
|
|
const cookies = document.cookie.split(';');
|
|
|
|
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
let cookie = cookies[i];
|
|
|
|
while (cookie.charAt(0) === ' ') {
|
|
|
|
cookie = cookie.substring(1, cookie.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cookie.indexOf(nameEQ) === 0) {
|
|
|
|
return cookie.substring(nameEQ.length, cookie.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteCookie(name: string) {
|
2019-11-22 16:06:23 +01:00
|
|
|
document.cookie = name + `=; Max-Age=-99999999; domain=.${DOMAIN}; path=/;`;
|
2019-11-22 16:32:25 +01:00
|
|
|
|
|
|
|
// Legacy
|
|
|
|
// Adding this here to delete any old cookies before we switched to . + DOMAIN
|
|
|
|
// Remove this if you see it after July 1st, 2020
|
|
|
|
document.cookie = name + `=; Max-Age=-99999999; domain=${DOMAIN}; path=/;`;
|
2019-10-17 17:27:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const setSavedPassword = (value?: string, saveToDisk: boolean) => {
|
|
|
|
return new Promise<*>(resolve => {
|
2019-10-17 18:34:35 +02:00
|
|
|
const password = value === undefined || value === null ? '' : value;
|
|
|
|
sessionPassword = password;
|
|
|
|
|
|
|
|
if (saveToDisk) {
|
|
|
|
if (password) {
|
2019-10-22 19:57:32 +02:00
|
|
|
setCookie('saved-password', password, 14);
|
2019-10-17 18:34:35 +02:00
|
|
|
} else {
|
|
|
|
deleteSavedPassword();
|
|
|
|
}
|
2019-10-16 07:01:18 +02:00
|
|
|
}
|
2019-09-26 18:28:08 +02:00
|
|
|
});
|
2019-08-20 14:29:59 +02:00
|
|
|
};
|
|
|
|
|
2019-08-28 04:35:07 +02:00
|
|
|
export const getSavedPassword = () => {
|
2019-10-17 17:27:41 +02:00
|
|
|
return new Promise<*>(resolve => {
|
2019-10-15 23:23:51 +02:00
|
|
|
if (sessionPassword) {
|
|
|
|
resolve(sessionPassword);
|
|
|
|
}
|
2019-12-21 01:07:59 +01:00
|
|
|
|
|
|
|
return getKeychainPassword().then(p => resolve(p));
|
2019-12-19 23:35:12 +01:00
|
|
|
});
|
|
|
|
};
|
2019-10-15 23:23:51 +02:00
|
|
|
|
2019-12-19 23:35:12 +01:00
|
|
|
export const getKeychainPassword = () => {
|
|
|
|
return new Promise<*>(resolve => {
|
2020-01-16 23:03:41 +01:00
|
|
|
let password;
|
|
|
|
// @if TARGET='web'
|
|
|
|
// In the future, this will be the only code in this function
|
|
|
|
// Handling keytar stuff separately so we can easily rip it out later
|
|
|
|
password = getCookie('saved-password');
|
|
|
|
resolve(password);
|
|
|
|
// @endif
|
|
|
|
|
|
|
|
// @if TARGET='app'
|
|
|
|
password = getCookie('saved-password');
|
|
|
|
|
2020-01-15 05:34:28 +01:00
|
|
|
if (password) {
|
2019-09-26 18:28:08 +02:00
|
|
|
resolve(password);
|
2020-01-16 23:03:41 +01:00
|
|
|
} else {
|
|
|
|
// No password saved in a cookie, get it from the keychain, then delete the value in the keychain
|
|
|
|
ipcRenderer.once('get-password-response', (event, keychainPassword) => {
|
|
|
|
resolve(keychainPassword);
|
|
|
|
|
|
|
|
if (keychainPassword) {
|
|
|
|
setSavedPassword(keychainPassword, true);
|
|
|
|
ipcRenderer.send('delete-password');
|
|
|
|
}
|
2020-01-15 05:34:28 +01:00
|
|
|
});
|
2020-01-16 23:03:41 +01:00
|
|
|
|
2020-01-15 05:34:28 +01:00
|
|
|
ipcRenderer.send('get-password');
|
|
|
|
}
|
2019-10-15 06:20:12 +02:00
|
|
|
// @endif
|
2019-09-26 18:28:08 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteSavedPassword = () => {
|
2019-10-17 17:27:41 +02:00
|
|
|
return new Promise<*>(resolve => {
|
2019-10-22 19:57:32 +02:00
|
|
|
deleteCookie('saved-password');
|
|
|
|
resolve();
|
2019-09-26 18:28:08 +02:00
|
|
|
});
|
2019-08-26 22:18:30 +02:00
|
|
|
};
|
2019-08-20 14:29:59 +02:00
|
|
|
|
2019-10-17 17:27:41 +02:00
|
|
|
export const getAuthToken = () => {
|
|
|
|
return getCookie('auth_token');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setAuthToken = (value: string) => {
|
|
|
|
return setCookie('auth_token', value, 365);
|
|
|
|
};
|
|
|
|
|
2019-09-26 18:07:11 +02:00
|
|
|
export const deleteAuthToken = () => {
|
2019-10-17 17:27:41 +02:00
|
|
|
return new Promise<*>(resolve => {
|
2019-10-24 17:00:36 +02:00
|
|
|
deleteCookie('auth_token');
|
2019-09-26 18:28:08 +02:00
|
|
|
resolve();
|
|
|
|
});
|
2019-08-28 04:35:07 +02:00
|
|
|
};
|
|
|
|
|
2019-11-01 17:19:28 +01:00
|
|
|
export const doSignOutCleanup = () => {
|
2019-10-31 16:27:15 +01:00
|
|
|
return new Promise<*>(resolve => {
|
|
|
|
deleteCookie('auth_token');
|
|
|
|
deleteCookie('saved-password');
|
|
|
|
resolve();
|
2020-01-16 23:03:41 +01:00
|
|
|
|
|
|
|
// @if TARGET='app'
|
|
|
|
ipcRenderer.send('delete-auth-token');
|
|
|
|
ipcRenderer.send('delete-password');
|
|
|
|
// @endif;
|
2019-10-31 16:27:15 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-08-28 04:35:07 +02:00
|
|
|
export const testKeychain = () => {
|
|
|
|
// we should make sure it works on startup
|
2019-08-27 21:11:56 +02:00
|
|
|
};
|