Skip to content

Commit

Permalink
fix: update to allow stored connected app info
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Jan 29, 2019
1 parent 28de75c commit 66ea057
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,16 @@ function _parseIdUrl(idUrl: string) {
};
}

// Legacy. The connected app info is owned by the thing that
// creates new AuthInfos. Currently that is the auth:* commands which
// aren't owned by this core library. These values need to be here
// for any old auth files where the id and secret aren't stored.
//
// Ideally, this would be removed at some point in the distant future
// when all auth files now have the clientId stored in it.
const DEFAULT_CONNECTED_APP_INFO = {
clientId: 'SalesforceDevelopmentExperience',
clientSecret: '1384510088588713504'
legacyClientId: 'SalesforceDevelopmentExperience',
legacyClientSecret: '1384510088588713504'
};

class AuthInfoCrypto extends Crypto {
Expand Down Expand Up @@ -436,12 +443,6 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {

const dataToSave = cloneJson(this.fields);

// Do not persist the default client ID and secret
if (dataToSave.clientId === DEFAULT_CONNECTED_APP_INFO.clientId) {
delete dataToSave.clientId;
delete dataToSave.clientSecret;
}

this.logger.debug(dataToSave);

const config = await AuthInfoConfig.create({
Expand Down Expand Up @@ -505,7 +506,7 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {
opts = {
oauth2: {
loginUrl: instanceUrl || 'https://login.salesforce.com',
clientId: this.fields.clientId || DEFAULT_CONNECTED_APP_INFO.clientId,
clientId: this.fields.clientId || DEFAULT_CONNECTED_APP_INFO.legacyClientId,
redirectUri: 'http://localhost:1717/OauthRedirect'
},
accessToken,
Expand Down Expand Up @@ -746,8 +747,11 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {

// Build OAuth config for a refresh token auth flow
private async buildRefreshTokenConfig(options: OAuth2Options): Promise<AuthFields> {
// Ideally, this would be removed at some point in the distant future when all auth files
// now have the clientId stored in it.
if (!options.clientId) {
Object.assign(options, DEFAULT_CONNECTED_APP_INFO);
options.clientId = DEFAULT_CONNECTED_APP_INFO.legacyClientId;
options.clientSecret = DEFAULT_CONNECTED_APP_INFO.legacyClientSecret;
}

const oauth2 = new OAuth2(options);
Expand Down
7 changes: 6 additions & 1 deletion test/unit/authInfoTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,13 +1010,18 @@ describe('AuthInfo', () => {
const decryptedActualFields = configFileWrite.firstCall.thisValue.toObject();
decryptedActualFields.accessToken = crypto.decrypt(decryptedActualFields.accessToken);
decryptedActualFields.refreshToken = crypto.decrypt(decryptedActualFields.refreshToken);
decryptedActualFields.clientSecret = crypto.decrypt(decryptedActualFields.clientSecret);
const expectedFields = {
accessToken: changedData.accessToken,
instanceUrl: testMetadata.instanceUrl,
username,
orgId: authResponse.id.split('/')[0],
loginUrl: refreshTokenConfig.loginUrl,
refreshToken: refreshTokenConfig.refreshToken
refreshToken: refreshTokenConfig.refreshToken,
// clientId and clientSecret are now stored in the file, even the defaults.
// We just hard code the legacy values here to ensure old auth files will still work.
clientId: 'SalesforceDevelopmentExperience',
clientSecret: '1384510088588713504'
};
// Note that this also verifies the clientId and clientSecret are not persisted,
// and that data is encrypted when saved (because we have to decrypt it to verify here).
Expand Down

0 comments on commit 66ea057

Please sign in to comment.