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

feat: add access token hub auth strategy #90

Merged
merged 3 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 36 additions & 1 deletion src/hubAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { env } from '@salesforce/kit';
export enum AuthStrategy {
JWT = 'JWT',
AUTH_URL = 'AUTH_URL',
ACCESS_TOKEN = 'ACCESS_TOKEN',
REUSE = 'REUSE',
NONE = 'NONE',
}
Expand Down Expand Up @@ -57,14 +58,20 @@ export const prepareForJwt = (homeDir: string): string => {
return jwtKey;
};

export const prepareForAccessToken = (homeDir: string): string => {
const accessTokenFile = path.join(homeDir, 'accessTokenFile');
fs.writeFileSync(accessTokenFile, env.getString('TESTKIT_AUTH_ACCESS_TOKEN', ''));
return accessTokenFile;
};

export const prepareForAuthUrl = (homeDir: string): string => {
const tmpUrl = path.join(homeDir, 'tmpUrl');
fs.writeFileSync(tmpUrl, env.getString('TESTKIT_AUTH_URL', ''));
return tmpUrl;
};

/**
* Inspects the environment (via AuthStrategy) and authenticates to a devhub via JWT or AuthUrl
* Inspects the environment (via AuthStrategy) and authenticates to a devhub via JWT, AuthUrl or AccessToken
* Sets the hub as default for use in tests
*
* @param homeDir the testSession directory where credential files will be written
Expand All @@ -74,6 +81,7 @@ export const prepareForAuthUrl = (homeDir: string): string => {
* for jwt: TESTKIT_HUB_USERNAME, TESTKIT_JWT_CLIENT_ID, TESTKIT_JWT_KEY
* optional but recommended: TESTKIT_HUB_INSTANCE
* required for AuthUrl: TESTKIT_AUTH_URL
* required for AccessToken: TESTKIT_AUTH_ACCESS_TOKEN, TESTKIT_HUB_INSTANCE
*/
export const testkitHubAuth = (homeDir: string, authStrategy: AuthStrategy = getAuthStrategy()): void => {
const logger = debug('testkit:authFromStubbedHome');
Expand Down Expand Up @@ -104,6 +112,7 @@ export const testkitHubAuth = (homeDir: string, authStrategy: AuthStrategy = get
}
return;
}

if (authStrategy === AuthStrategy.AUTH_URL) {
logger('trying to authenticate with AuthUrl');

Expand All @@ -121,6 +130,29 @@ export const testkitHubAuth = (homeDir: string, authStrategy: AuthStrategy = get

return;
}

shetzel marked this conversation as resolved.
Show resolved Hide resolved
if (authStrategy === AuthStrategy.ACCESS_TOKEN) {
logger('trying to authenticate with Access Token');

const accessTokenFile = prepareForAccessToken(homeDir);

const shellOutput = shell.exec(
`sfdx auth:accesstoken:store --noprompt -d -f ${accessTokenFile} -r ${env.getString(
'TESTKIT_HUB_INSTANCE',
DEFAULT_INSTANCE_URL
)}`,
execOpts
) as shell.ShellString;
logger(shellOutput);
if (shellOutput.code !== 0) {
throw new Error(
`auth:sfdxurl for url ${accessTokenFile} failed with exit code: ${shellOutput.code}\n ${
shellOutput.stdout + shellOutput.stderr
}`
);
}
return;
}
logger('no hub configured');
};

Expand All @@ -135,6 +167,9 @@ const getAuthStrategy = (): AuthStrategy => {
if (env.getString('TESTKIT_AUTH_URL')) {
return AuthStrategy.AUTH_URL;
}
if (env.getString('TESTKIT_AUTH_ACCESS_TOKEN') && env.getString('TESTKIT_HUB_INSTANCE')) {
return AuthStrategy.ACCESS_TOKEN;
}
// none of the above are included, so we want to reuse an already authenticated hub
if (env.getString('TESTKIT_HUB_USERNAME')) {
return AuthStrategy.REUSE;
Expand Down
1 change: 1 addition & 0 deletions src/testSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface TestSessionOptions {
* TESTKIT_JWT_KEY = JWT key (not a filepath, the actual contents of the key)
* TESTKIT_HUB_INSTANCE = instance url for the hub. Defaults to https://login.salesforce.com
* TESTKIT_AUTH_URL = auth url to be used with auth:sfdxurl:store
* TESTKIT_AUTH_ACCESS_TOKEN = access token to be used with auth:accesstoken:store
*/
export class TestSession extends AsyncOptionalCreatable<TestSessionOptions> {
public id: string;
Expand Down
2 changes: 2 additions & 0 deletions test/unit/hubAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('hubAuth', () => {
it('works with existing defaultdevhub if provided');
it('auths from authurl in env');
it('auths from authurl and sets as default');
it('auths from access token and sets as default');
it('auths from access token');
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we going to ignore these unit tests forever? Even having NUTs for this would be something. Right now there's nothing.

it('auths from jwt in env');
it('auths from jwt with custom hubAlias');
});