Skip to content

Commit

Permalink
Merge pull request #882 from deriv-com/revert-871-fix-cookie-domain
Browse files Browse the repository at this point in the history
Revert "Fix cookie domain check for Silent Login"
  • Loading branch information
matin-deriv authored Dec 10, 2024
2 parents 3217727 + 75f09cc commit 2720353
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"@binary-com/binary-style": "^0.2.26",
"@binary-com/webtrader-charts": "^0.6.2",
"@deriv-com/analytics": "^1.26.1",
"@deriv-com/auth-client": "1.3.3",
"@deriv-com/auth-client": "1.3.1",
"@deriv-com/quill-ui": "^1.16.2",
"@deriv-com/utils": "^0.0.38",
"@deriv/deriv-api": "^1.0.15",
Expand Down
60 changes: 58 additions & 2 deletions src/javascript/_common/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const {
} = require('@deriv-com/utils');
const Cookies = require('js-cookie');
const requestOidcAuthentication = require('@deriv-com/auth-client').requestOidcAuthentication;
const handlePostLogout = require('@deriv-com/analytics').handlePostLogout;
const Analytics = require('./analytics');

export const DEFAULT_OAUTH_LOGOUT_URL = 'https://oauth.deriv.com/oauth2/sessions/logout';

export const DEFAULT_OAUTH_ORIGIN_URL = 'https://oauth.deriv.com';

const LOGOUT_HANDLER_TIMEOUT = 10000;

const SocketURL = {
[URLConstants.derivP2pProduction]: 'blue.derivws.com',
[URLConstants.derivP2pStaging] : 'red.derivws.com',
Expand Down Expand Up @@ -79,7 +80,62 @@ export const isOAuth2Enabled = () => {
};

export const getLogoutHandler = onWSLogoutAndRedirect => {
const oAuth2Logout = handlePostLogout(onWSLogoutAndRedirect);
const isAuthEnabled = isOAuth2Enabled();
let timeout;

if (!isAuthEnabled) {
return onWSLogoutAndRedirect;
}

const cleanup = () => {
clearTimeout(timeout);

const iframe = document.getElementById('logout-iframe');
if (iframe) iframe.remove();
};

const onMessage = event => {
if (event.data === 'logout_complete') {
const domains = ['deriv.com', 'binary.sx', 'pages.dev', 'localhost'];
const currentDomain = window.location.hostname.split('.').slice(-2).join('.');
if (domains.includes(currentDomain)) {
Cookies.set('logged_state', 'false', {
expires: 30,
path : '/',
secure : true,
});
}
onWSLogoutAndRedirect();
window.removeEventListener('message', onMessage);
cleanup();
}
};

window.addEventListener('message', onMessage);

const oAuth2Logout = () => {
if (!isAuthEnabled) {
onWSLogoutAndRedirect();
return;
}

let iframe = document.getElementById('logout-iframe');
if (!iframe) {
iframe = document.createElement('iframe');
iframe.id = 'logout-iframe';
iframe.style.display = 'none';
document.body.appendChild(iframe);

timeout = setTimeout(() => {
onWSLogoutAndRedirect();
window.removeEventListener('message', onMessage);
cleanup();
}, LOGOUT_HANDLER_TIMEOUT);
}

iframe.src = getOAuthLogoutUrl();
};

return oAuth2Logout;
};

Expand Down
10 changes: 10 additions & 0 deletions src/javascript/app/pages/callback/callback.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ const CallbackContainer = () => {
}
getElementById('loading_link').setAttribute('href', redirect_url);

const domains = ['deriv.com', 'binary.sx', 'pages.dev', 'localhost'];
const currentDomain = window.location.hostname.split('.').slice(-2).join('.');
if (domains.includes(currentDomain)) {
Cookies.set('logged_state', 'true', {
expires: 30,
path : '/',
secure : true,
});
}

window.location.href = redirect_url; // need to redirect not using pjax
});
};
Expand Down

0 comments on commit 2720353

Please sign in to comment.