Skip to content

Commit

Permalink
fix: accessToken works as username
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 14, 2020
1 parent 86f3de1 commit 6f0ec24
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { Global } from './global';
import { Logger } from './logger';
import { SfdxError, SfdxErrorConfig } from './sfdxError';
import { fs } from './util/fs';
import { sfdc } from './util/sfdc';

/**
* Fields for authorization, org, and local information.
Expand Down Expand Up @@ -758,8 +759,7 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {
this.fields.username = this.options.username || getString(options, 'username') || undefined;

// If the username is an access token, use that for auth and don't persist
const accessTokenMatch = isString(this.fields.username) && this.fields.username.match(/^(00D\w{12,15})![.\w]*$/);
if (accessTokenMatch) {
if (isString(this.fields.username) && sfdc.matchesAccessToken(this.fields.username)) {
// Need to initAuthOptions the logger and authInfoCrypto since we don't call init()
this.logger = await Logger.child('AuthInfo');
this.authInfoCrypto = await AuthInfoCrypto.create({
Expand All @@ -772,7 +772,7 @@ export class AuthInfo extends AsyncCreatable<AuthInfo.Options> {
this.update({
accessToken: this.options.username,
instanceUrl,
orgId: accessTokenMatch[1],
orgId: this.fields.username.split('!')[0],
});

this.usingAccessToken = true;
Expand Down
4 changes: 4 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ async function retrieveUserFields(logger: Logger, username: string): Promise<Use
authInfo: await AuthInfo.create({ username }),
});

if (sfdc.matchesAccessToken(username)) {
username = (await connection.identity()).username;
}

const fromFields = Object.keys(REQUIRED_FIELDS).map(upperFirst);
const requiredFieldsFromAdminQuery = `SELECT ${fromFields} FROM User WHERE Username='${username}'`;
const result: QueryResult<string[]> = await connection.query<string[]>(requiredFieldsFromAdminQuery);
Expand Down
9 changes: 9 additions & 0 deletions src/util/sfdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,13 @@ export const sfdc = {
});
return key;
},

/**
* Tests whether a given string is an access token
*
* @param value
*/
matchesAccessToken: (value: string): boolean => {
return value.match(/^(00D\w{12,15})![.\w]*$/) !== null;
},
};
13 changes: 13 additions & 0 deletions test/unit/util/sfdcTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,17 @@ describe('util/sfdc', () => {
expect(sfdc.findUpperCaseKeys(testObj, ['nested'])).to.equal(undefined);
});
});

describe('matchesAccessToken', () => {
it('should return true for a valid access token', () => {
expect(
sfdc.matchesAccessToken(
'00D0t000000HkBf!AQ8AQAuHh7lXOFdOA202PMQuGflRrtUkVIfSNK1BrWLlJTJuvypx3r8dLONoJdniYKap1nsTlbxRbbGDqT6r2Rze_Ii5no2y'
)
).to.equal(true);
});
it('should return false for an invalid access token', () => {
expect(sfdc.matchesAccessToken('iamjustaregularusername@example.com')).to.equal(false);
});
});
});

0 comments on commit 6f0ec24

Please sign in to comment.