Skip to content

Commit

Permalink
fix: use new sf-plugins-core prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jan 4, 2024
1 parent 3cd0fbf commit d02c83f
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 347 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2023, Salesforce.com, Inc.
Copyright (c) 2024, Salesforce.com, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@inquirer/checkbox": "^1.5.0",
"@inquirer/confirm": "^2.0.15",
"@inquirer/password": "^1.1.14",
"@oclif/core": "^3.15.0",
"@salesforce/core": "^6.4.2",
"@inquirer/select": "^1.3.1",
"@oclif/core": "^3.16.0",
"@salesforce/core": "^6.4.3",
"@salesforce/kit": "^3.0.15",
"@salesforce/sf-plugins-core": "^5.0.13",
"@salesforce/sf-plugins-core": "5.0.14-dev.0",
"@salesforce/ts-types": "^2.0.9",
"chalk": "^5.3.0",
"open": "^9.1.0"
},
"devDependencies": {
"@oclif/plugin-command-snapshot": "^5.0.3",
"@salesforce/cli-plugins-testkit": "^5.1.3",
"@salesforce/dev-scripts": "^8.1.3",
"@salesforce/dev-scripts": "^8.2.0",
"@salesforce/plugin-command-reference": "^3.0.58",
"@salesforce/ts-sinon": "^1.4.19",
"eslint-plugin-sf-plugin": "^1.17.0",
Expand Down
10 changes: 3 additions & 7 deletions src/commands/org/list/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/



import { loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { AuthInfo, Messages, OrgAuthorization } from '@salesforce/core';
type AuthListResult = Omit<OrgAuthorization, 'aliases'> & { alias: string };
export type AuthListResults = AuthListResult[];
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'list');

export default class ListAuth extends SfCommand<AuthListResults> {
Expand Down Expand Up @@ -40,16 +38,14 @@ export default class ListAuth extends SfCommand<AuthListResults> {
});

const hasErrors = auths.filter((auth) => !!auth.error).length > 0;
let columns = {
const columns = {
alias: { header: 'ALIAS' },
username: { header: 'USERNAME' },
orgId: { header: 'ORG ID' },
instanceUrl: { header: 'INSTANCE URL' },
oauthMethod: { header: 'AUTH METHOD' },
...(hasErrors ? { error: { header: 'ERROR' } } : {}),
};
if (hasErrors) {
columns = { ...columns, ...{ error: { header: 'ERROR' } } };
}
this.styledHeader('authenticated orgs');
this.table(mappedAuths, columns);
return mappedAuths;
Expand Down
30 changes: 16 additions & 14 deletions src/commands/org/login/access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import { Flags, loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { AuthFields, AuthInfo, Messages, matchesAccessToken, SfError, StateAggregator } from '@salesforce/core';
import { env } from '@salesforce/kit';
import { Interfaces } from '@oclif/core';
import common from '../../../common.js';
import { InferredFlags } from '@oclif/core/lib/interfaces';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'accesstoken.store');
Expand Down Expand Up @@ -64,13 +63,13 @@ export default class LoginAccessToken extends SfCommand<AuthFields> {
loglevel,
};

private flags!: Interfaces.InferredFlags<typeof LoginAccessToken.flags>;
private flags!: InferredFlags<typeof LoginAccessToken.flags>;

public async run(): Promise<AuthFields> {
const { flags } = await this.parse(LoginAccessToken);
this.flags = flags;
const instanceUrl = flags['instance-url'].href;
const accessToken = await getAccessToken();
const accessToken = await this.getAccessToken();
const authInfo = await this.getUserInfo(accessToken, instanceUrl);
return this.storeAuthFromAccessToken(authInfo);
}
Expand Down Expand Up @@ -107,19 +106,22 @@ export default class LoginAccessToken extends SfCommand<AuthFields> {
if (!this.flags['no-prompt']) {
const stateAggregator = await StateAggregator.getInstance();
if (await stateAggregator.orgs.exists(username)) {
return this.confirm(messages.getMessage('overwriteAccessTokenAuthUserFile', [username]));
return this.confirm({ message: messages.getMessage('overwriteAccessTokenAuthUserFile', [username]) });
}
}
return true;
}
}

const getAccessToken = async (): Promise<string> => {
const accessToken =
env.getString('SF_ACCESS_TOKEN') ?? env.getString('SFDX_ACCESS_TOKEN') ?? (await common.accessTokenPrompt());

if (!matchesAccessToken(accessToken)) {
throw new SfError(messages.getMessage('invalidAccessTokenFormat', [ACCESS_TOKEN_FORMAT]));
private async getAccessToken(): Promise<string> {
const accessToken =
env.getString('SF_ACCESS_TOKEN') ??
env.getString('SFDX_ACCESS_TOKEN') ??
(this.flags['no-prompt'] === true
? '' // will throw when validating
: await this.secretPrompt({ message: commonMessages.getMessage('accessTokenStdin') }));
if (!matchesAccessToken(accessToken)) {
throw new SfError(messages.getMessage('invalidAccessTokenFormat', [ACCESS_TOKEN_FORMAT]));
}
return accessToken;
}
return accessToken;
};
}
18 changes: 8 additions & 10 deletions src/commands/org/login/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import open, { apps, AppName } from 'open';
import { Flags, SfCommand, loglevel } from '@salesforce/sf-plugins-core';
import { AuthFields, AuthInfo, Logger, Messages, OAuth2Config, SfError, WebOAuthServer } from '@salesforce/core';
import { Env } from '@salesforce/kit';
import { Interfaces } from '@oclif/core';
import common from '../../../common.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
Expand All @@ -24,12 +23,12 @@ export default class LoginWeb extends SfCommand<AuthFields> {
public static readonly aliases = ['force:auth:web:login', 'auth:web:login'];

public static readonly flags = {
browser: Flags.string({
browser: Flags.option({
char: 'b',
summary: messages.getMessage('flags.browser.summary'),
description: messages.getMessage('flags.browser.description'),
options: ['chrome', 'edge', 'firefox'], // These are ones supported by "open" package
}),
})(),
'client-id': Flags.string({
char: 'i',
summary: commonMessages.getMessage('flags.client-id.summary'),
Expand Down Expand Up @@ -72,11 +71,8 @@ export default class LoginWeb extends SfCommand<AuthFields> {
loglevel,
};

private flags!: Interfaces.InferredFlags<typeof LoginWeb.flags>;

public async run(): Promise<AuthFields> {
const { flags } = await this.parse(LoginWeb);
this.flags = flags;
if (isSFDXContainerMode()) {
throw new SfError(messages.getMessage('deviceWarning'), 'DEVICE_WARNING');
}
Expand All @@ -86,11 +82,13 @@ export default class LoginWeb extends SfCommand<AuthFields> {
const oauthConfig: OAuth2Config = {
loginUrl: await common.resolveLoginUrl(flags['instance-url']?.href),
clientId: flags['client-id'],
...(flags['client-id'] ? { clientSecret: await common.clientSecretPrompt() } : {}),
...(flags['client-id']
? { clientSecret: await this.secretPrompt({ message: commonMessages.getMessage('clientSecretStdin') }) }
: {}),
};

try {
const authInfo = await this.executeLoginFlow(oauthConfig);
const authInfo = await this.executeLoginFlow(oauthConfig, flags.browser);
await authInfo.handleAliasAndDefaultSettings({
alias: flags.alias,
setDefault: flags['set-default'],
Expand All @@ -113,10 +111,10 @@ export default class LoginWeb extends SfCommand<AuthFields> {

// leave it because it's stubbed in the test
// eslint-disable-next-line class-methods-use-this
private async executeLoginFlow(oauthConfig: OAuth2Config): Promise<AuthInfo> {
private async executeLoginFlow(oauthConfig: OAuth2Config, browser?: string): Promise<AuthInfo> {
const oauthServer = await WebOAuthServer.create({ oauthConfig });
await oauthServer.start();
const app = this.flags.browser && this.flags.browser in apps ? (this.flags.browser as AppName) : undefined;
const app = browser && browser in apps ? (browser as AppName) : undefined;
const openOptions = app ? { app: { name: apps[app] }, wait: false } : { wait: false };
await open(oauthServer.getAuthorizationUrl(), openOptions);
return oauthServer.authorizeAndSave();
Expand Down
Loading

0 comments on commit d02c83f

Please sign in to comment.