-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added OIDC flow to SmartTrader #855
Changes from all commits
a0eec60
8a8d1a1
36536ed
ccfdf95
bb043fc
1b6e6ae
812acb1
665255c
c57129b
68a998d
aed9fbc
b0bbb43
7251a42
71ff52a
53a5366
a0b9293
10add16
28a26f0
f3bd46a
8c96ef4
be2aa24
3116340
12a926d
4107c8b
1f91446
b9074a9
87eb912
50da890
cfe71b6
501cdb7
ca32a53
f9cf0b8
420717d
53035fa
74f78df
26b35d5
de1e28e
bbe9ca9
d76eb41
0f93afe
f7aeed2
30bd381
52f2aef
9ab86b2
33aeb3f
fc8a882
1ad7336
6a9cb64
0043649
84f488e
27141ed
24ce47c
c9953f3
4ad6f8a
962e7b9
7dee45c
e4bc5ed
2584b8c
1f26231
ae77db2
a641869
b405ad0
84710f5
4a14e25
338efba
3654995
901370a
febd81c
c83e354
f7b7bb2
17441a6
fc6494b
fd1d560
9f6124e
940b97e
ffee2e2
227e38a
5d744ae
9649585
2f376b5
8bf5dfa
3a7900d
799fe7f
93cfe40
5fb7037
7980d38
46228db
ad43311
eeffaa5
bb3ed14
6949d0c
b0d5b8b
2bc42aa
d65f341
2f0e547
95694b8
be4bb06
586ef75
fb379d4
2b6dd40
08a9893
a525b54
0b445ef
3b130ee
8c9a2d1
672b89c
c055390
e6be927
5d03ecc
fa28625
2a4d8e3
cf2ba37
ab44a94
d4ade52
e532eae
1ddf226
4c59510
e869d2d
1ce509e
cd06998
160d436
a7c4196
defe378
7865236
29f9de4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ | |
} | ||
] | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"singleQuote": true, | ||
"tabWidth": 4, | ||
"semi": true, | ||
"trailingComma": "es5", | ||
"bracketSpacing": true, | ||
"jsxBracketSameLine": false, | ||
"arrowParens": "avoid", | ||
"printWidth": 100, | ||
"endOfLine": "lf", | ||
"object-property-newline": "always", | ||
"key-spacing": { | ||
"beforeColon": false, | ||
"afterColon": true, | ||
"mode": "strict" | ||
} | ||
} |
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
URLConstants, | ||
WebSocketUtils, | ||
} = require('@deriv-com/utils'); | ||
const Cookies = require('js-cookie'); | ||
const requestOidcAuthentication = require('@deriv-com/auth-client').requestOidcAuthentication; | ||
const Analytics = require('./analytics'); | ||
|
||
export const DEFAULT_OAUTH_LOGOUT_URL = 'https://oauth.deriv.com/oauth2/sessions/logout'; | ||
|
@@ -79,22 +81,33 @@ | |
|
||
export const getLogoutHandler = onWSLogoutAndRedirect => { | ||
const isAuthEnabled = isOAuth2Enabled(); | ||
let timeout; | ||
|
||
if (!isAuthEnabled) { | ||
return onWSLogoutAndRedirect; | ||
} | ||
|
||
const onMessage = async event => { | ||
const allowedOrigin = getOAuthOrigin(); | ||
if (allowedOrigin === event.origin) { | ||
if (event.data === 'logout_complete') { | ||
try { | ||
await onWSLogoutAndRedirect(); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`logout was completed successfully on oauth hydra server, but logout handler returned error: ${err}`); | ||
} | ||
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(); | ||
} | ||
}; | ||
|
||
|
@@ -113,8 +126,10 @@ | |
iframe.style.display = 'none'; | ||
document.body.appendChild(iframe); | ||
|
||
setTimeout(() => { | ||
timeout = setTimeout(() => { | ||
onWSLogoutAndRedirect(); | ||
window.removeEventListener('message', onMessage); | ||
cleanup(); | ||
}, LOGOUT_HANDLER_TIMEOUT); | ||
} | ||
|
||
|
@@ -123,3 +138,49 @@ | |
|
||
return oAuth2Logout; | ||
}; | ||
|
||
export const requestSingleSignOn = async () => { | ||
const _requestSingleSignOn = async () => { | ||
Check warning on line 143 in src/javascript/_common/auth.js GitHub Actions / Build and Test
|
||
// if we have previously logged in, | ||
// this cookie will be set by the Callback page (which is exported from @deriv-com/auth-client library) to true when we have successfully logged in from other apps | ||
const isLoggedInCookie = Cookies.get('logged_state') === 'true'; | ||
const clientAccounts = JSON.parse(localStorage.getItem('client.accounts') || '{}'); | ||
const isClientAccountsPopulated = Object.keys(clientAccounts).length > 0; | ||
const isAuthEnabled = isOAuth2Enabled(); | ||
const isCallbackPage = window.location.pathname.includes('callback'); | ||
const isEndpointPage = window.location.pathname.includes('endpoint'); | ||
|
||
// we only do SSO if: | ||
// we have previously logged-in before from SmartTrader or any other apps (Deriv.app, etc) - isLoggedInCookie | ||
// if we are not in the callback route to prevent re-calling this function - !isCallbackPage | ||
// if client.accounts in localStorage is empty - !isClientAccountsPopulated | ||
// and if feature flag for OIDC Phase 2 is enabled - isAuthEnabled | ||
if (isLoggedInCookie && !isCallbackPage && !isEndpointPage && !isClientAccountsPopulated && isAuthEnabled) { | ||
await requestOidcAuthentication({ | ||
redirectCallbackUri: `${window.location.origin}/en/callback`, | ||
}); | ||
} | ||
}; | ||
|
||
const isGrowthbookLoaded = Analytics.isGrowthbookLoaded(); | ||
if (!isGrowthbookLoaded) { | ||
let retryInterval = 0; | ||
// this interval is to check if Growthbook is already initialised. | ||
// If not, keep checking it (max 10 times) and SSO if conditions are met | ||
const interval = setInterval(() => { | ||
if (retryInterval > 10) { | ||
clearInterval(interval); | ||
} else { | ||
const isLoaded = Analytics.isGrowthbookLoaded(); | ||
if (isLoaded) { | ||
_requestSingleSignOn(); | ||
clearInterval(interval); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This interval is to check if Growthbook has already been loaded. If not, wait for it to be initialised and attempt to SSO if conditions are met |
||
} else { | ||
retryInterval += 1; | ||
} | ||
} | ||
}, 500); | ||
} else { | ||
_requestSingleSignOn(); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const SocketCache = require('../../_common/base/socket_cache'); | ||
const CallbackElement = require('../pages/callback/callback.jsx'); | ||
|
||
const CallbackHandler = (() => { | ||
const onLoad = async () => { | ||
parent.window.is_logging_in = 1; // this flag is used in base.js to prevent auto-reloading this page | ||
CallbackElement.init(); | ||
SocketCache.clear(); | ||
}; | ||
|
||
return { | ||
onLoad, | ||
}; | ||
})(); | ||
|
||
module.exports = CallbackHandler; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added prettier config to enable autofix in Vscode