-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlogin.ts
More file actions
142 lines (123 loc) · 4.94 KB
/
login.ts
File metadata and controls
142 lines (123 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
cliux,
CLIError,
authHandler as oauthHandler,
flags,
managementSDKClient,
FlagInput,
log,
handleAndLogError,
messageHandler,
} from '@contentstack/cli-utilities';
import { User } from '../../interfaces';
import { authHandler, interactive, mfaHandler } from '../../utils';
import { BaseCommand } from '../../base-command';
export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
static run; // to fix the test issue
static description = 'User sessions login';
static examples = [
'$ csdx auth:login',
'$ csdx auth:login -u <username>',
'$ csdx auth:login -u <username> -p <password>',
'$ csdx auth:login --username <username>',
'$ csdx auth:login --username <username> --password <password>',
];
static flags: FlagInput = {
username: flags.string({
char: 'u',
description: 'Email address of your Contentstack account.',
multiple: false,
required: false,
exclusive: ['oauth'],
}),
password: flags.string({
char: 'p',
description: 'Password of your Contentstack app.',
multiple: false,
required: false,
exclusive: ['oauth'],
}),
oauth: flags.boolean({
description: 'Enables single sign-on (SSO) in Contentstack CLI.',
required: false,
default: false,
exclusive: ['username', 'password'],
}),
};
static aliases = ['login'];
async run(): Promise<any> {
log.debug('LoginCommand run method started', this.contextDetails);
try {
log.debug('Initializing management API client', this.contextDetails);
const managementAPIClient = await managementSDKClient({ host: this.cmaHost, skipTokenValidity: true });
log.debug('Management API client initialized successfully', this.contextDetails);
const { flags: loginFlags } = await this.parse(LoginCommand);
log.debug('Token add flags parsed', { ...this.contextDetails, flags: loginFlags });
authHandler.client = managementAPIClient;
log.debug('Auth handler client set', this.contextDetails);
const oauth = loginFlags?.oauth;
log.debug(`Authentication method: ${oauth ? 'OAuth' : 'Basic'}`, this.contextDetails);
if (oauth === true) {
log.debug('Starting OAuth authentication flow', this.contextDetails);
oauthHandler.host = this.cmaHost;
await oauthHandler.oauth();
log.debug('OAuth authentication completed', this.contextDetails);
} else {
log.debug('Starting basic authentication flow', this.contextDetails);
const username = loginFlags?.username || (await interactive.askUsername());
const password = loginFlags?.password || (await interactive.askPassword());
log.debug('Credentials obtained', {
...this.contextDetails,
hasUsername: !!username,
hasPassword: !!password,
});
await this.login(username, password);
}
} catch (error) {
log.debug('Login command failed', {
...this.contextDetails,
error,
});
if ((error?.message && error?.message.includes('2FA')) || error?.message.includes('MFA')) {
error.message = `${error.message}\nFor more information about MFA, visit: https://www.contentstack.com/docs/developers/security/multi-factor-authentication`;
}
handleAndLogError(error, { ...this.contextDetails });
process.exit();
}
}
async login(username: string, password: string): Promise<void> {
log.debug('Starting login process', { ...this.contextDetails, username });
try {
log.debug('Calling auth handler login', this.contextDetails);
let tfaToken: string | undefined;
try {
tfaToken = await mfaHandler.getMFACode();
if (tfaToken) {
log.debug('MFA token generated from stored configuration', this.contextDetails);
}
} catch (error) {
log.debug('Failed to generate MFA token from config', { ...this.contextDetails, error });
tfaToken = undefined;
}
const user: User = await authHandler.login(username, password, tfaToken);
log.debug('Auth handler login completed', {
...this.contextDetails,
hasUser: !!user,
hasAuthToken: !!user?.authtoken,
userEmail: user?.email,
});
if (typeof user !== 'object' || !user.authtoken || !user.email) {
log.debug('Login failed - invalid user response', { ...this.contextDetails, user });
throw new CLIError('Failed to login - invalid response');
}
log.debug('Setting config data for basic auth', this.contextDetails);
await oauthHandler.setConfigData('basicAuth', user);
log.debug('Config data set successfully', this.contextDetails);
log.success(messageHandler.parse('CLI_AUTH_LOGIN_SUCCESS'), this.contextDetails);
log.debug('Login process completed successfully', this.contextDetails);
} catch (error) {
log.debug('Login process failed', { ...this.contextDetails, error });
throw error;
}
}
}