Skip to content

Commit 43eafe3

Browse files
committed
@W-22137898 Remove sliding window TTL extension in favor of introspect
The token introspection already returns the real expiration from the server, making the local sliding window redundant. Simplify handleProxyResponse to only detect 401s.
1 parent b92dc8f commit 43eafe3

2 files changed

Lines changed: 12 additions & 74 deletions

File tree

scripts/portal_generator/assets/portal.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ async function executeXOriginSource(sourceIdx, buttonEl) {
534534
});
535535

536536
var data = await resp.json();
537-
handleProxyResponse(data, fullUrl);
537+
handleProxyResponse(data);
538538

539539
// Restore button
540540
if (buttonEl) {
@@ -1967,15 +1967,6 @@ var _ttlExpirationTimerId = null;
19671967
var _TTL_CHECK_INTERVAL = 30000;
19681968
var _SESSION_TTL = 3600000;
19691969

1970-
function isAccountsUrl(url) {
1971-
try {
1972-
var path = new URL(url).pathname;
1973-
return path.startsWith('/accounts/') || path === '/accounts';
1974-
} catch (e) {
1975-
return url.indexOf('/accounts/') !== -1 || url.indexOf('/accounts') === url.length - 9;
1976-
}
1977-
}
1978-
19791970
async function introspectToken() {
19801971
var token = sessionStorage.getItem('anypoint_token');
19811972
if (!token) return null;
@@ -2025,12 +2016,6 @@ function scheduleExpirationCheck(expMs) {
20252016
}, delay);
20262017
}
20272018

2028-
function extendTokenExpiration() {
2029-
var token = sessionStorage.getItem('anypoint_token');
2030-
if (!token) return;
2031-
setTokenExpiration(Date.now() + _SESSION_TTL);
2032-
}
2033-
20342019
function markTokenExpired() {
20352020
sessionStorage.setItem('anypoint_token_expires_at', '0');
20362021
stopTtlTimer();
@@ -2070,13 +2055,9 @@ async function checkTtlExpiration() {
20702055
}
20712056
}
20722057

2073-
function handleProxyResponse(data, requestUrl) {
2058+
function handleProxyResponse(data) {
20742059
if (data.status === 401) {
20752060
markTokenExpired();
2076-
return;
2077-
}
2078-
if (data.status >= 200 && data.status < 300 && requestUrl && !isAccountsUrl(requestUrl)) {
2079-
extendTokenExpiration();
20802061
}
20812062
}
20822063

@@ -2308,7 +2289,7 @@ async function loadXOriginValues(opId, paramName) {
23082289
});
23092290

23102291
var data = await resp.json();
2311-
handleProxyResponse(data, fullUrl);
2292+
handleProxyResponse(data);
23122293

23132294
if (btn) {
23142295
btn.disabled = false;
@@ -2509,7 +2490,7 @@ async function loadXOriginValuesForEnv(paramName) {
25092490
});
25102491

25112492
var data = await resp.json();
2512-
handleProxyResponse(data, fullUrl);
2493+
handleProxyResponse(data);
25132494

25142495
if (btn) {
25152496
btn.disabled = false;
@@ -3688,7 +3669,7 @@ async function sendRequest(opId, buttonEl) {
36883669
})
36893670
});
36903671
var data = await resp.json();
3691-
handleProxyResponse(data, fullUrl);
3672+
handleProxyResponse(data);
36923673

36933674
// Restore button
36943675
if (buttonEl) {
@@ -5631,7 +5612,7 @@ async function executePlaygroundStep(sid, buttonEl) {
56315612
});
56325613

56335614
var result = await resp.json();
5634-
handleProxyResponse(result, fullUrl);
5615+
handleProxyResponse(result);
56355616

56365617
// Restore button
56375618
if (buttonEl) {
@@ -6540,7 +6521,7 @@ async function runWorkflowStep(skillSlug, stepIndex) {
65406521
})
65416522
});
65426523
var data = await resp.json();
6543-
handleProxyResponse(data, fullUrl);
6524+
handleProxyResponse(data);
65446525

65456526
if (spinner) spinner.style.display = 'none';
65466527
if (rightPanel) rightPanel.setAttribute('open', '');

