Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sm/acccesstoken works as username #344

Merged
merged 3 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is last || undefined necessary, given getString either returns the property or undefined?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the || undefined suffix looks unnecessary based on how getString works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS complains:
Type 'Optional<string | null>' is not assignable to type 'string | undefined'.
Type 'null' is not assignable to type 'string | undefined'.

going to leave it for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right. getString returns string | null not string | undefined. null !== 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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used in multiple places, so moved to the sfdc utils

// 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
8 changes: 8 additions & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ async function retrieveUserFields(logger: Logger, username: string): Promise<Use
authInfo: await AuthInfo.create({ username }),
});

if (sfdc.matchesAccessToken(username)) {
peternhale marked this conversation as resolved.
Show resolved Hide resolved
logger.debug('received an accessToken for the username. Converting...');
username = (await connection.identity()).username;
logger.debug(`accessToken converted to ${username}`);
} else {
logger.debug('not a accessToken');
}

const fromFields = Object.keys(REQUIRED_FIELDS).map(upperFirst);
peternhale marked this conversation as resolved.
Show resolved Hide resolved
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;
peternhale marked this conversation as resolved.
Show resolved Hide resolved
},
};
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('[email protected]')).to.equal(false);
});
});
});