Skip to content

Commit a23716b

Browse files
committed
Merge branch 'development' into fix/dx-3434
2 parents 623e73b + 03f9560 commit a23716b

File tree

23 files changed

+2754
-2339
lines changed

23 files changed

+2754
-2339
lines changed

package-lock.json

Lines changed: 77 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-auth/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli-auth
1818
$ csdx COMMAND
1919
running command...
2020
$ csdx (--version)
21-
@contentstack/cli-auth/1.5.2 darwin-x64 node-v22.18.0
21+
@contentstack/cli-auth/1.6.0 darwin-arm64 node-v22.14.0
2222
$ csdx --help [COMMAND]
2323
USAGE
2424
$ csdx COMMAND

packages/contentstack-auth/env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ BRANCH_ENABLED_DELIVERY_TOKEN
66
BRANCH_DISABLED_DELIVERY_TOKEN
77
BRANCH_ENABLED_ENVIRONMENT
88
BRANCH_DISABLED_ENVIRONMENT
9+
CONTENTSTACK_MFA_SECRET

packages/contentstack-auth/messages/index.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,16 @@
4949
"CLI_AUTH_TOKENS_VALIDATION_INVALID_API_KEY": "Invalid api key",
5050
"CLI_AUTH_EXIT_PROCESS": "Exiting the process...",
5151
"CLI_SELECT_TOKEN_TYPE": "Select the type of token to add",
52-
"CLI_AUTH_ENTER_BRANCH": "Enter branch name"
52+
"CLI_AUTH_MFA_INVALID_SECRET": "Invalid MFA secret format. Verify your authentication setup.",
53+
"CLI_AUTH_MFA_GENERATION_FAILED": "Failed to generate MFA code. Switching to manual MFA code entry.",
54+
"CLI_AUTH_MFA_DECRYPT_FAILED": "Failed to decrypt stored MFA secret. Try Resetting the MFA secret. Proceeding for Manual MFA code input.",
55+
"CLI_AUTH_MFA_INVALID_CODE": "Invalid authentication code format.",
56+
"CLI_AUTH_MFA_RECONFIGURE_HINT": "Consider reconfiguring MFA using config:mfa:add",
57+
"CLI_AUTH_SMS_OTP_FAILED": "Failed to send SMS OTP. Try again or use a different two-factor authentication method.",
58+
"CLI_AUTH_2FA_FAILED": "Two-factor authentication failed! Try again.",
59+
"CLI_AUTH_LOGIN_NO_USER": "No user found with the provided credentials!",
60+
"CLI_AUTH_LOGIN_NO_CREDENTIALS": "No credentials provided. Enter your email and password to log in.",
61+
"CLI_AUTH_LOGOUT_NO_TOKEN": "No auth token found. Log in before logging out.",
62+
"CLI_AUTH_TOKEN_VALIDATION_FAILED": "Token validation failed. Log in again.",
63+
"CLI_AUTH_TOKEN_VALIDATION_NO_TOKEN": "No auth token found. Log in to continue.."
5364
}

packages/contentstack-auth/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@contentstack/cli-auth",
33
"description": "Contentstack CLI plugin for authentication activities",
4-
"version": "1.5.2",
4+
"version": "1.6.0",
55
"author": "Contentstack",
66
"bugs": "https://github.com/contentstack/cli/issues",
77
"scripts": {
@@ -25,7 +25,8 @@
2525
"@contentstack/cli-command": "~1.6.0",
2626
"@contentstack/cli-utilities": "~1.13.1",
2727
"@oclif/core": "^4.3.0",
28-
"@oclif/plugin-help": "^6.2.28"
28+
"@oclif/plugin-help": "^6.2.28",
29+
"otplib": "^12.0.1"
2930
},
3031
"devDependencies": {
3132
"@fancy-test/nock": "^0.1.1",

packages/contentstack-auth/src/commands/auth/login.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
messageHandler,
1111
} from '@contentstack/cli-utilities';
1212
import { User } from '../../interfaces';
13-
import { authHandler, interactive } from '../../utils';
13+
import { authHandler, interactive, mfaHandler } from '../../utils';
1414
import { BaseCommand } from '../../base-command';
1515

1616
export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
@@ -40,6 +40,7 @@ export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
4040
required: false,
4141
exclusive: ['oauth'],
4242
}),
43+
4344
oauth: flags.boolean({
4445
description: 'Enables single sign-on (SSO) in Contentstack CLI.',
4546
required: false,
@@ -57,9 +58,9 @@ export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
5758
log.debug('Initializing management API client', this.contextDetails);
5859
const managementAPIClient = await managementSDKClient({ host: this.cmaHost, skipTokenValidity: true });
5960
log.debug('Management API client initialized successfully', this.contextDetails);
60-
61+
6162
const { flags: loginFlags } = await this.parse(LoginCommand);
62-
log.debug('Token add flags parsed', {...this.contextDetails, flags: loginFlags});
63+
log.debug('Token add flags parsed', { ...this.contextDetails, flags: loginFlags });
6364

6465
authHandler.client = managementAPIClient;
6566
log.debug('Auth handler client set', this.contextDetails);
@@ -76,12 +77,22 @@ export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
7677
log.debug('Starting basic authentication flow', this.contextDetails);
7778
const username = loginFlags?.username || (await interactive.askUsername());
7879
const password = loginFlags?.password || (await interactive.askPassword());
79-
log.debug('Credentials obtained', { ...this.contextDetails, hasUsername: !!username, hasPassword: !!password });
80+
log.debug('Credentials obtained', {
81+
...this.contextDetails,
82+
hasUsername: !!username,
83+
hasPassword: !!password,
84+
});
85+
8086
await this.login(username, password);
8187
}
8288
} catch (error) {
83-
log.debug('Login command failed', { ...this.contextDetails, error });
84-
cliux.error('CLI_AUTH_LOGIN_FAILED');
89+
log.debug('Login command failed', {
90+
...this.contextDetails,
91+
error,
92+
});
93+
if ((error?.message && error?.message.includes('2FA')) || error?.message.includes('MFA')) {
94+
error.message = `${error.message}\nFor more information about MFA, visit: https://www.contentstack.com/docs/developers/security/multi-factor-authentication`;
95+
}
8596
handleAndLogError(error, { ...this.contextDetails });
8697
process.exit();
8798
}
@@ -92,7 +103,19 @@ export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
92103

93104
try {
94105
log.debug('Calling auth handler login', this.contextDetails);
95-
const user: User = await authHandler.login(username, password);
106+
let tfaToken: string | undefined;
107+
108+
try {
109+
tfaToken = await mfaHandler.getMFACode();
110+
if (tfaToken) {
111+
log.debug('MFA token generated from stored configuration', this.contextDetails);
112+
}
113+
} catch (error) {
114+
log.debug('Failed to generate MFA token from config', { ...this.contextDetails, error });
115+
tfaToken = undefined;
116+
}
117+
118+
const user: User = await authHandler.login(username, password, tfaToken);
96119
log.debug('Auth handler login completed', {
97120
...this.contextDetails,
98121
hasUser: !!user,

0 commit comments

Comments
 (0)