Skip to content

Commit

Permalink
feat: help with internal lighting domain issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed May 31, 2023
1 parent bc4b9c7 commit 8617e5b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/commands/org/login/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { OAuth2Config } from 'jsforce';
import { AuthFields, AuthInfo, DeviceOauthService, Messages } from '@salesforce/core';
import { get, Optional } from '@salesforce/ts-types';
import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { DeviceCodeResponse } from '@salesforce/core/lib/deviceOauthService';
import { ux } from '@oclif/core';
Expand Down Expand Up @@ -72,7 +71,7 @@ export default class LoginDevice extends AuthBaseCommand<DeviceLoginResult> {
if (await this.shouldExitCommand(false)) return {};

const oauthConfig: OAuth2Config = {
loginUrl: await Common.resolveLoginUrl(get(flags['instance-url'], 'href', null) as Optional<string>),
loginUrl: await Common.resolveLoginUrl(flags['instance-url']?.href),
clientId: flags['client-id'],
};

Expand Down
7 changes: 4 additions & 3 deletions src/commands/org/login/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { AuthFields, AuthInfo, AuthRemover, Logger, Messages, SfError } from '@salesforce/core';
import { getString } from '@salesforce/ts-types';
import { Interfaces } from '@oclif/core';
import { AuthBaseCommand } from '../../../authBaseCommand';
import { Common } from '../../../common';
Expand Down Expand Up @@ -99,8 +98,10 @@ export default class LoginJwt extends AuthBaseCommand<AuthFields> {
result = authInfo.getFields(true);
await AuthInfo.identifyPossibleScratchOrgs(result, authInfo);
} catch (err) {
const msg = getString(err, 'message');
throw messages.createError('JwtGrantError', [msg]);
if (!(err instanceof Error)) {
throw err;
}
throw messages.createError('JwtGrantError', [err.message]);
}

const successMsg = commonMessages.getMessage('authorizeCommandSuccess', [result.username, result.orgId]);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/org/login/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { OAuth2Config } from 'jsforce';
import { AuthFields, AuthInfo, Logger, Messages, SfError, WebOAuthServer } from '@salesforce/core';
import { Env } from '@salesforce/kit';
import { get, Optional } from '@salesforce/ts-types';
import { Interfaces } from '@oclif/core';
import { AuthBaseCommand } from '../../../authBaseCommand';
import { Common } from '../../../common';
Expand Down Expand Up @@ -94,7 +93,7 @@ export default class LoginWeb extends AuthBaseCommand<AuthFields> {
if (await this.shouldExitCommand(flags['no-prompt'])) return {};

const oauthConfig: OAuth2Config = {
loginUrl: await Common.resolveLoginUrl(get(flags['instance-url'], 'href', null) as Optional<string>),
loginUrl: await Common.resolveLoginUrl(flags['instance-url']?.href),
clientId: flags['client-id'],
};

Expand Down
10 changes: 4 additions & 6 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Logger, SfdcUrl, SfProject, Messages, SfError } from '@salesforce/core';
import { getString, isObject, Optional } from '@salesforce/ts-types';
import { getString, isObject } from '@salesforce/ts-types';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');

export class Common {
public static async resolveLoginUrl(instanceUrl: Optional<string>): Promise<Optional<string>> {
public static async resolveLoginUrl(instanceUrl?: string): Promise<string> {
const logger = await Logger.child('Common', { tag: 'resolveLoginUrl' });
if (instanceUrl) {
if (instanceUrl.includes('lightning.force.com')) {
logger.warn(messages.getMessage('invalidInstanceUrl'));
if (instanceUrl.match(/lightning\..*force\.com/)) {
throw new SfError(messages.getMessage('invalidInstanceUrl'), 'URL_WARNING');
}
return instanceUrl;
Expand All @@ -30,8 +29,7 @@ export class Common {
logger.debug(`error occurred while trying to determine loginUrl: ${message}`);
loginUrl = SfdcUrl.PRODUCTION;
}
if (loginUrl.includes('lightning.force.com')) {
logger.warn(messages.getMessage('invalidInstanceUrl'));
if (loginUrl.match(/lightning\..*force\.com/)) {
throw new SfError(messages.getMessage('invalidInstanceUrl'), 'URL_WARNING');
}
logger.debug(`loginUrl: ${loginUrl}`);
Expand Down
23 changes: 17 additions & 6 deletions test/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { ConfigContents, SfdcUrl, SfError } from '@salesforce/core';
import { expect } from 'chai';
import { ConfigContents, SfdcUrl } from '@salesforce/core';
import { assert, expect } from 'chai';
import { TestContext, uniqid } from '@salesforce/core/lib/testSetup';
import { Common } from '../src/common';

Expand Down Expand Up @@ -76,8 +76,8 @@ describe('common unit tests', () => {
await Common.resolveLoginUrl(undefined);
expect.fail('This test is failing because it is expecting an error that is never thrown');
} catch (error) {
const err = error as SfError;
expect(err.name).to.equal('URL_WARNING');
assert(error instanceof Error);
expect(error.name).to.equal('URL_WARNING');
}
});
it('should throw on lightning login URL passed in to resolveLoginUrl()', async () => {
Expand All @@ -86,8 +86,19 @@ describe('common unit tests', () => {
await Common.resolveLoginUrl('https://shanedevhub.lightning.force.com');
expect.fail('This test is failing because it is expecting an error that is never thrown');
} catch (error) {
const err = error as SfError;
expect(err.name).to.equal('URL_WARNING');
assert(error instanceof Error);
expect(error.name).to.equal('URL_WARNING');
}
});

it('should throw on internal lightning login URL passed in to resolveLoginUrl()', async () => {
await projectSetup($$, true);
try {
await Common.resolveLoginUrl('https://dro000000osjp2a0.test1.lightning.pc-rnd.force.com/');
expect.fail('This test is failing because it is expecting an error that is never thrown');
} catch (error) {
assert(error instanceof Error);
expect(error.name).to.equal('URL_WARNING');
}
});
});
Expand Down

0 comments on commit 8617e5b

Please sign in to comment.