-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de69d69
commit be1724f
Showing
10 changed files
with
316 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,118 @@ | ||
const Utils = require('@deriv-com/utils'); | ||
const { | ||
AppIDConstants, | ||
LocalStorageConstants, | ||
LocalStorageUtils, | ||
URLConstants, | ||
WebSocketUtils, | ||
} = require('@deriv-com/utils'); | ||
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 SocketURL = { | ||
[URLConstants.derivP2pProduction]: 'blue.derivws.com', | ||
[URLConstants.derivP2pStaging] : 'red.derivws.com', | ||
}; | ||
|
||
export const getServerInfo = () => { | ||
const origin = window.location.origin; | ||
const hostname = window.location.hostname; | ||
|
||
const existingAppId = LocalStorageUtils.getValue(LocalStorageConstants.configAppId); | ||
const existingServerUrl = LocalStorageUtils.getValue(LocalStorageConstants.configServerURL); | ||
// since we don't have official app_id for staging, | ||
// we will use the red server with app_id=62019 for the staging-p2p.deriv.com for now | ||
// to fix the login issue | ||
if (origin === URLConstants.derivP2pStaging && (!existingAppId || !existingServerUrl)) { | ||
LocalStorageUtils.setValue(LocalStorageConstants.configServerURL, SocketURL[origin]); | ||
LocalStorageUtils.setValue(LocalStorageConstants.configAppId, `${AppIDConstants.domainAppId[hostname]}`); | ||
} | ||
|
||
// const storedServerUrl = LocalStorageUtils.getValue(LocalStorageConstants.configServerURL); | ||
const serverUrl = 'qa101.deriv.dev'; | ||
|
||
const appId = LocalStorageUtils.getValue(LocalStorageConstants.configAppId); | ||
const lang = LocalStorageUtils.getValue(LocalStorageConstants.i18nLanguage); | ||
|
||
return { | ||
appId, | ||
lang, | ||
serverUrl, | ||
}; | ||
}; | ||
|
||
export const getOAuthLogoutUrl = () => { | ||
const { appId, serverUrl } = Utils.WebSocketUtils.getServerInfo(); | ||
const { appId, serverUrl } = getServerInfo(); | ||
|
||
const oauthUrl = appId && serverUrl ? `https://${serverUrl}/oauth2/sessions/logout` : DEFAULT_OAUTH_LOGOUT_URL; | ||
|
||
return oauthUrl; | ||
}; | ||
|
||
export const getOAuthOrigin = () => { | ||
const { appId, serverUrl } = Utils.WebSocketUtils.getServerInfo(); | ||
const { appId, serverUrl } = getServerInfo(); | ||
|
||
const oauthUrl = appId && serverUrl ? `https://${serverUrl}` : DEFAULT_OAUTH_ORIGIN_URL; | ||
|
||
return oauthUrl; | ||
}; | ||
|
||
export const AuthClient = (() => { | ||
const isOAuth2Enabled = oAuth2GrowthbookConfig => { | ||
const { OAuth2EnabledApps, OAuth2EnabledAppsInitialised } = oAuth2GrowthbookConfig; | ||
const appId = Utils.WebSocketUtils.getAppId(); | ||
export const isOAuth2Enabled = () => { | ||
const [OAuth2EnabledApps, OAuth2EnabledAppsInitialised] = Analytics.getGrowthbookFeatureValue({ | ||
featureFlag: 'hydra_be', | ||
}); | ||
const appId = WebSocketUtils.getAppId(); | ||
|
||
if (OAuth2EnabledAppsInitialised) { | ||
const FEHydraAppIds = OAuth2EnabledApps?.length | ||
? OAuth2EnabledApps[OAuth2EnabledApps.length - 1]?.enabled_for ?? [] | ||
: []; | ||
return FEHydraAppIds.includes(+appId); | ||
} | ||
if (OAuth2EnabledAppsInitialised) { | ||
const FEHydraAppIds = OAuth2EnabledApps?.length | ||
? OAuth2EnabledApps[OAuth2EnabledApps.length - 1]?.enabled_for ?? [] | ||
: []; | ||
return FEHydraAppIds.includes(+appId); | ||
} | ||
|
||
return false; | ||
}; | ||
const getLogoutHandler = (oAuth2GrowthbookConfig, onWSLogoutAndRedirect) => { | ||
const isAuthEnabled = isOAuth2Enabled(oAuth2GrowthbookConfig); | ||
return false; | ||
}; | ||
|
||
if (!isAuthEnabled) { | ||
console.log('lol auth2 is not enabled'); | ||
return onWSLogoutAndRedirect; | ||
} | ||
export const getLogoutHandler = onWSLogoutAndRedirect => { | ||
const isAuthEnabled = isOAuth2Enabled(); | ||
|
||
if (!isAuthEnabled) { | ||
return onWSLogoutAndRedirect; | ||
} | ||
|
||
const onMessage = async event => { | ||
const allowedOrigin = getOAuthOrigin(); | ||
if (allowedOrigin === event.origin) { | ||
if (event.data === 'logout_complete') { | ||
console.log('logout_complete calledd!'); | ||
onWSLogoutAndRedirect(); | ||
} | ||
const onMessage = async event => { | ||
const allowedOrigin = getOAuthOrigin(); | ||
if (allowedOrigin === event.origin) { | ||
if (event.data === 'logout_complete') { | ||
await onWSLogoutAndRedirect(); | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
window.addEventListener('message', onMessage); | ||
window.addEventListener('message', onMessage); | ||
|
||
const oAuth2Logout = async () => { | ||
if (!isAuthEnabled) { | ||
console.log('lol auth2 is not enabled'); | ||
onWSLogoutAndRedirect(); | ||
return; | ||
} | ||
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); | ||
let iframe = document.getElementById('logout-iframe'); | ||
if (!iframe) { | ||
iframe = document.createElement('iframe'); | ||
iframe.id = 'logout-iframe'; | ||
iframe.style.display = 'none'; | ||
document.body.appendChild(iframe); | ||
|
||
setTimeout(() => { | ||
onWSLogoutAndRedirect(); | ||
}, 10000); | ||
} | ||
console.log('auth2 doneee'); | ||
}; | ||
setTimeout(() => { | ||
onWSLogoutAndRedirect(); | ||
}, 10000); | ||
} | ||
|
||
return oAuth2Logout; | ||
iframe.src = getOAuthLogoutUrl(); | ||
}; | ||
|
||
return { | ||
getLogoutHandler, | ||
isOAuth2Enabled, | ||
}; | ||
})(); | ||
return oAuth2Logout; | ||
}; |
Oops, something went wrong.