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

155 lines
3.6 KiB
JavaScript
Raw Normal View History

const { DOMAIN } = require('../../config.js');
const AUTH_TOKEN = 'auth_token';
const SAVED_PASSWORD = 'saved_password';
const DEPRECATED_SAVED_PASSWORD = 'saved-password';
const domain =
typeof window === 'object' && window.location.hostname.includes('localhost') ? window.location.hostname : DOMAIN;
const isProduction = process.env.NODE_ENV === 'production';
const maxExpiration = 2147483647;
2019-10-15 23:23:51 +02:00
let sessionPassword;
2020-03-17 19:58:34 +01:00
function setCookie(name, value, expirationDaysOnWeb) {
let expires = '';
2020-01-16 23:03:41 +01:00
if (expirationDaysOnWeb) {
let date = new Date();
2020-01-16 23:03:41 +01:00
date.setTime(date.getTime() + expirationDaysOnWeb * 24 * 60 * 60 * 1000);
// If on PC, set to not expire (max)
expires = `expires=${IS_WEB ? date.toUTCString() : maxExpiration};`;
}
let cookie = `${name}=${value || ''}; ${expires} path=/;`;
if (isProduction) {
cookie += ` SameSite=None;`;
}
if (!isProduction) {
cookie += ` SameSite=Lax;`;
}
if (isProduction) {
cookie += ` domain=${domain}; Secure;`;
}
document.cookie = cookie;
}
2020-03-17 19:58:34 +01:00
function getCookie(name) {
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;
}
2020-03-17 19:58:34 +01:00
function deleteCookie(name) {
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 removed the "." in front of the domain
// Remove this if you see it after March 11th, 2021
// https://github.com/lbryio/lbry-desktop/pull/3830
document.cookie = name + `=; Max-Age=-99999999; domain=.${domain}; path=/;`;
}
2020-03-17 19:58:34 +01:00
function setSavedPassword(value, saveToDisk) {
return new Promise(resolve => {
const password = value === undefined || value === null ? '' : value;
sessionPassword = password;
if (saveToDisk) {
if (password) {
setCookie(SAVED_PASSWORD, password, 14);
} else {
deleteSavedPassword();
}
}
});
2020-03-17 19:58:34 +01:00
}
2019-08-20 14:29:59 +02:00
2020-03-17 19:58:34 +01:00
function getSavedPassword() {
return new Promise(resolve => {
2019-10-15 23:23:51 +02:00
if (sessionPassword) {
resolve(sessionPassword);
}
return getPasswordFromCookie().then(p => resolve(p));
});
2020-03-17 19:58:34 +01:00
}
2019-10-15 23:23:51 +02:00
2020-03-17 19:58:34 +01:00
function getPasswordFromCookie() {
return new Promise(resolve => {
2020-01-16 23:03:41 +01:00
let password;
password = getCookie(SAVED_PASSWORD);
2020-01-16 23:03:41 +01:00
resolve(password);
});
2020-03-17 19:58:34 +01:00
}
2020-03-17 19:58:34 +01:00
function deleteSavedPassword() {
return new Promise(resolve => {
deleteCookie(SAVED_PASSWORD);
2019-10-22 19:57:32 +02:00
resolve();
});
2020-03-17 19:58:34 +01:00
}
2019-08-20 14:29:59 +02:00
2020-03-17 19:58:34 +01:00
function getAuthToken() {
return getCookie(AUTH_TOKEN);
2020-03-17 19:58:34 +01:00
}
2020-03-17 19:58:34 +01:00
function setAuthToken(value) {
return setCookie(AUTH_TOKEN, value, 365);
2020-03-17 19:58:34 +01:00
}
2020-03-17 19:58:34 +01:00
function deleteAuthToken() {
return new Promise(resolve => {
deleteCookie(AUTH_TOKEN);
resolve();
});
2020-03-17 19:58:34 +01:00
}
2019-08-28 04:35:07 +02:00
2020-03-17 19:58:34 +01:00
function doSignOutCleanup() {
return new Promise(resolve => {
deleteAuthToken();
deleteSavedPassword();
resolve();
});
2020-03-17 19:58:34 +01:00
}
2020-03-17 19:58:34 +01:00
function doAuthTokenRefresh() {
const authToken = getAuthToken();
if (authToken) {
deleteAuthToken();
setAuthToken(authToken);
}
2020-03-17 19:58:34 +01:00
}
function doDeprecatedPasswordMigrationMarch2020() {
const savedPassword = getCookie(DEPRECATED_SAVED_PASSWORD);
if (savedPassword) {
deleteCookie(DEPRECATED_SAVED_PASSWORD);
setSavedPassword(savedPassword, true);
}
}
2020-03-17 19:58:34 +01:00
module.exports = {
setCookie,
getCookie,
deleteCookie,
setSavedPassword,
getSavedPassword,
getPasswordFromCookie,
deleteSavedPassword,
getAuthToken,
setAuthToken,
deleteAuthToken,
doSignOutCleanup,
doAuthTokenRefresh,
doDeprecatedPasswordMigrationMarch2020,
2019-08-27 21:11:56 +02:00
};