-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
138 lines (117 loc) · 3.83 KB
/
Copy pathui.js
File metadata and controls
138 lines (117 loc) · 3.83 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
const React = require('react');
const {Text, Box, useInput, useApp} = require('ink');
const {getConfigVar} = require('./config');
const {getCredentials, writeProfile} = require('./lib/authenticator');
const getConfig = flags => {
const tokenTtl = flags.duration || getConfigVar('TOKEN_TTL') || '43200';
const profileName = flags.profile || getConfigVar('TOKEN_PROFILE_NAME') || getConfigVar('PROFILE_NAME');
const accountName = flags.user || getConfigVar('ACCOUNT_NAME');
const accountNumber = flags.account || getConfigVar('ACCOUNT_NUMBER');
const mainProfileName = flags.source || getConfigVar('MAIN_PROFILE_NAME') || 'default';
return {tokenTtl, profileName, accountName, accountNumber, mainProfileName};
};
const App = (flags = {}) => {
const {tokenTtl, profileName, accountName, accountNumber, mainProfileName} = getConfig(flags);
const {exit} = useApp();
const [token, setToken] = React.useState('');
const [done, setDone] = React.useState(false);
const [error, setError] = React.useState(false);
const [isProcessing, setIsProcessing] = React.useState(false);
// Validate required configuration
React.useEffect(() => {
const missing = [];
if (!profileName) {
missing.push('profile name (--profile or TOKEN_PROFILE_NAME)');
}
if (!accountName) {
missing.push('account name (--user or ACCOUNT_NAME)');
}
if (!accountNumber) {
missing.push('account number (--account or ACCOUNT_NUMBER)');
}
if (missing.length > 0) {
setError(new Error(`Missing required configuration: ${missing.join(', ')}`));
}
}, [profileName, accountName, accountNumber]);
useInput((input, key) => {
// Don't process input if there are configuration errors
if (error) {
if (key.escape || (key.ctrl && input === 'c')) {
exit();
}
return;
}
if (key.delete || key.backspace) {
setToken(token.slice(0, -1));
return;
}
if (key.return) {
if (token.length !== 6) {
setError(new Error('MFA token must be 6 digits'));
return;
}
setIsProcessing(true);
(async () => {
try {
const cred = await getCredentials({token, profileName, mainProfileName, accountNumber, accountName, tokenTtl});
writeProfile(profileName)(cred);
setDone(true);
} catch (error) {
setError(error);
} finally {
setIsProcessing(false);
setTimeout(() => exit(), 1000); // Give user time to see success message
}
})();
return;
}
// Handle both single digits and pasted multi-digit strings
if (/^\d+$/.test(input)) {
const newToken = `${token}${input}`.slice(0, 6);
setToken(newToken);
}
});
if (error) {
return (
<Box flexDirection="column">
<Text color="red">❌ Error: {error.message}</Text>
<Text color="yellow">
Run 'mfa --help' for usage information.
</Text>
</Box>
);
}
return (
<Box flexDirection="column">
<Box marginBottom={1}>
<Text color="blue">🔐 AWS MFA Token Generator</Text>
</Box>
<Box flexDirection="column" marginBottom={1}>
<Text>📋 Configuration:</Text>
<Text> Profile: <Text color="cyan">{profileName}</Text></Text>
<Text> Account: <Text color="cyan">{accountNumber}</Text></Text>
<Text> User: <Text color="cyan">{accountName}</Text></Text>
<Text> Source: <Text color="cyan">{mainProfileName}</Text></Text>
<Text> Duration: <Text color="cyan">{tokenTtl}s</Text></Text>
</Box>
{!isProcessing && !done && (
<Box>
<Text>
🔢 Enter MFA code from your authenticator app: <Text color="green">{token.replace(/./g, '*').slice(0, -1)}{token.slice(-1)}</Text>
</Text>
</Box>
)}
{isProcessing && (
<Box>
<Text color="blue">⏳ Processing MFA token...</Text>
</Box>
)}
{done && (
<Box>
<Text color="green">✅ Credentials successfully updated in profile '{profileName}'!</Text>
</Box>
)}
</Box>
);
};
module.exports = App;