UNPKG

@contentstack/cli-utilities

Version:

Utilities for contentstack projects

123 lines (122 loc) 4.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("./index"); class AuthenticationHandler { constructor() { this.token = null; this.authType = index_1.configHandler.get('authorisationType'); this.isOAuth = this.authType === 'OAUTH'; } async getAuthDetails() { try { switch (this.authType) { case 'BASIC': this.token = index_1.configHandler.get('authtoken'); break; case 'OAUTH': await index_1.authHandler.compareOAuthExpiry(); this.token = `Bearer ${index_1.configHandler.get('oauthAccessToken')}`; break; default: const authToken = index_1.configHandler.get('authtoken'); if (authToken) { this.token = authToken; } else { index_1.cliux.print('Session timed out, please login to proceed', { color: 'yellow', }); process.exit(1); } break; } } catch (error) { index_1.cliux.print(`Error occurred while fetching auth details: ${error.message}`, { color: 'red', }); process.exit(1); } } get isOauthEnabled() { return this.isOAuth; } get accessToken() { if (!this.token) { throw new Error('Token is not available. Please authenticate first.'); } return this.token; } async refreshAccessToken(error, maxRetryCount = 1) { if (error.response && error.response.status) { switch (error.response.status) { case 401: // NOTE: Refresh the token if the type is OAuth. const region = index_1.configHandler.get('region') || {}; if (region === null || region === void 0 ? void 0 : region.cma) { let hostName = ''; if (region.cma.startsWith('http')) { const u = new URL(region.cma); if (u.host) hostName = u.host; } hostName = hostName || region.cma; const refreshed = await this.refreshToken(hostName); if (refreshed) { return this.refreshAccessToken(error, maxRetryCount); // Retry after refreshing the token } console.log('API(401) case error:-', error.response); // For Basic Auth, exit immediately without retrying return; } break; case 429: case 408: if (maxRetryCount >= 3) { index_1.cliux.print('Max retry count reached, please login to proceed', { color: 'yellow', }); process.exit(1); } maxRetryCount++; // Increment for the next retry attempt // These cases require a wait, adding a delay before retrying await new Promise((resolve) => setTimeout(resolve, 1000)); // wait for 1 second return this.refreshAccessToken(error, maxRetryCount); // Retry default: return; // Handle other cases if necessary } } } refreshToken(hostName) { return new Promise((resolve) => { if (this.authType === 'BASIC') { index_1.cliux.print('Session timed out, please login to proceed', { color: 'yellow', }); process.exit(); } else if (this.authType === 'OAUTH') { index_1.authHandler.host = hostName; // NOTE Handle OAuth refresh token index_1.authHandler .compareOAuthExpiry(true) .then(() => { this.token = `Bearer ${index_1.configHandler.get('oauthAccessToken')}`; resolve(true); }) .catch((error) => { console.log(error); resolve(false); }); } else { index_1.cliux.print('You do not have the permissions to perform this action, please login to proceed', { color: 'yellow', }); process.exit(); } }); } } const authenticationHandler = new AuthenticationHandler(); exports.default = authenticationHandler;