lbry-desktop/ui/util/saved-passwords.js

145 lines
3.3 KiB
JavaScript
Raw Normal View History

// @flow
2019-08-28 04:35:07 +02:00
import { ipcRenderer } from 'electron';
2019-08-20 14:29:59 +02:00
2019-10-15 23:23:51 +02:00
let sessionPassword;
function setCookie(name: string, value: string, days: number) {
let expires = '';
if (days) {
let date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = '; expires=' + date.toUTCString();
}
document.cookie = `${name}=${value || ''}${expires}; path=/;samesite=strict;`;
}
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) {
document.cookie = name + '=; Max-Age=-99999999; path=/;';
}
export const setSavedPassword = (value?: string, saveToDisk: boolean) => {
return new Promise<*>(resolve => {
2019-10-22 19:57:32 +02:00
// @if TARGET='app'
ipcRenderer.once('set-password-response', (event, success) => {
resolve(success);
});
2019-10-22 19:57:32 +02:00
// @endif
2019-10-15 23:23:51 +02:00
const password = value === undefined || value === null ? '' : value;
sessionPassword = password;
if (saveToDisk) {
if (password) {
2019-10-22 19:57:32 +02:00
// @if TARGET='app'
ipcRenderer.send('set-password', password);
2019-10-22 19:57:32 +02:00
// @endif
// @if TARGET='web'
setCookie('saved-password', password, 14);
// @endif
} else {
deleteSavedPassword();
}
}
});
2019-08-20 14:29:59 +02:00
};
2019-08-28 04:35:07 +02:00
export const getSavedPassword = () => {
return new Promise<*>(resolve => {
2019-10-15 23:23:51 +02:00
if (sessionPassword) {
resolve(sessionPassword);
}
2019-10-15 06:20:12 +02:00
// @if TARGET='app'
ipcRenderer.once('get-password-response', (event, password) => {
resolve(password);
});
ipcRenderer.send('get-password');
2019-10-15 06:20:12 +02:00
// @endif
// @if TARGET='web'
2019-10-22 19:57:32 +02:00
const password = getCookie('saved-password');
resolve(password);
2019-10-15 06:20:12 +02:00
// @endif
});
};
export const deleteSavedPassword = () => {
return new Promise<*>(resolve => {
// @if TARGET='app'
ipcRenderer.once('delete-password-response', (event, success) => {
resolve();
});
ipcRenderer.send('delete-password');
// @endif;
2019-10-22 19:57:32 +02:00
// @if TARGET='web'
deleteCookie('saved-password');
resolve();
// @endif
});
2019-08-26 22:18:30 +02:00
};
2019-08-20 14:29:59 +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 = () => {
return new Promise<*>(resolve => {
deleteCookie('auth_token');
// @if TARGET='app'
ipcRenderer.once('delete-auth-token-response', (event, success) => {
2019-09-26 18:07:11 +02:00
resolve();
});
ipcRenderer.send('delete-auth-token');
// @endif;
// @if TARGET='web'
resolve();
// @endif
});
2019-08-28 04:35:07 +02:00
};
export const doSignOutCleanup = () => {
return new Promise<*>(resolve => {
deleteCookie('auth_token');
deleteCookie('saved-password');
// @if TARGET='app'
ipcRenderer.once('delete-auth-token-response', (event, success) => {
resolve();
});
ipcRenderer.send('delete-auth-token');
// @endif;
// @if TARGET='web'
resolve();
// @endif
});
};
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
};