@kumologica/sdk 3.6.2-beta1 → 3.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumologica/sdk",
|
|
3
|
-
"version": "3.6.2
|
|
3
|
+
"version": "3.6.2",
|
|
4
4
|
"productName": "Kumologica Designer",
|
|
5
5
|
"copyright": "Copyright 2020 Kumologica Pty Ltd, All Rights Reserved.",
|
|
6
6
|
"author": "Kumologica Pty Ltd <contact@kumologica.com>",
|
|
@@ -83,9 +83,9 @@
|
|
|
83
83
|
"@aws-sdk/credential-providers": "^3.556.0",
|
|
84
84
|
"@aws-sdk/lib-dynamodb": "^3.549.0",
|
|
85
85
|
"@electron/remote": "^2.0.8",
|
|
86
|
-
"@kumologica/builder": "3.6.2
|
|
87
|
-
"@kumologica/devkit": "3.6.2
|
|
88
|
-
"@kumologica/runtime": "3.6.2
|
|
86
|
+
"@kumologica/builder": "3.6.2",
|
|
87
|
+
"@kumologica/devkit": "3.6.2",
|
|
88
|
+
"@kumologica/runtime": "3.6.2",
|
|
89
89
|
"adm-zip": "0.4.13",
|
|
90
90
|
"ajv": "8.10.0",
|
|
91
91
|
"archive-type": "^4.0.0",
|
|
@@ -1,425 +0,0 @@
|
|
|
1
|
-
const {
|
|
2
|
-
CognitoIdentityProviderClient,
|
|
3
|
-
SignUpCommand,
|
|
4
|
-
ConfirmSignUpCommand,
|
|
5
|
-
InitiateAuthCommand,
|
|
6
|
-
RefreshTokenCommand,
|
|
7
|
-
} = require('@aws-sdk/client-cognito-identity-provider');
|
|
8
|
-
const fs = require('fs').promises;
|
|
9
|
-
const path = require('path');
|
|
10
|
-
const axios = require('axios');
|
|
11
|
-
const { prompt } = require('enquirer'); // Import enquirer for prompting
|
|
12
|
-
|
|
13
|
-
// Simulated DNS-based config fetch
|
|
14
|
-
async function fetchCognitoConfig(dnsName = 'config.myapp.com') {
|
|
15
|
-
try {
|
|
16
|
-
const response = await axios.get(`https://${dnsName}/cognito-config`);
|
|
17
|
-
return response.data;
|
|
18
|
-
} catch (err) {
|
|
19
|
-
console.error('Config fetch error:', err.message);
|
|
20
|
-
return {
|
|
21
|
-
region: 'YOUR_AWS_REGION', // e.g., 'us-east-1'
|
|
22
|
-
userPoolId: 'YOUR_USER_POOL_ID', // e.g., 'us-east-1_abc123'
|
|
23
|
-
clientId: 'YOUR_CLIENT_ID' // e.g., '1a2b3c4d5e6f7g8h9i0j'
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Directory and file for storing tokens
|
|
29
|
-
const tokensDir = path.join(__dirname, 'tokens');
|
|
30
|
-
const tokensFile = path.join(tokensDir, 'tokens.json');
|
|
31
|
-
|
|
32
|
-
// --- Initialize Cognito Client with Dynamic Config ---
|
|
33
|
-
async function getCognitoClient() {
|
|
34
|
-
const config = await fetchCognitoConfig();
|
|
35
|
-
return {
|
|
36
|
-
client: new CognitoIdentityProviderClient({ region: config.region }),
|
|
37
|
-
userPoolId: config.userPoolId,
|
|
38
|
-
clientId: config.clientId,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// --- Signup Function with Email Verification ---
|
|
43
|
-
async function signUp(email, password, client, clientId) {
|
|
44
|
-
const params = {
|
|
45
|
-
ClientId: clientId,
|
|
46
|
-
Username: email,
|
|
47
|
-
Password: password,
|
|
48
|
-
UserAttributes: [{ Name: 'email', Value: email }],
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const command = new SignUpCommand(params);
|
|
52
|
-
const result = await client.send(command);
|
|
53
|
-
console.log('Signup successful! Please check your email for a verification code.', result.UserSub);
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// --- Email Verification Function ---
|
|
58
|
-
async function verifyEmail(email, verificationCode, client, clientId) {
|
|
59
|
-
const params = {
|
|
60
|
-
ClientId: clientId,
|
|
61
|
-
Username: email,
|
|
62
|
-
ConfirmationCode: verificationCode,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const command = new ConfirmSignUpCommand(params);
|
|
66
|
-
const result = await client.send(command);
|
|
67
|
-
console.log('Email verified successfully!');
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// --- Login Function ---
|
|
72
|
-
async function login(email, password, client, clientId) {
|
|
73
|
-
const params = {
|
|
74
|
-
AuthFlow: 'USER_PASSWORD_AUTH',
|
|
75
|
-
ClientId: clientId,
|
|
76
|
-
AuthParameters: { USERNAME: email, PASSWORD: password },
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const command = new InitiateAuthCommand(params);
|
|
80
|
-
const result = await client.send(command);
|
|
81
|
-
const tokens = {
|
|
82
|
-
accessToken: result.AuthenticationResult.AccessToken,
|
|
83
|
-
idToken: result.AuthenticationResult.IdToken,
|
|
84
|
-
refreshToken: result.AuthenticationResult.RefreshToken,
|
|
85
|
-
};
|
|
86
|
-
console.log('Login successful!', tokens);
|
|
87
|
-
|
|
88
|
-
await fs.mkdir(tokensDir, { recursive: true });
|
|
89
|
-
await fs.writeFile(tokensFile, JSON.stringify(tokens, null, 2));
|
|
90
|
-
console.log('Tokens stored in', tokensFile);
|
|
91
|
-
|
|
92
|
-
return tokens;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// --- Token Validation Function ---
|
|
96
|
-
function validateToken(token) {
|
|
97
|
-
try {
|
|
98
|
-
const decoded = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
|
|
99
|
-
const currentTime = Math.floor(Date.now() / 1000);
|
|
100
|
-
if (decoded.exp < currentTime) {
|
|
101
|
-
console.log('Token expired');
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
console.log('Token is valid');
|
|
105
|
-
return true;
|
|
106
|
-
} catch (err) {
|
|
107
|
-
console.error('Token validation error:', err.message);
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// --- Refresh Token Function ---
|
|
113
|
-
async function refreshToken(refreshToken, client, clientId) {
|
|
114
|
-
const params = {
|
|
115
|
-
AuthFlow: 'REFRESH_TOKEN_AUTH',
|
|
116
|
-
ClientId: clientId,
|
|
117
|
-
AuthParameters: { REFRESH_TOKEN: refreshToken },
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const command = new RefreshTokenCommand(params);
|
|
121
|
-
const result = await client.send(command);
|
|
122
|
-
const newTokens = {
|
|
123
|
-
accessToken: result.AuthenticationResult.AccessToken,
|
|
124
|
-
idToken: result.AuthenticationResult.IdToken,
|
|
125
|
-
refreshToken: refreshToken,
|
|
126
|
-
};
|
|
127
|
-
console.log('Token refreshed successfully!', newTokens);
|
|
128
|
-
|
|
129
|
-
await fs.writeFile(tokensFile, JSON.stringify(newTokens, null, 2));
|
|
130
|
-
console.log('Refreshed tokens updated in', tokensFile);
|
|
131
|
-
|
|
132
|
-
return newTokens;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// --- Check for Existing Tokens ---
|
|
136
|
-
async function getStoredTokens() {
|
|
137
|
-
try {
|
|
138
|
-
const data = await fs.readFile(tokensFile, 'utf8');
|
|
139
|
-
return JSON.parse(data);
|
|
140
|
-
} catch (err) {
|
|
141
|
-
if (err.code === 'ENOENT') {
|
|
142
|
-
console.log('No stored tokens found.');
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
throw err;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// --- Updated Main Function with Enquirer Prompt ---
|
|
150
|
-
async function main() {
|
|
151
|
-
const email = 'user@example.com'; // You could prompt for this too
|
|
152
|
-
const password = 'SecurePass123!'; // You could prompt for this too
|
|
153
|
-
|
|
154
|
-
try {
|
|
155
|
-
const { client, clientId } = await getCognitoClient();
|
|
156
|
-
|
|
157
|
-
const storedTokens = await getStoredTokens();
|
|
158
|
-
|
|
159
|
-
if (storedTokens) {
|
|
160
|
-
const isValid = validateToken(storedTokens.accessToken);
|
|
161
|
-
if (isValid) {
|
|
162
|
-
console.log('Using existing valid tokens:', storedTokens);
|
|
163
|
-
return storedTokens;
|
|
164
|
-
} else {
|
|
165
|
-
const refreshedTokens = await refreshToken(storedTokens.refreshToken, client, clientId);
|
|
166
|
-
console.log('Using refreshed tokens:', refreshedTokens);
|
|
167
|
-
return refreshedTokens;
|
|
168
|
-
}
|
|
169
|
-
} else {
|
|
170
|
-
// No tokens, proceed with signup
|
|
171
|
-
await signUp(email, password, client, clientId);
|
|
172
|
-
|
|
173
|
-
// Prompt user for verification code
|
|
174
|
-
const { verificationCode } = await prompt({
|
|
175
|
-
type: 'input',
|
|
176
|
-
name: 'verificationCode',
|
|
177
|
-
message: 'Enter the verification code sent to your email:',
|
|
178
|
-
validate: (value) => value.length > 0 || 'Please enter a valid code',
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
await verifyEmail(email, verificationCode, client, clientId);
|
|
182
|
-
const tokens = await login(email, password, client, clientId);
|
|
183
|
-
console.log('New tokens generated and stored:', tokens);
|
|
184
|
-
return tokens;
|
|
185
|
-
}
|
|
186
|
-
} catch (error) {
|
|
187
|
-
console.error('Error in process:', error.message);
|
|
188
|
-
throw error;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Run the script
|
|
193
|
-
main()
|
|
194
|
-
.then((tokens) => console.log('Final tokens:', tokens))
|
|
195
|
-
.catch((err) => console.error('Main execution failed:', err.message));
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
-----
|
|
200
|
-
|
|
201
|
-
v2
|
|
202
|
-
|
|
203
|
-
const {
|
|
204
|
-
CognitoIdentityProviderClient,
|
|
205
|
-
SignUpCommand,
|
|
206
|
-
ConfirmSignUpCommand,
|
|
207
|
-
InitiateAuthCommand,
|
|
208
|
-
RefreshTokenCommand,
|
|
209
|
-
} = require('@aws-sdk/client-cognito-identity-provider');
|
|
210
|
-
const fs = require('fs').promises;
|
|
211
|
-
const path = require('path');
|
|
212
|
-
const axios = require('axios');
|
|
213
|
-
const { prompt } = require('enquirer');
|
|
214
|
-
const jwt = require('jsonwebtoken');
|
|
215
|
-
const jwksClient = require('jwks-rsa');
|
|
216
|
-
|
|
217
|
-
// Simulated DNS-based config fetch
|
|
218
|
-
async function fetchCognitoConfig(dnsName = 'config.myapp.com') {
|
|
219
|
-
try {
|
|
220
|
-
const response = await axios.get(`https://${dnsName}/cognito-config`);
|
|
221
|
-
return response.data;
|
|
222
|
-
} catch (err) {
|
|
223
|
-
console.error('Config fetch error:', err.message);
|
|
224
|
-
return {
|
|
225
|
-
region: 'YOUR_AWS_REGION', // e.g., 'us-east-1'
|
|
226
|
-
userPoolId: 'YOUR_USER_POOL_ID', // e.g., 'us-east-1_abc123'
|
|
227
|
-
clientId: 'YOUR_CLIENT_ID' // e.g., '1a2b3c4d5e6f7g8h9i0j'
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// Directory and file for storing tokens
|
|
233
|
-
const tokensDir = path.join(__dirname, 'tokens');
|
|
234
|
-
const tokensFile = path.join(tokensDir, 'tokens.json');
|
|
235
|
-
|
|
236
|
-
// --- Initialize Cognito Client with Dynamic Config ---
|
|
237
|
-
async function getCognitoClient() {
|
|
238
|
-
const config = await fetchCognitoConfig();
|
|
239
|
-
return {
|
|
240
|
-
client: new CognitoIdentityProviderClient({ region: config.region }),
|
|
241
|
-
userPoolId: config.userPoolId,
|
|
242
|
-
clientId: config.clientId,
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// --- Signup Function with Email Verification ---
|
|
247
|
-
async function signUp(email, password, client, clientId) {
|
|
248
|
-
const params = {
|
|
249
|
-
ClientId: clientId,
|
|
250
|
-
Username: email,
|
|
251
|
-
Password: password,
|
|
252
|
-
UserAttributes: [{ Name: 'email', Value: email }],
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
const command = new SignUpCommand(params);
|
|
256
|
-
const result = await client.send(command);
|
|
257
|
-
console.log('Signup successful! Please check your email for a verification code.', result.UserSub);
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// --- Email Verification Function ---
|
|
262
|
-
async function verifyEmail(email, verificationCode, client, clientId) {
|
|
263
|
-
const params = {
|
|
264
|
-
ClientId: clientId,
|
|
265
|
-
Username: email,
|
|
266
|
-
ConfirmationCode: verificationCode,
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
const command = new ConfirmSignUpCommand(params);
|
|
270
|
-
const result = await client.send(command);
|
|
271
|
-
console.log('Email verified successfully!');
|
|
272
|
-
return result;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// --- Login Function ---
|
|
276
|
-
async function login(email, password, client, clientId) {
|
|
277
|
-
const params = {
|
|
278
|
-
AuthFlow: 'USER_PASSWORD_AUTH',
|
|
279
|
-
ClientId: clientId,
|
|
280
|
-
AuthParameters: { USERNAME: email, PASSWORD: password },
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
const command = new InitiateAuthCommand(params);
|
|
284
|
-
const result = await client.send(command);
|
|
285
|
-
const tokens = {
|
|
286
|
-
accessToken: result.AuthenticationResult.AccessToken,
|
|
287
|
-
idToken: result.AuthenticationResult.IdToken,
|
|
288
|
-
refreshToken: result.AuthenticationResult.RefreshToken,
|
|
289
|
-
};
|
|
290
|
-
console.log('Login successful!', tokens);
|
|
291
|
-
|
|
292
|
-
await fs.mkdir(tokensDir, { recursive: true });
|
|
293
|
-
await fs.writeFile(tokensFile, JSON.stringify(tokens, null, 2));
|
|
294
|
-
console.log('Tokens stored in', tokensFile);
|
|
295
|
-
|
|
296
|
-
return tokens;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// --- Full Token Validation Function for Production ---
|
|
300
|
-
async function validateToken(token, userPoolId, clientId) {
|
|
301
|
-
const jwksUri = `https://cognito-idp.${process.env.AWS_REGION || 'us-east-1'}.amazonaws.com/${userPoolId}/.well-known/jwks.json`;
|
|
302
|
-
const client = jwksClient({
|
|
303
|
-
jwksUri,
|
|
304
|
-
cache: true,
|
|
305
|
-
rateLimit: true,
|
|
306
|
-
jwksRequestsPerMinute: 5,
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
function getKey(header, callback) {
|
|
310
|
-
client.getSigningKey(header.kid, (err, key) => {
|
|
311
|
-
if (err) {
|
|
312
|
-
callback(err);
|
|
313
|
-
} else {
|
|
314
|
-
const signingKey = key.getPublicKey();
|
|
315
|
-
callback(null, signingKey);
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return new Promise((resolve, reject) => {
|
|
321
|
-
jwt.verify(
|
|
322
|
-
token,
|
|
323
|
-
getKey,
|
|
324
|
-
{
|
|
325
|
-
issuer: `https://cognito-idp.${process.env.AWS_REGION || 'us-east-1'}.amazonaws.com/${userPoolId}`,
|
|
326
|
-
audience: clientId, // For access token; idToken uses 'aud' differently, adjust if validating idToken
|
|
327
|
-
algorithms: ['RS256'],
|
|
328
|
-
},
|
|
329
|
-
(err, decoded) => {
|
|
330
|
-
if (err) {
|
|
331
|
-
console.error('Token validation failed:', err.message);
|
|
332
|
-
resolve(false);
|
|
333
|
-
} else {
|
|
334
|
-
console.log('Token is valid:', decoded);
|
|
335
|
-
resolve(true);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
);
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// --- Refresh Token Function ---
|
|
343
|
-
async function refreshToken(refreshToken, client, clientId) {
|
|
344
|
-
const params = {
|
|
345
|
-
AuthFlow: 'REFRESH_TOKEN_AUTH',
|
|
346
|
-
ClientId: clientId,
|
|
347
|
-
AuthParameters: { REFRESH_TOKEN: refreshToken },
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
const command = new RefreshTokenCommand(params);
|
|
351
|
-
const result = await client.send(command);
|
|
352
|
-
const newTokens = {
|
|
353
|
-
accessToken: result.AuthenticationResult.AccessToken,
|
|
354
|
-
idToken: result.AuthenticationResult.IdToken,
|
|
355
|
-
refreshToken: refreshToken,
|
|
356
|
-
};
|
|
357
|
-
console.log('Token refreshed successfully!', newTokens);
|
|
358
|
-
|
|
359
|
-
await fs.writeFile(tokensFile, JSON.stringify(newTokens, null, 2));
|
|
360
|
-
console.log('Refreshed tokens updated in', tokensFile);
|
|
361
|
-
|
|
362
|
-
return newTokens;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
// --- Check for Existing Tokens ---
|
|
366
|
-
async function getStoredTokens() {
|
|
367
|
-
try {
|
|
368
|
-
const data = await fs.readFile(tokensFile, 'utf8');
|
|
369
|
-
return JSON.parse(data);
|
|
370
|
-
} catch (err) {
|
|
371
|
-
if (err.code === 'ENOENT') {
|
|
372
|
-
console.log('No stored tokens found.');
|
|
373
|
-
return null;
|
|
374
|
-
}
|
|
375
|
-
throw err;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// --- Updated Main Function ---
|
|
380
|
-
async function main() {
|
|
381
|
-
const email = 'user@example.com';
|
|
382
|
-
const password = 'SecurePass123!';
|
|
383
|
-
|
|
384
|
-
try {
|
|
385
|
-
const { client, userPoolId, clientId } = await getCognitoClient();
|
|
386
|
-
|
|
387
|
-
const storedTokens = await getStoredTokens();
|
|
388
|
-
|
|
389
|
-
if (storedTokens) {
|
|
390
|
-
const isValid = await validateToken(storedTokens.accessToken, userPoolId, clientId);
|
|
391
|
-
if (isValid) {
|
|
392
|
-
console.log('Using existing valid tokens:', storedTokens);
|
|
393
|
-
return storedTokens;
|
|
394
|
-
} else {
|
|
395
|
-
const refreshedTokens = await refreshToken(storedTokens.refreshToken, client, clientId);
|
|
396
|
-
console.log('Using refreshed tokens:', refreshedTokens);
|
|
397
|
-
return refreshedTokens;
|
|
398
|
-
}
|
|
399
|
-
} else {
|
|
400
|
-
await signUp(email, password, client, clientId);
|
|
401
|
-
|
|
402
|
-
const { verificationCode } = await prompt({
|
|
403
|
-
type: 'input',
|
|
404
|
-
name: 'verificationCode',
|
|
405
|
-
message: 'Enter the verification code sent to your email:',
|
|
406
|
-
validate: (value) => value.length > 0 || 'Please enter a valid code',
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
await verifyEmail(email, verificationCode, client, clientId);
|
|
410
|
-
const tokens = await login(email, password, client, clientId);
|
|
411
|
-
console.log('New tokens generated and stored:', tokens);
|
|
412
|
-
return tokens;
|
|
413
|
-
}
|
|
414
|
-
} catch (error) {
|
|
415
|
-
console.error('Error in process:', error.message);
|
|
416
|
-
throw error;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// Run the script
|
|
421
|
-
main()
|
|
422
|
-
.then((tokens) => console.log('Final tokens:', tokens))
|
|
423
|
-
.catch((err) => console.error('Main execution failed:', err.message));
|
|
424
|
-
|
|
425
|
-
|