Skip to content

Commit 5002713

Browse files
cs-rajharshithad0703
authored andcommitted
Merge pull request #2328 from contentstack/main
Back Merge
1 parent 181448f commit 5002713

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

packages/contentstack-auth/src/utils/auth-handler.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,26 @@ class AuthHandler {
118118
if (result.user) {
119119
log.debug('Login successful, user found', { module: 'auth-handler', userEmail: result.user.email });
120120
resolve(result.user as User);
121-
} else if (result.error_code === 294) {
122-
const tfToken = await this.handleOTPFlow(tfaToken, loginPayload);
123-
121+
} else {
122+
log.debug('Login failed: no user found.', { module: 'auth-handler', result });
123+
reject(new Error(messageHandler.parse('CLI_AUTH_LOGIN_NO_USER')));
124+
}
125+
})
126+
.catch(async (error: any) => {
127+
if (error.errorCode === 294) {
124128
try {
129+
const tfToken = await this.handleOTPFlow(tfaToken, loginPayload);
125130
resolve(await this.login(email, password, tfToken));
126131
} catch (error) {
127132
log.debug('Login with TFA token failed.', { module: 'auth-handler', error });
128133
cliux.print('CLI_AUTH_2FA_FAILED', { color: 'red' });
129134
reject(error);
130135
}
131136
} else {
132-
log.debug('Login failed: no user found.', { module: 'auth-handler', result });
133-
reject(new Error(messageHandler.parse('CLI_AUTH_LOGIN_NO_USER')));
137+
log.debug('Login API call failed.', { module: 'auth-handler', error: error?.errorMessage || error });
138+
cliux.print('CLI_AUTH_LOGIN_FAILED', { color: 'yellow' });
139+
reject(error);
134140
}
135-
})
136-
.catch((error: any) => {
137-
log.debug('Login API call failed.', { module: 'auth-handler', error: error?.errorMessage || error });
138-
cliux.print('CLI_AUTH_LOGIN_FAILED', { color: 'yellow' });
139-
handleAndLogError(error, { module: 'auth-handler' });
140141
});
141142
} else {
142143
const hasEmail = !!email;
@@ -203,7 +204,7 @@ class AuthHandler {
203204
.catch((error: Error) => {
204205
log.debug('Token validation failed.', { module: 'auth-handler', error: error.message });
205206
cliux.print('CLI_AUTH_TOKEN_VALIDATION_FAILED', { color: 'yellow' });
206-
handleAndLogError(error, { module: 'auth-handler' });
207+
reject(error);
207208
});
208209
} else {
209210
log.debug('Token validation failed: no auth token provided.', { module: 'auth-handler' });

packages/contentstack-auth/src/utils/mfa-handler.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@ class MFAHandler {
8787
}
8888
}
8989

90-
if (!secret) {
91-
log.debug('Checking stored MFA secret', { module: 'mfa-handler' });
92-
const mfaConfig = configHandler.get('mfa');
93-
if (mfaConfig?.secret) {
94-
try {
95-
secret = this.encrypter.decrypt(mfaConfig.secret);
96-
source = 'stored configuration';
97-
} catch (error) {
98-
log.debug('Failed to decrypt stored MFA secret', { module: 'mfa-handler', error });
99-
handleAndLogError(error, { module: 'mfa-handler' }, messageHandler.parse('CLI_AUTH_MFA_DECRYPT_FAILED'));
100-
}
101-
}
102-
}
103-
10490
if (secret) {
10591
try {
10692
const code = this.generateMFACode(secret);

packages/contentstack-auth/test/unit/auth-handler.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ describe('Auth Handler', function () {
3232
return Promise.reject(new Error('Invalid 2FA code'));
3333
}
3434
} else {
35-
return Promise.resolve({ error_code: 294 });
35+
const error: any = new Error('2FA required');
36+
error.errorCode = 294;
37+
return Promise.reject(error);
3638
}
3739
}
3840
return Promise.resolve({ user });
@@ -115,13 +117,23 @@ describe('Auth Handler', function () {
115117
it('Login with 2FA enabled invalid otp, failed to login', async function () {
116118
this.timeout(10000);
117119
TFAEnabled = true;
118-
let result;
120+
askOTPStub.restore();
121+
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
122+
return Promise.resolve(InvalidTFATestToken);
123+
});
119124
try {
120-
result = await authHandler.login(credentials.email, credentials.password);
125+
await authHandler.login(credentials.email, credentials.password);
126+
expect.fail('Should have thrown an error');
121127
} catch (error) {
122-
result = error;
128+
expect(error).to.be.instanceOf(Error);
129+
expect((error as Error).message).to.include('Invalid 2FA code');
130+
} finally {
131+
TFAEnabled = false;
132+
askOTPStub.restore();
133+
askOTPStub = sinon.stub(interactive, 'askOTP').callsFake(function () {
134+
return Promise.resolve(TFATestToken);
135+
});
123136
}
124-
TFAEnabled = false;
125137
});
126138

127139
it('Login with 2FA enabled with sms channel, should be logged in successfully', async function () {

packages/contentstack-utilities/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"author": "contentstack",
3333
"license": "MIT",
3434
"dependencies": {
35-
"@contentstack/management": "~1.25.1",
35+
"@contentstack/management": "~1.27.3",
3636
"@contentstack/marketplace-sdk": "^1.4.0",
3737
"@oclif/core": "^4.3.0",
3838
"axios": "^1.9.0",

0 commit comments

Comments
 (0)