scripts/tests/portal.test.js

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -885,24 +885,6 @@ describe('_closeAllSkillDropdowns', () => {
885885
// Session TTL Management
886886
// ===========================================================================
887887

888-
describe('isAccountsUrl', () => {
889-
test('returns true for /accounts/ paths', () => {
890-
expect(isAccountsUrl('https://anypoint.mulesoft.com/accounts/login')).toBe(true);
891-
expect(isAccountsUrl('https://anypoint.mulesoft.com/accounts/api/me')).toBe(true);
892-
expect(isAccountsUrl('https://anypoint.mulesoft.com/accounts/oauth2/introspect')).toBe(true);
893-
});
894-
895-
test('returns false for non-accounts paths', () => {
896-
expect(isAccountsUrl('https://anypoint.mulesoft.com/apimanager/api/v1/organizations/123/apis')).toBe(false);
897-
expect(isAccountsUrl('https://anypoint.mulesoft.com/exchange/api/v2/assets')).toBe(false);
898-
});
899-
900-
test('handles malformed URLs gracefully', () => {
901-
expect(isAccountsUrl('not-a-url-with-/accounts/')).toBe(true);
902-
expect(isAccountsUrl('not-a-url')).toBe(false);
903-
});
904-
});
905-
906888
describe('setTokenExpiration', () => {
907889
beforeEach(() => sessionStorage.clear());
908890

@@ -912,24 +894,6 @@ describe('setTokenExpiration', () => {
912894
});
913895
});
914896

915-
describe('extendTokenExpiration', () => {
916-
beforeEach(() => sessionStorage.clear());
917-
918-
test('does nothing when no token exists', () => {
919-
extendTokenExpiration();
920-
expect(sessionStorage.getItem('anypoint_token_expires_at')).toBeNull();
921-
});
922-
923-
test('sets expiration to ~1h from now when token exists', () => {
924-
sessionStorage.setItem('anypoint_token', 'test-token');
925-
var before = Date.now();
926-
extendTokenExpiration();
927-
var stored = parseInt(sessionStorage.getItem('anypoint_token_expires_at'), 10);
928-
expect(stored).toBeGreaterThanOrEqual(before + 3600000 - 100);
929-
expect(stored).toBeLessThanOrEqual(Date.now() + 3600000 + 100);
930-
});
931-
});
932-
933897
describe('markTokenExpired', () => {
934898
beforeEach(() => {
935899
sessionStorage.clear();
@@ -975,26 +939,19 @@ describe('handleProxyResponse', () => {
975939
});
976940

977941
test('marks token expired on 401', () => {
978-
handleProxyResponse({ status: 401 }, 'https://anypoint.mulesoft.com/apimanager/api/v1/orgs');
942+
handleProxyResponse({ status: 401 });
979943
expect(sessionStorage.getItem('anypoint_token_expires_at')).toBe('0');
980944
});
981945

982-
test('extends TTL on 2xx non-accounts response', () => {
983-
var before = Date.now();
984-
handleProxyResponse({ status: 200 }, 'https://anypoint.mulesoft.com/apimanager/api/v1/orgs');
985-
var stored = parseInt(sessionStorage.getItem('anypoint_token_expires_at'), 10);
986-
expect(stored).toBeGreaterThanOrEqual(before + 3600000 - 100);
987-
});
988-
989-
test('does NOT extend TTL on 2xx accounts response', () => {
946+
test('does nothing on non-401 responses', () => {
990947
var original = sessionStorage.getItem('anypoint_token_expires_at');
991-
handleProxyResponse({ status: 200 }, 'https://anypoint.mulesoft.com/accounts/api/me');
948+
handleProxyResponse({ status: 200 });
992949
expect(sessionStorage.getItem('anypoint_token_expires_at')).toBe(original);
993950
});
994951

995-
test('does nothing on non-401 error responses', () => {
952+
test('does nothing on server error responses', () => {
996953
var original = sessionStorage.getItem('anypoint_token_expires_at');
997-
handleProxyResponse({ status: 500 }, 'https://anypoint.mulesoft.com/apimanager/api/v1/orgs');
954+
handleProxyResponse({ status: 500 });
998955
expect(sessionStorage.getItem('anypoint_token_expires_at')).toBe(original);
999956
});
1000957
});

0 commit comments

Comments
 (0)