2020-03-17 19:58:34 +01:00
|
|
|
const { DOMAIN } = require('../../config.js');
|
2020-03-12 03:29:53 +01:00
|
|
|
const AUTH_TOKEN = 'auth_token';
|
|
|
|
const SAVED_PASSWORD = 'saved_password';
|
2020-04-13 21:16:07 +02:00
|
|
|
const domain =
|
|
|
|
typeof window === 'object' && window.location.hostname.includes('localhost') ? window.location.hostname : DOMAIN;
|
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-03-17 19:58:34 +01:00
|
|
|
function setCookie(name, value, expirationDaysOnWeb) {
|
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
|
|
|
}
|
|
|
|
|
2020-07-24 21:28:09 +02:00
|
|
|
let cookie = `${name}=${value || ''}; ${expires} path=/;`;
|
|
|
|
if (isProduction) {
|
|
|
|
cookie += ` SameSite=None;`;
|
|
|
|
}
|
|
|
|
if (!isProduction) {
|
|
|
|
cookie += ` SameSite=Lax;`;
|
|
|
|
}
|
2019-11-27 16:18:43 +01:00
|
|
|
if (isProduction) {
|
2020-03-12 03:29:53 +01:00
|
|
|
cookie += ` domain=${domain}; Secure;`;
|
2019-11-27 16:18:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
document.cookie = cookie;
|
2019-10-17 17:27:41 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
function getCookie(name) {
|
2019-10-17 17:27:41 +02:00
|
|
|
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) {
|
2020-03-12 03:29:53 +01:00
|
|
|
document.cookie = name + `=; Max-Age=-99999999; domain=${domain}; path=/;`;
|
2019-11-22 16:32:25 +01:00
|
|
|
|
|
|
|
// Legacy
|
2020-03-12 03:29:53 +01:00
|
|
|
// 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=/;`;
|
2019-10-17 17:27:41 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
function setSavedPassword(value, saveToDisk) {
|
2021-12-20 07:12:05 +01:00
|
|
|
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) {
|
2020-03-12 03:29:53 +01: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
|
|
|
});
|
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() {
|
2021-12-20 07:12:05 +01: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
|
|
|
|
2021-12-20 07:12:05 +01:00
|
|
|
return getPasswordFromCookie().then((p) => resolve(p));
|
2019-12-19 23:35:12 +01:00
|
|
|
});
|
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() {
|
2021-12-20 07:12:05 +01:00
|
|
|
return new Promise((resolve) => {
|
2020-01-16 23:03:41 +01:00
|
|
|
let password;
|
2020-03-12 03:29:53 +01:00
|
|
|
password = getCookie(SAVED_PASSWORD);
|
2020-01-16 23:03:41 +01:00
|
|
|
resolve(password);
|
2019-09-26 18:28:08 +02:00
|
|
|
});
|
2020-03-17 19:58:34 +01:00
|
|
|
}
|
2019-09-26 18:28:08 +02:00
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
function deleteSavedPassword() {
|
2021-12-20 07:12:05 +01:00
|
|
|
return new Promise((resolve) => {
|
2020-03-12 03:29:53 +01:00
|
|
|
deleteCookie(SAVED_PASSWORD);
|
2019-10-22 19:57:32 +02:00
|
|
|
resolve();
|
2019-09-26 18:28:08 +02:00
|
|
|
});
|
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() {
|
2020-03-12 03:29:53 +01:00
|
|
|
return getCookie(AUTH_TOKEN);
|
2020-03-17 19:58:34 +01:00
|
|
|
}
|
2019-10-17 17:27:41 +02:00
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
function setAuthToken(value) {
|
2020-03-12 03:29:53 +01:00
|
|
|
return setCookie(AUTH_TOKEN, value, 365);
|
2020-03-17 19:58:34 +01:00
|
|
|
}
|
2019-10-17 17:27:41 +02:00
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
function deleteAuthToken() {
|
2021-12-20 07:12:05 +01:00
|
|
|
return new Promise((resolve) => {
|
2020-03-12 03:29:53 +01:00
|
|
|
deleteCookie(AUTH_TOKEN);
|
2019-09-26 18:28:08 +02:00
|
|
|
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() {
|
2021-12-20 07:12:05 +01:00
|
|
|
return new Promise((resolve) => {
|
2020-03-12 03:29:53 +01:00
|
|
|
deleteAuthToken();
|
|
|
|
deleteSavedPassword();
|
2019-10-31 16:27:15 +01:00
|
|
|
resolve();
|
|
|
|
});
|
2020-03-17 19:58:34 +01:00
|
|
|
}
|
2019-10-31 16:27:15 +01:00
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
function doAuthTokenRefresh() {
|
2020-03-12 03:29:53 +01:00
|
|
|
const authToken = getAuthToken();
|
|
|
|
if (authToken) {
|
|
|
|
deleteAuthToken();
|
|
|
|
setAuthToken(authToken);
|
|
|
|
}
|
2020-03-17 19:58:34 +01:00
|
|
|
}
|
2020-03-12 03:29:53 +01:00
|
|
|
|
2020-03-17 19:58:34 +01:00
|
|
|
module.exports = {
|
|
|
|
setCookie,
|
|
|
|
getCookie,
|
|
|
|
deleteCookie,
|
|
|
|
setSavedPassword,
|
|
|
|
getSavedPassword,
|
|
|
|
getPasswordFromCookie,
|
|
|
|
deleteSavedPassword,
|
|
|
|
getAuthToken,
|
|
|
|
setAuthToken,
|
|
|
|
deleteAuthToken,
|
|
|
|
doSignOutCleanup,
|
|
|
|
doAuthTokenRefresh,
|
2019-08-27 21:11:56 +02:00
|
|
|
};
|