/** * =============================================================== * Guest Website Authentication Provider * =============================================================== * * This portlet is the single source of truth for authentication. * * The rest of the frontend MUST NEVER know: * - how OAuth works * - where the token comes from * - how tokens are refreshed * * The Client Extension simply calls: * * await __Api.getAuthHeader(); * * and this provider is responsible for ensuring a valid token * is always available. */ const MAX_RETRIES = 3; globalThis.__Api = globalThis.__Api || ; globalThis.__Api._authentication = ; globalThis.__Api.authenticationPromise = new Promise((resolve, reject) => globalThis.__Api._authentication.resolve = resolve; globalThis.__Api._authentication.reject = reject; ); globalThis.__Api._tokenCache = globalThis.__Api._tokenCache || ; const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); async function fetchWithRetry(url, options = , maxRetries = MAX_RETRIES) let lastError; for (let attempt = 1; attempt <= maxRetries; attempt++) try const response = await fetch(url, options); if (!response.ok) throw new Error(`HTTP `); return response; catch (error) lastError = error; console.warn( `Authentication request failed (/)`, error, ); if (attempt < maxRetries) await sleep(1000 * 2 ** (attempt - 1)); throw lastError; async function fetchJson(url) const response = await fetchWithRetry(url, method: POST, ); return response.json(); /** * Stores an access token in the shared in-memory cache. */ function cacheToken(data) Object.assign(globalThis.__Api._tokenCache, token: data.access_token, token_type: data.token_type || Bearer, expires_at: Date.now() + Number(data.expires_in) * 1000, ); /** * Preferred authentication flow. * * Browser * ↓ * Portlet * ↓ * OAuth2 * ↓ * Access Token */ async function fetchBackendAccessToken() const tokenData = await fetchJson(https://www.sbilife.co.in/about-us/careers/join-as-employeep_p_id=sbil_corp_auth2_keys_SbilCorpAuth2KeysPortlet_INSTANCE_omls&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_resource_id=getAccessToken&p_p_cacheability=cacheLevelPage&_sbil_corp_auth2_keys_SbilCorpAuth2KeysPortlet_INSTANCE_omls_cmd=getAccessToken); cacheToken(tokenData); /** * Refreshes the access token. * * The Client Extension should ONLY call this function. * It should never know how the token is obtained. */ globalThis.__Api.refreshAuthToken = async function () try await fetchBackendAccessToken(); return; catch (backendError) console.warn( Backend token retrieval failed. Falling back to legacy OAuth flow., backendError, ); ; /** * Initial authentication. * * This runs once during page load so that the first API * request already has a valid access token available. */ (async () => try await globalThis.__Api.refreshAuthToken(); globalThis.__Api._authentication.resolve(); catch (error) console.error(Unable to initialize authentication., error); globalThis.__Api._authentication.reject(error); )();
S
Join as Employee at SBI Life
Job Description
More Info
Key Skills
Promise
