Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 281 additions & 37 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contentstack-audit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"@oclif/plugin-help": "^5",
"@oclif/plugin-plugins": "^5.0.0",
"chalk": "^4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"chalk": "^4.0.0",
"debug": "^4.1.1",
"inquirer": "8.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-bootstrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $ npm install -g @contentstack/cli-cm-bootstrap
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-bootstrap/1.12.0 darwin-arm64 node-v22.2.0
@contentstack/cli-cm-bootstrap/1.13.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-bootstrap/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-bootstrap",
"description": "Bootstrap contentstack apps",
"version": "1.12.0",
"version": "1.13.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"scripts": {
Expand All @@ -17,9 +17,9 @@
"test:report": "nyc --reporter=lcov mocha \"test/**/*.test.js\""
},
"dependencies": {
"@contentstack/cli-cm-seed": "~1.9.0",
"@contentstack/cli-cm-seed": "~1.10.0",
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"inquirer": "8.2.4",
"mkdirp": "^1.0.4",
"tar": "^6.2.1 "
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-branches/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"@oclif/core": "^3.26.5",
"async": "^3.2.4",
"big-json": "^3.2.0",
Expand All @@ -26,7 +26,7 @@
},
"devDependencies": {
"@contentstack/cli-auth": "~1.3.19",
"@contentstack/cli-config": "~1.7.0",
"@contentstack/cli-config": "~1.7.2",
"@contentstack/cli-dev-dependencies": "~1.2.4",
"@oclif/plugin-help": "^5.1.19",
"@oclif/test": "^2.5.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-bulk-publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"bluebird": "^3.7.2",
"chalk": "^4.1.2",
"dotenv": "^16.1.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-clone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ npm install -g @contentstack/cli-cm-clone
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-clone/1.12.0 darwin-arm64 node-v22.2.0
@contentstack/cli-cm-clone/1.13.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
8 changes: 4 additions & 4 deletions packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@contentstack/cli-cm-clone",
"description": "Contentstack stack clone plugin",
"version": "1.12.0",
"version": "1.13.0",
"author": "Contentstack",
"bugs": "https://github.com/rohitmishra209/cli-cm-clone/issues",
"dependencies": {
"@colors/colors": "^1.5.0",
"@contentstack/cli-cm-export": "~1.13.0",
"@contentstack/cli-cm-import": "~1.18.0",
"@contentstack/cli-cm-export": "~1.14.0",
"@contentstack/cli-cm-import": "~1.19.0",
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"async": "^3.2.4",
"chalk": "^4.1.0",
"child_process": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-command/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"format": "eslint src/**/*.ts --fix"
},
"dependencies": {
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"contentstack": "^3.10.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"chalk": "^4.0.0",
"debug": "^4.1.1",
"inquirer": "8.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-export-to-csv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"chalk": "^4.1.0",
"fast-csv": "^4.3.6",
"inquirer": "8.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-export/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $ npm install -g @contentstack/cli-cm-export
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-export/1.13.0 darwin-arm64 node-v22.2.0
@contentstack/cli-cm-export/1.14.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-export/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@contentstack/cli-cm-export",
"description": "Contentstack CLI plugin to export content from stack",
"version": "1.13.0",
"version": "1.14.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-variants": "~1.0.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"@oclif/core": "^3.26.5",
"async": "^3.2.4",
"big-json": "^3.2.0",
Expand All @@ -26,7 +26,7 @@
},
"devDependencies": {
"@contentstack/cli-auth": "~1.3.19",
"@contentstack/cli-config": "~1.7.0",
"@contentstack/cli-config": "~1.7.2",
"@contentstack/cli-dev-dependencies": "~1.2.4",
"@oclif/plugin-help": "^5.1.19",
"@oclif/test": "^2.5.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $ npm install -g @contentstack/cli-cm-import
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-import/1.18.0 darwin-arm64 node-v22.2.0
@contentstack/cli-cm-import/1.19.0 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@contentstack/cli-cm-import",
"description": "Contentstack CLI plugin to import content into stack",
"version": "1.18.0",
"version": "1.19.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-audit": "~1.7.0",
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"@contentstack/management": "~1.17.0",
"@contentstack/cli-variants": "~1.0.0",
"@oclif/core": "^3.26.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-launch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dependencies": {
"@apollo/client": "^3.7.9",
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"@oclif/core": "^3.26.5",
"@oclif/plugin-help": "^5",
"@oclif/plugin-plugins": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-migrate-rte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"@contentstack/json-rte-serializer": "~2.0.4",
"collapse-whitespace": "^1.1.7",
"chalk": "^4.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-migration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"async": "^3.2.4",
"callsites": "^3.1.0",
"cardinal": "^2.1.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-seed/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@contentstack/cli-cm-seed",
"description": "create a Stack from existing content types, entries, assets, etc.",
"version": "1.9.0",
"version": "1.10.0",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-cm-import": "~1.18.0",
"@contentstack/cli-cm-import": "~1.19.0",
"@contentstack/cli-command": "~1.3.0",
"@contentstack/cli-utilities": "~1.7.2",
"@contentstack/cli-utilities": "~1.8.0",
"inquirer": "8.2.4",
"mkdirp": "^1.0.4",
"tar": "^6.1.13",
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-utilities",
"version": "1.7.3",
"version": "1.8.0",
"description": "Utilities for contentstack projects",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -41,6 +41,7 @@
"cli-table": "^0.3.11",
"conf": "^10.2.0",
"debug": "^4.1.1",
"dotenv": "^16.4.5",
"figures": "^3.2.0",
"inquirer": "8.2.4",
"inquirer-search-checkbox": "^1.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/contentstack-utilities/src/auth-handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import cliux from './cli-ux';
import HttpClient from './http-client';
import configHandler from './config-handler';
import dotenv from 'dotenv';
import * as ContentstackManagementSDK from '@contentstack/management';
import messageHandler from './message-handler';
const http = require('http');
const url = require('url');
import open from 'open';
import {LoggerService} from './logger';
const crypto = require('crypto');
dotenv.config();

/**
* @class
Expand Down
126 changes: 126 additions & 0 deletions packages/contentstack-utilities/src/authentication-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { cliux as ux, authHandler, configHandler } from './index';

class AuthenticationHandler {
private authType: string;
private isOAuth: boolean;
private token: string | null = null;

constructor() {
this.authType = configHandler.get('authorisationType');
this.isOAuth = this.authType === 'OAUTH';
}

async getAuthDetails(): Promise<void> {
try {
switch (this.authType) {
case 'BASIC':
this.token = configHandler.get('authtoken');
break;
case 'OAUTH':
await authHandler.compareOAuthExpiry();
this.token = `Bearer ${configHandler.get('oauthAccessToken')}`;
break;
default:
const authToken = configHandler.get('authtoken');
if (authToken) {
this.token = authToken;
} else {
ux.print('Session timed out, please login to proceed', {
color: 'yellow',
});
process.exit(1);
}
break;
}
} catch (error) {
ux.print(`Error occurred while fetching auth details: ${error.message}`, {
color: 'red',
});
process.exit(1);
}
}

get isOauthEnabled(): boolean {
return this.isOAuth;
}

get accessToken(): string {
if (!this.token) {
throw new Error('Token is not available. Please authenticate first.');
}
return this.token;
}

async refreshAccessToken(error: any, maxRetryCount = 1): Promise<void> {
if (error.response && error.response.status) {
if (maxRetryCount >= 3) {
ux.print('Max retry count reached, please login to proceed', {
color: 'yellow',
});
process.exit(1);
}

maxRetryCount++; // Increment for the next retry attempt

switch (error.response.status) {
case 401:
// NOTE: Refresh the token if the type is OAuth.
const region: { cma: string; name: string; cda: string } = configHandler.get('region') || {};
if (region?.cma) {
let hostName: string = '';
if (region.cma.startsWith('http')) {
const u = new URL(region.cma);
if (u.host) hostName = u.host;
}
hostName = hostName || region.cma;
await this.refreshToken(hostName);
return this.refreshAccessToken(error, maxRetryCount); // Retry after refreshing the token
}

case 429:
case 408:
// 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: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
if (this.authType === 'BASIC') {
// NOTE Handle basic auth 401 here
resolve(false);
ux.print('Session timed out, please login to proceed', {
color: 'yellow',
});
process.exit();
} else if (this.authType === 'OAUTH') {
authHandler.host = hostName;
// NOTE Handle OAuth refresh token
authHandler
.compareOAuthExpiry(true)
.then(() => {
this.token = `Bearer ${configHandler.get('oauthAccessToken')}`;
resolve(true);
})
.catch((error: any) => {
console.log(error);
resolve(false);
});
} else {
resolve(false);
ux.print('You do not have the permissions to perform this action, please login to proceed', {
color: 'yellow',
});
process.exit();
}
});
}
}

const authenticationHandler = new AuthenticationHandler();
export default authenticationHandler;
Loading