Skip to content

Commit

Permalink
feat: add web login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed May 3, 2021
1 parent 09e0ad2 commit 6f68a51
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 82 deletions.
Empty file added messages/connect.md
Empty file.
21 changes: 0 additions & 21 deletions messages/messages.json

This file was deleted.

6 changes: 0 additions & 6 deletions messages/org.json

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/core": "^0.5.4",
"@salesforce/core": "^2.20.10"
"@salesforce/core": "3.0.0",
"open": "^8.0.7",
"tslib": "^2"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
Expand Down
58 changes: 58 additions & 0 deletions src/commands/env/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as os from 'os';
import * as open from 'open';

import { Command, Flags } from '@oclif/core';
import { AuthFields, AuthInfo, Logger, OAuth2Options, SfdxError, WebOAuthServer } from '@salesforce/core';

// TODO: add back once md messages are supported
// Messages.importMessagesDirectory(__dirname);
// const messages = Messages.loadMessages('@salesforce/plugin-env', 'connect');

export default class EnvConnect extends Command {
// public static readonly description = messages.getMessage('description');
// public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly description = 'connect to a Salesforce account or environment';
public static readonly examples = ''.split(os.EOL);
public static flags = {
'login-url': Flags.string({
char: 'r',
description: 'the login URL',
default: 'https://login.salesforce.com',
}),
};

public async run(): Promise<AuthFields> {
const { flags } = await this.parse(EnvConnect);
const oauthConfig: OAuth2Options = { loginUrl: flags['login-url'] };

try {
const authInfo = await this.executeLoginFlow(oauthConfig);
const fields = authInfo.getFields(true);
const successMsg = `Successfully authorized ${fields.username} with ID ${fields.orgId}`;
this.log(successMsg);
return fields;
} catch (err) {
const error = err as Error;
Logger.childFromRoot('auth').debug(error);
if (error.name === 'AuthCodeExchangeError') {
const errMsg = `Invalid client credentials. Verify the OAuth client secret and ID. ${error.message}`;
throw new SfdxError(errMsg);
}
throw error;
}
}

private async executeLoginFlow(oauthConfig: OAuth2Options): Promise<AuthInfo> {
const oauthServer = await WebOAuthServer.create({ oauthConfig });
await oauthServer.start();
await open(oauthServer.getAuthorizationUrl(), { wait: false });
return oauthServer.authorizeAndSave();
}
}
34 changes: 0 additions & 34 deletions src/commands/hello/org.ts

This file was deleted.

6 changes: 6 additions & 0 deletions test/commands/env/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
19 changes: 0 additions & 19 deletions test/commands/hello/org.test.ts

This file was deleted.

41 changes: 40 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,27 @@
chalk "^2.4.2"
cli-ux "^4.9.3"

"@salesforce/core@^2.15.2", "@salesforce/core@^2.2.0", "@salesforce/core@^2.20.10", "@salesforce/core@^2.20.3":
"@salesforce/[email protected]":
version "3.0.0"
resolved "https://registry.npmjs.org/@salesforce/core/-/core-3.0.0.tgz#d26e43c35c9711d7fab7caa862fa69048c770d1f"
integrity sha512-Q5gLhlYqrPo+vXAivmWSjneBluo7tgYO6xfJC1IdUnm1OEL9RZBFY8bWXhuoikulP4hczIK13XqDrt2cq/Nvjw==
dependencies:
"@salesforce/bunyan" "^2.0.0"
"@salesforce/kit" "^1.5.0"
"@salesforce/schemas" "^1.0.1"
"@salesforce/ts-types" "^1.0.0"
"@types/graceful-fs" "^4.1.3"
"@types/jsforce" "1.9.23"
"@types/mkdirp" "1.0.0"
debug "^3.1.0"
graceful-fs "^4.2.4"
jsen "0.6.6"
jsforce "^1.10.0"
jsonwebtoken "8.5.0"
mkdirp "1.0.4"
sfdx-faye "^1.0.9"

"@salesforce/core@^2.15.2", "@salesforce/core@^2.2.0", "@salesforce/core@^2.20.3":
version "2.20.10"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-2.20.10.tgz#b2171043ddaac51b464e8ea3659b4f34ce7bbf73"
integrity sha512-ogER5ieNlmHlBt0SfVC0XLHtPtH/WaBAFtsZitHMLV/Zyv0PHwP+5xpSygMmSDzuLZjRooXikH9dFu0z8WEjJw==
Expand Down Expand Up @@ -2030,6 +2050,11 @@ default-require-extensions@^3.0.0:
dependencies:
strip-bom "^4.0.0"

define-lazy-prop@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/define-lazy-prop/-/define-lazy-prop-2.0.0.tgz#3f7ae421129bcaaac9bc74905c98a0009ec9ee7f"
integrity sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og==

define-properties@^1.1.2, define-properties@^1.1.3:
version "1.1.3"
resolved "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1"
Expand Down Expand Up @@ -3443,6 +3468,11 @@ is-docker@^2.0.0:
resolved "https://registry.npmjs.org/is-docker/-/is-docker-2.1.1.tgz#4125a88e44e450d384e09047ede71adc2d144156"
integrity sha512-ZOoqiXfEwtGknTiuDEy8pN2CfE3TxMHprvNer1mXiqwkOT77Rw3YVrUQ52EqAOU3QAWDQ+bQdx7HJzrv7LS2Hw==

is-docker@^2.1.1:
version "2.2.1"
resolved "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz#33eeabe23cfe86f14bde4408a02c0cfb853acdaa"
integrity sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==

is-extendable@^0.1.0, is-extendable@^0.1.1:
version "0.1.1"
resolved "https://registry.npmjs.org/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
Expand Down Expand Up @@ -4678,6 +4708,15 @@ onetime@^5.1.0:
dependencies:
mimic-fn "^2.1.0"

open@^8.0.7:
version "8.0.7"
resolved "https://registry.npmjs.org/open/-/open-8.0.7.tgz#5597eeff14b440f6ff78fb7ced9ede9f69b2122d"
integrity sha512-qoyG0kpdaWVoL5MiwTRQWujSdivwBOgfLadVEdpsZNHOK1+kBvmVtLYdgWr8G4cgBpG9zaxezn6jz6PPdQW5xg==
dependencies:
define-lazy-prop "^2.0.0"
is-docker "^2.1.1"
is-wsl "^2.2.0"

opencollective-postinstall@^2.0.2:
version "2.0.3"
resolved "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz#7a0fff978f6dbfa4d006238fbac98ed4198c3259"
Expand Down

0 comments on commit 6f68a51

Please sign in to comment.