Skip to content

Commit 503fb46

Browse files
author
Sachin Maheshwari
committed
correcting storing logic for refresh token through iframe
1 parent f0fcda8 commit 503fb46

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/connector-wrapper.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
const {createFrame} = require('./iframe')
2-
const {getToken, isTokenExpired} = require ('./token')
1+
const { createFrame } = require('./iframe')
2+
const { getToken, isTokenExpired } = require('./token')
33

44
let iframe = null
55
let loading = null
66
let url = ''
77
let mock = false
88
let token = ''
99

10-
export function configureConnector({connectorUrl, frameId, mockMode, mockToken}) {
10+
export function configureConnector({ connectorUrl, frameId, mockMode, mockToken }) {
1111
if (mockMode) {
1212
mock = true
1313
token = mockToken
1414
} else if (iframe) {
1515
console.warn('tc-accounts connector can only be configured once, this request has been ignored.')
1616
} else {
1717
iframe = createFrame(frameId, connectorUrl)
18-
url = connectorUrl
19-
20-
loading = new Promise( (resolve) => {
21-
iframe.onload = function() {
18+
url = connectorUrl
19+
20+
loading = new Promise((resolve) => {
21+
iframe.onload = function () {
2222
loading = null
2323
resolve()
2424
}
2525
})
2626
}
2727
}
2828

29-
const proxyCall = function() {
29+
const proxyCall = function () {
3030
if (mock) {
3131
throw new Error('connector is running in mock mode. This method (proxyCall) should not be invoked.')
3232
}
@@ -37,21 +37,20 @@ const proxyCall = function() {
3737

3838
function request() {
3939
const token = getToken('v3jwt')
40+
// 65 is offset in seconds, before expiry
4041
if (token && !isTokenExpired(token, 65)) {
4142
return new Promise((resolve, reject) => {
4243
token ? resolve({ token: token }) : reject("v3jwt cookie not found")
4344
})
4445
} else {
4546
return new Promise((resolve, reject) => {
4647
function receiveMessage(e) {
47-
console.log("Received at auth-lib:", e)
4848
const safeFormat = e.data.type === "SUCCESS" || e.data.type === "FAILURE"
4949
if (safeFormat) {
5050
window.removeEventListener('message', receiveMessage)
5151
if (e.data.type === "SUCCESS") {
5252
token ? resolve({ token: token }) : reject("v3jwt cookie not found")
53-
}
54-
if (e.data.type === "FAILURE") {
53+
} else {
5554
reject("unable to get refesh token")
5655
}
5756
}
@@ -83,7 +82,7 @@ export function getFreshToken() {
8382
}
8483

8584
return proxyCall()
86-
.then( data => data.token )
85+
.then(data => data.token)
8786
}
8887

8988

web-assets/js/setupAuth0WithRedirect.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,13 @@ const authSetup = function () {
425425
if (token && !isTokenExpired(token, 65)) {
426426
informIt(success, e);
427427
} else if (auth0) {
428-
logger("inside auth0 block", "ok");
429428
auth0.isAuthenticated().then(function (isAuthenticated) {
430-
logger("inside auth0 block isAuthenticated", isAuthenticated);
431429
if (isAuthenticated) {
432430
auth0.getTokenSilently().then(function (token) {
433-
logger("inside auth0 block getTokenSilently", token);
434-
storeToken();
431+
storeRefreshedToken();
435432
informIt(success, e);
436433
}).catch(function (err) {
437-
logger("receiveMessage: Error in refreshing through ifram token: ", err)
434+
logger("receiveMessage: Error in refreshing token through iframe:", err)
438435
informIt(failed, e);
439436
});
440437
} else {
@@ -452,10 +449,47 @@ const authSetup = function () {
452449
}
453450
}
454451

452+
/**
453+
* post message to iframe
454+
* @param data payload
455+
* @param e event object
456+
*/
455457
function informIt(data, e) {
456458
e.source.postMessage(data, e.origin);
457459
}
458460

461+
function storeRefreshedToken() {
462+
auth0.getIdTokenClaims().then(function (claims) {
463+
idToken = claims.__raw;
464+
let userActive = false;
465+
Object.keys(claims).findIndex(function (key) {
466+
if (key.includes('active')) {
467+
userActive = claims[key];
468+
return true;
469+
}
470+
return false;
471+
});
472+
if (userActive) {
473+
let tcsso = '';
474+
Object.keys(claims).findIndex(function (key) {
475+
if (key.includes(tcSSOCookie)) {
476+
tcsso = claims[key];
477+
return true;
478+
}
479+
return false;
480+
});
481+
logger('Storing refreshed token...', true);
482+
setCookie(tcJWTCookie, idToken, cookieExpireIn);
483+
setCookie(v3JWTCookie, idToken, cookieExpireIn);
484+
setCookie(tcSSOCookie, tcsso, cookieExpireIn);
485+
} else {
486+
logger("Refeshed token - user active ? ", userActive);
487+
}
488+
}).catch(function (e) {
489+
logger("Refeshed token - error in fetching token from auth0: ", e);
490+
});
491+
};
492+
459493
function changeWindowMessage() {
460494

461495
if ((!returnAppUrl && !appUrl) || ((returnAppUrl == 'undefined') && (appUrl == 'undefined'))) {

0 commit comments

Comments
 (0)