SDA-3900 Cookies alignment with PRD

This commit is contained in:
sbenmoussati 2023-02-15 15:35:18 +01:00 committed by Salah Benmoussati
parent ef33720b9b
commit 491239e2d9

View File

@ -1,8 +1,9 @@
import { session, WebContents } from 'electron'; import { CookiesSetDetails, session, WebContents } from 'electron';
import { apiName } from '../common/api-interface'; import { apiName } from '../common/api-interface';
import { isMac } from '../common/env'; import { isMac } from '../common/env';
import { logger } from '../common/logger'; import { logger } from '../common/logger';
import { getCommandLineArgs } from '../common/utils'; import { getCommandLineArgs } from '../common/utils';
import { whitelistHandler } from '../common/whitelist-handler';
import { config } from './config-handler'; import { config } from './config-handler';
import { activate } from './window-actions'; import { activate } from './window-actions';
import { windowHandler } from './window-handler'; import { windowHandler } from './window-handler';
@ -112,29 +113,47 @@ class ProtocolHandler {
* Sets session cookies and navigates to the pod url * Sets session cookies and navigates to the pod url
*/ */
public async handleSeamlessLogin(protocolUri: string): Promise<void> { public async handleSeamlessLogin(protocolUri: string): Promise<void> {
const { url } = config.getUserConfigFields(['url']); const globalConfig = config.getGlobalConfigFields(['url']);
const userConfig = config.getUserConfigFields(['url']);
const url = userConfig.url ? userConfig.url : globalConfig.url;
const { subdomain, tld, domain } = whitelistHandler.parseDomain(url);
const cookieDomain = `.${subdomain}.${domain}${tld}`;
if (protocolUri) { if (protocolUri) {
const urlParams = new URLSearchParams(new URL(protocolUri).search); const urlParams = new URLSearchParams(new URL(protocolUri).search);
const skeyValue = urlParams.get('skey'); const skeyValue = urlParams.get('skey');
const anticsrfValue = urlParams.get('anticsrf'); const anticsrfValue = urlParams.get('anticsrf');
if (skeyValue) { if (skeyValue && anticsrfValue) {
await session.defaultSession.cookies.set({ const skeyCookie: CookiesSetDetails = {
url, url,
name: 'skey', name: 'skey',
value: skeyValue, value: skeyValue,
}); secure: true,
} httpOnly: true,
if (anticsrfValue) { sameSite: 'no_restriction',
await session.defaultSession.cookies.set({ domain: cookieDomain,
};
const csrfCookie: CookiesSetDetails = {
url, url,
name: 'anti-csrf-cookie', name: 'anti-csrf-cookie',
value: anticsrfValue, value: anticsrfValue,
}); secure: true,
sameSite: 'no_restriction',
domain: cookieDomain,
};
try {
await session.defaultSession.cookies.set(skeyCookie);
await session.defaultSession.cookies.set(csrfCookie);
logger.info('protocol-handler: cookies has been set');
} catch (error) {
logger.error(
'protocol-handler: error occurred with cookies. Details: ',
error,
);
}
} }
logger.info('protocol-handler: cookies has been set');
const mainWebContents = windowHandler.getMainWebContents(); const mainWebContents = windowHandler.getMainWebContents();
if (mainWebContents && !mainWebContents?.isDestroyed() && url) { if (mainWebContents && !mainWebContents?.isDestroyed() && url) {
logger.info('protocol-handler: redirecting main webContents', url); logger.info('protocol-handler: redirecting main webContents ', url);
windowHandler.setMainWindowOrigin(url); windowHandler.setMainWindowOrigin(url);
mainWebContents?.loadURL(url); mainWebContents?.loadURL(url);
} }