|
| 1 | +const { |
| 2 | + AppIDConstants, |
| 3 | + LocalStorageConstants, |
| 4 | + LocalStorageUtils, |
| 5 | + URLConstants, |
| 6 | + WebSocketUtils, |
| 7 | +} = require('@deriv-com/utils'); |
| 8 | +const Analytics = require('./analytics'); |
| 9 | + |
| 10 | +export const DEFAULT_OAUTH_LOGOUT_URL = 'https://oauth.deriv.com/oauth2/sessions/logout'; |
| 11 | + |
| 12 | +export const DEFAULT_OAUTH_ORIGIN_URL = 'https://oauth.deriv.com'; |
| 13 | + |
| 14 | +const LOGOUT_HANDLER_TIMEOUT = 10000; |
| 15 | + |
| 16 | +const SocketURL = { |
| 17 | + [URLConstants.derivP2pProduction]: 'blue.derivws.com', |
| 18 | + [URLConstants.derivP2pStaging] : 'red.derivws.com', |
| 19 | +}; |
| 20 | + |
| 21 | +export const getServerInfo = () => { |
| 22 | + const origin = window.location.origin; |
| 23 | + const hostname = window.location.hostname; |
| 24 | + |
| 25 | + const existingAppId = LocalStorageUtils.getValue(LocalStorageConstants.configAppId); |
| 26 | + const existingServerUrl = LocalStorageUtils.getValue(LocalStorageConstants.configServerURL); |
| 27 | + // since we don't have official app_id for staging, |
| 28 | + // we will use the red server with app_id=62019 for the staging-p2p.deriv.com for now |
| 29 | + // to fix the login issue |
| 30 | + if (origin === URLConstants.derivP2pStaging && (!existingAppId || !existingServerUrl)) { |
| 31 | + LocalStorageUtils.setValue(LocalStorageConstants.configServerURL, SocketURL[origin]); |
| 32 | + LocalStorageUtils.setValue(LocalStorageConstants.configAppId, `${AppIDConstants.domainAppId[hostname]}`); |
| 33 | + } |
| 34 | + |
| 35 | + const serverUrl = LocalStorageUtils.getValue(LocalStorageConstants.configServerURL) || localStorage.getItem('config.server_url') || 'oauth.deriv.com'; |
| 36 | + |
| 37 | + const defaultAppId = WebSocketUtils.getAppId(); |
| 38 | + const appId = LocalStorageUtils.getValue(LocalStorageConstants.configAppId) || defaultAppId; |
| 39 | + const lang = LocalStorageUtils.getValue(LocalStorageConstants.i18nLanguage) || 'en'; |
| 40 | + |
| 41 | + return { |
| 42 | + appId, |
| 43 | + lang, |
| 44 | + serverUrl, |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +export const getOAuthLogoutUrl = () => { |
| 49 | + const { appId, serverUrl } = getServerInfo(); |
| 50 | + |
| 51 | + const oauthUrl = appId && serverUrl ? `https://${serverUrl}/oauth2/sessions/logout` : DEFAULT_OAUTH_LOGOUT_URL; |
| 52 | + |
| 53 | + return oauthUrl; |
| 54 | +}; |
| 55 | + |
| 56 | +export const getOAuthOrigin = () => { |
| 57 | + const { appId, serverUrl } = getServerInfo(); |
| 58 | + |
| 59 | + const oauthUrl = appId && serverUrl ? `https://${serverUrl}` : DEFAULT_OAUTH_ORIGIN_URL; |
| 60 | + |
| 61 | + return oauthUrl; |
| 62 | +}; |
| 63 | + |
| 64 | +export const isOAuth2Enabled = () => { |
| 65 | + const [OAuth2EnabledApps, OAuth2EnabledAppsInitialised] = Analytics.getGrowthbookFeatureValue({ |
| 66 | + featureFlag: 'hydra_be', |
| 67 | + }); |
| 68 | + const appId = WebSocketUtils.getAppId(); |
| 69 | + |
| 70 | + if (OAuth2EnabledAppsInitialised) { |
| 71 | + const FEHydraAppIds = OAuth2EnabledApps?.length |
| 72 | + ? OAuth2EnabledApps[OAuth2EnabledApps.length - 1]?.enabled_for ?? [] |
| 73 | + : []; |
| 74 | + return FEHydraAppIds.includes(+appId); |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | +}; |
| 79 | + |
| 80 | +export const getLogoutHandler = onWSLogoutAndRedirect => { |
| 81 | + const isAuthEnabled = isOAuth2Enabled(); |
| 82 | + |
| 83 | + if (!isAuthEnabled) { |
| 84 | + return onWSLogoutAndRedirect; |
| 85 | + } |
| 86 | + |
| 87 | + const onMessage = async event => { |
| 88 | + const allowedOrigin = getOAuthOrigin(); |
| 89 | + if (allowedOrigin === event.origin) { |
| 90 | + if (event.data === 'logout_complete') { |
| 91 | + try { |
| 92 | + await onWSLogoutAndRedirect(); |
| 93 | + } catch (err) { |
| 94 | + // eslint-disable-next-line no-console |
| 95 | + console.error(`logout was completed successfully on oauth hydra server, but logout handler returned error: ${err}`); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + window.addEventListener('message', onMessage); |
| 102 | + |
| 103 | + const oAuth2Logout = () => { |
| 104 | + if (!isAuthEnabled) { |
| 105 | + onWSLogoutAndRedirect(); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + let iframe = document.getElementById('logout-iframe'); |
| 110 | + if (!iframe) { |
| 111 | + iframe = document.createElement('iframe'); |
| 112 | + iframe.id = 'logout-iframe'; |
| 113 | + iframe.style.display = 'none'; |
| 114 | + document.body.appendChild(iframe); |
| 115 | + |
| 116 | + setTimeout(() => { |
| 117 | + onWSLogoutAndRedirect(); |
| 118 | + }, LOGOUT_HANDLER_TIMEOUT); |
| 119 | + } |
| 120 | + |
| 121 | + iframe.src = getOAuthLogoutUrl(); |
| 122 | + }; |
| 123 | + |
| 124 | + return oAuth2Logout; |
| 125 | +}; |
0 commit comments