contensis-cli 1.0.0-beta.12 → 1.0.0-beta.14
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/dist/commands/connect.js +1 -1
- package/dist/commands/connect.js.map +2 -2
- package/dist/commands/create.js +1 -1
- package/dist/commands/create.js.map +2 -2
- package/dist/commands/login.js +1 -1
- package/dist/commands/login.js.map +2 -2
- package/dist/commands/remove.js +1 -1
- package/dist/commands/remove.js.map +2 -2
- package/dist/commands/set.js +2 -2
- package/dist/commands/set.js.map +2 -2
- package/dist/localisation/en-GB.js +1 -1
- package/dist/localisation/en-GB.js.map +2 -2
- package/dist/providers/CredentialProvider.js +0 -2
- package/dist/providers/CredentialProvider.js.map +2 -2
- package/dist/services/ContensisCliService.js +92 -99
- package/dist/services/ContensisCliService.js.map +2 -2
- package/dist/shell.js +10 -3
- package/dist/shell.js.map +2 -2
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +1 -1
- package/src/commands/connect.ts +1 -1
- package/src/commands/create.ts +1 -1
- package/src/commands/login.ts +1 -1
- package/src/commands/remove.ts +1 -1
- package/src/commands/set.ts +2 -2
- package/src/localisation/en-GB.ts +4 -2
- package/src/providers/CredentialProvider.ts +1 -1
- package/src/services/ContensisCliService.ts +120 -131
- package/src/shell.ts +14 -3
- package/src/version.ts +1 -1
|
@@ -208,7 +208,7 @@ class ContensisCli {
|
|
|
208
208
|
session.UpdateEnv(this.env, environment);
|
|
209
209
|
|
|
210
210
|
if (this.env?.lastUserId) {
|
|
211
|
-
await this.ConnectContensis();
|
|
211
|
+
// await this.ConnectContensis();
|
|
212
212
|
await this.PrintProjects();
|
|
213
213
|
} else {
|
|
214
214
|
log.warning(messages.projects.noList());
|
|
@@ -232,19 +232,11 @@ class ContensisCli {
|
|
|
232
232
|
const isGuidId = userId && isUuid(userId);
|
|
233
233
|
|
|
234
234
|
if (currentEnv && userId) {
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
userId,
|
|
238
|
-
alias: currentEnv,
|
|
239
|
-
},
|
|
235
|
+
const credentials = await this.GetCredentials(
|
|
236
|
+
userId,
|
|
240
237
|
env.passwordFallback
|
|
241
|
-
)
|
|
238
|
+
);
|
|
242
239
|
|
|
243
|
-
if (credentialError && !credentials.current) {
|
|
244
|
-
// Log problem with Credential Provider
|
|
245
|
-
log.error(credentialError as any);
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
240
|
const cachedPassword = credentials?.current?.password;
|
|
249
241
|
|
|
250
242
|
if (cachedPassword) {
|
|
@@ -308,36 +300,19 @@ class ContensisCli {
|
|
|
308
300
|
const isTargetGuidId = targetUserId && isUuid(targetUserId);
|
|
309
301
|
|
|
310
302
|
if (sourceUserId && currentEnv && targetUserId) {
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
},
|
|
317
|
-
sourcePassword
|
|
318
|
-
).Init();
|
|
303
|
+
const sourceCredentials = await this.GetCredentials(
|
|
304
|
+
sourceUserId,
|
|
305
|
+
sourcePassword,
|
|
306
|
+
sourceEnv
|
|
307
|
+
);
|
|
319
308
|
|
|
320
|
-
if (sourceCredentialError && !sourceCredentials.current) {
|
|
321
|
-
// Log problem with Credential Provider
|
|
322
|
-
logError(sourceCredentialError);
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
309
|
const cachedSourcePassword = sourceCredentials?.current?.password;
|
|
326
310
|
|
|
327
|
-
const
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
alias: currentEnv,
|
|
332
|
-
},
|
|
333
|
-
env.passwordFallback
|
|
334
|
-
).Init();
|
|
311
|
+
const targetCredentials = await this.GetCredentials(
|
|
312
|
+
targetUserId,
|
|
313
|
+
env.passwordFallback
|
|
314
|
+
);
|
|
335
315
|
|
|
336
|
-
if (targetCredentialError && !targetCredentials.current) {
|
|
337
|
-
// Log problem with Credential Provider
|
|
338
|
-
log.error(targetCredentialError as any);
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
316
|
const cachedTargetPassword = targetCredentials?.current?.password;
|
|
342
317
|
|
|
343
318
|
if (cachedSourcePassword && cachedTargetPassword) {
|
|
@@ -396,6 +371,34 @@ class ContensisCli {
|
|
|
396
371
|
}
|
|
397
372
|
};
|
|
398
373
|
|
|
374
|
+
GetCredentials = async (
|
|
375
|
+
userId: string,
|
|
376
|
+
password?: string,
|
|
377
|
+
currentEnv = this.currentEnv
|
|
378
|
+
): Promise<CredentialProvider | undefined> => {
|
|
379
|
+
const { env, log, messages } = this;
|
|
380
|
+
if (userId) {
|
|
381
|
+
const [credentialError, credentials] = await new CredentialProvider(
|
|
382
|
+
{ userId, alias: currentEnv },
|
|
383
|
+
password
|
|
384
|
+
).Init();
|
|
385
|
+
|
|
386
|
+
if (credentialError && !credentials.current) {
|
|
387
|
+
// Log problem with Credential Provider
|
|
388
|
+
log.error(credentialError as any);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (credentials.remarks.secure !== true) {
|
|
393
|
+
log.warning(messages.login.insecurePassword());
|
|
394
|
+
} else {
|
|
395
|
+
env.passwordFallback = undefined;
|
|
396
|
+
this.session.UpdateEnv(env, currentEnv);
|
|
397
|
+
}
|
|
398
|
+
return credentials;
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
399
402
|
Login = async (
|
|
400
403
|
userId: string,
|
|
401
404
|
{
|
|
@@ -403,115 +406,101 @@ class ContensisCli {
|
|
|
403
406
|
promptPassword = true,
|
|
404
407
|
sharedSecret = isSharedSecret(this.env.passwordFallback),
|
|
405
408
|
silent = false,
|
|
409
|
+
attempt = 1,
|
|
406
410
|
}: {
|
|
407
411
|
password?: string;
|
|
408
412
|
promptPassword?: boolean;
|
|
409
413
|
sharedSecret?: string;
|
|
410
414
|
silent?: boolean;
|
|
411
|
-
|
|
415
|
+
attempt?: number;
|
|
416
|
+
} = {}
|
|
412
417
|
): Promise<string | undefined> => {
|
|
413
|
-
let inputPassword = password;
|
|
418
|
+
let inputPassword = password || sharedSecret;
|
|
414
419
|
const { log, messages } = this;
|
|
415
420
|
|
|
416
421
|
if (userId) {
|
|
417
422
|
const { currentEnv, env } = this;
|
|
418
423
|
|
|
419
424
|
if (currentEnv) {
|
|
420
|
-
const
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
425
|
+
const credentials = await this.GetCredentials(userId, inputPassword);
|
|
426
|
+
|
|
427
|
+
if (credentials) {
|
|
428
|
+
const cachedPassword = isPassword(credentials.current?.password);
|
|
429
|
+
const cachedSecret = isSharedSecret(credentials.current?.password);
|
|
430
|
+
|
|
431
|
+
if (!cachedPassword && !cachedSecret && promptPassword) {
|
|
432
|
+
// Password prompt
|
|
433
|
+
({ inputPassword } = await inquirer.prompt([
|
|
434
|
+
{
|
|
435
|
+
type: 'password',
|
|
436
|
+
message: messages.login.passwordPrompt(currentEnv, userId),
|
|
437
|
+
name: 'inputPassword',
|
|
438
|
+
mask: '*',
|
|
439
|
+
prefix: undefined,
|
|
440
|
+
},
|
|
441
|
+
]));
|
|
442
|
+
}
|
|
437
443
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
type: 'password',
|
|
452
|
-
message: messages.login.passwordPrompt(currentEnv, userId),
|
|
453
|
-
name: 'inputPassword',
|
|
454
|
-
mask: '*',
|
|
455
|
-
prefix: undefined,
|
|
456
|
-
},
|
|
457
|
-
]));
|
|
458
|
-
}
|
|
444
|
+
if (inputPassword || cachedPassword || cachedSecret) {
|
|
445
|
+
const authService = new ContensisAuthService({
|
|
446
|
+
username: userId,
|
|
447
|
+
password: inputPassword || cachedPassword,
|
|
448
|
+
projectId: env?.currentProject || 'website',
|
|
449
|
+
rootUrl: this.urls?.cms || '',
|
|
450
|
+
clientId: userId,
|
|
451
|
+
clientSecret: sharedSecret || cachedSecret,
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const [authError, bearerToken] = await to(
|
|
455
|
+
authService.BearerToken()
|
|
456
|
+
);
|
|
459
457
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
// If the auth error was raised using a cached password
|
|
500
|
-
if (
|
|
501
|
-
(cachedPassword || cachedSecret) &&
|
|
502
|
-
credentials.remarks.secure
|
|
503
|
-
) {
|
|
504
|
-
// Remove any bad stored credential and trigger login prompt again
|
|
505
|
-
await credentials.Delete();
|
|
506
|
-
return await this.Login(userId, { password, sharedSecret });
|
|
507
|
-
} else {
|
|
508
|
-
throw new Error(messages.login.failed(currentEnv, userId));
|
|
458
|
+
// Login successful
|
|
459
|
+
if (bearerToken) {
|
|
460
|
+
// Set env vars
|
|
461
|
+
env.authToken = bearerToken;
|
|
462
|
+
env.lastUserId = userId;
|
|
463
|
+
env.passwordFallback =
|
|
464
|
+
credentials.remarks.secure !== true
|
|
465
|
+
? credentials.current?.password
|
|
466
|
+
: undefined;
|
|
467
|
+
// Persist env before finding projects or doing anything else
|
|
468
|
+
this.session.UpdateEnv(env);
|
|
469
|
+
|
|
470
|
+
if (!silent) {
|
|
471
|
+
Logger.success(messages.login.success(currentEnv, userId));
|
|
472
|
+
await this.PrintProjects();
|
|
473
|
+
}
|
|
474
|
+
if (inputPassword) await credentials.Save(inputPassword);
|
|
475
|
+
if (sharedSecret) await credentials.Save(sharedSecret);
|
|
476
|
+
} else if (authError) {
|
|
477
|
+
Logger.error(authError.toString());
|
|
478
|
+
// Clear env vars
|
|
479
|
+
env.authToken = '';
|
|
480
|
+
env.lastUserId = '';
|
|
481
|
+
env.passwordFallback = undefined;
|
|
482
|
+
// Persist env to remove cleared values
|
|
483
|
+
this.session.UpdateEnv(env);
|
|
484
|
+
|
|
485
|
+
// If the auth error was raised using a cached password
|
|
486
|
+
if (
|
|
487
|
+
(cachedPassword || cachedSecret) &&
|
|
488
|
+
credentials.remarks.secure
|
|
489
|
+
) {
|
|
490
|
+
// Remove any bad stored credential and trigger login prompt again
|
|
491
|
+
await credentials.Delete();
|
|
492
|
+
return await this.Login(userId, { password, sharedSecret });
|
|
493
|
+
} else {
|
|
494
|
+
throw new Error(messages.login.failed(currentEnv, userId));
|
|
495
|
+
}
|
|
509
496
|
}
|
|
510
|
-
}
|
|
511
497
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
498
|
+
return env.authToken;
|
|
499
|
+
} else {
|
|
500
|
+
Logger.error(messages.login.passwordPrompt());
|
|
501
|
+
if (attempt < 2)
|
|
502
|
+
return await this.Login(userId, { attempt: attempt + 1 });
|
|
503
|
+
}
|
|
515
504
|
}
|
|
516
505
|
} else {
|
|
517
506
|
// No environment set, use `contensis connect {alias}` first
|
package/src/shell.ts
CHANGED
|
@@ -72,8 +72,13 @@ class ContensisShell {
|
|
|
72
72
|
);
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
restart = async () => {
|
|
76
|
+
this.firstStart = false;
|
|
77
|
+
this.log.line(); // add a line so we can see where the shell has been restarted
|
|
78
|
+
await this.start();
|
|
79
|
+
};
|
|
80
|
+
|
|
75
81
|
start = async () => {
|
|
76
|
-
this.log.line();
|
|
77
82
|
this.refreshEnvironment();
|
|
78
83
|
this.userId = '';
|
|
79
84
|
const { currentEnvironment, env, log, messages } = this;
|
|
@@ -98,7 +103,10 @@ class ContensisShell {
|
|
|
98
103
|
silent: true,
|
|
99
104
|
}
|
|
100
105
|
);
|
|
101
|
-
if (token)
|
|
106
|
+
if (token) {
|
|
107
|
+
this.userId = env.lastUserId;
|
|
108
|
+
if (!env.currentProject) log.warning(messages.projects.tip());
|
|
109
|
+
}
|
|
102
110
|
this.firstStart = false;
|
|
103
111
|
this.refreshEnvironment();
|
|
104
112
|
} else {
|
|
@@ -221,7 +229,10 @@ class ContensisShell {
|
|
|
221
229
|
let globalShell: ContensisShell;
|
|
222
230
|
|
|
223
231
|
export const shell = () => {
|
|
224
|
-
|
|
232
|
+
// Return a benign function for shell().restart() when used in cli context
|
|
233
|
+
// as some commands need to restart the shell to show an updated prompt
|
|
234
|
+
// after successful connect / login / set project
|
|
235
|
+
if (typeof process.argv?.[2] !== 'undefined') return { restart() {} } as any;
|
|
225
236
|
if (!globalShell) globalShell = new ContensisShell();
|
|
226
237
|
return globalShell;
|
|
227
238
|
};
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const LIB_VERSION = "1.0.0-beta.
|
|
1
|
+
export const LIB_VERSION = "1.0.0-beta.14";
|