Skip to content

Commit

Permalink
feat: untested hubauth
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Feb 10, 2021
1 parent 32cfff4 commit c50e679
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/hubAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AuthInfo, ConfigAggregator, ConfigInfo, fs } from '@salesforce/core';
import { processWrapper } from './processWrapper';

/**
*
* @param hubAlias the alias of the devhub you'd like to use OR the resulting alias of the devhub that'll be authorized if using authUrl/jwt. Defaults to 'nutHub'
* @param setDefault set the authorized hub to be the default. Defaults to false
*/
export const hubAuth = async (hubAlias = 'nutHub', setAsDefault?: boolean): Promise<ConfigInfo | undefined> => {
// do we have a configured hub already? If so, we're good
const aggregator = await ConfigAggregator.create();
const configInfo = aggregator.getInfo('defaultdevhubusername');
if (configInfo) {
return configInfo;
}

// if not, look around the environment
// case 1: authUrl
if (processWrapper.AUTH_URL) {
// auth the hub, set as default with hubAlias
const oauth2Options = AuthInfo.parseSfdxAuthUrl(processWrapper.AUTH_URL);
const authInfo = await AuthInfo.create({ oauth2Options });
await authInfo.save();
// will set to the default.
await authInfo.setAlias(hubAlias);
// export the config
if (setAsDefault) {
await authInfo.setAsDefault({
defaultDevhubUsername: true,
});
}
return aggregator.getInfo('defaultdevhubusername');
}

// case 2: jwt credentials
if (processWrapper.JWT_CLIENT_ID && processWrapper.JWT_KEY && processWrapper.JWT_USERNAME) {
const tmpKeyPath = 'tmpKeyPath';
await fs.writeFile(tmpKeyPath, processWrapper.JWT_KEY);

// TODO: actually auth
await fs.remove(tmpKeyPath);
throw new Error('jwt not implemented yet');
}
};
16 changes: 16 additions & 0 deletions src/processWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { SfdcUrl } from '@salesforce/core';

export const processWrapper = {
AUTH_URL: process.env.SFDX_AUTH_URL,
JWT_KEY: process.env.JWT_KEY,
JWT_USERNAME: process.env.JWT_USERNAME,
JWT_CLIENT_ID: process.env.JWT_CLIENT_ID,
JWT_INSTANCE: process.env.JWT_INSTANCE ?? SfdcUrl.PRODUCTION,
};
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": "../node_modules/@salesforce/dev-config/tsconfig",
"include": ["unit/**/*.ts", "../node_modules/@types/**/*.d.ts"],
"include": ["unit/**/*.ts", "../node_modules/@types/**/*.d.ts", "**/*.nut.ts"],
"compilerOptions": {
"noEmit": true,
"skipLibCheck": true
Expand Down
14 changes: 14 additions & 0 deletions test/unit/hubAuth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

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 jwt in env');
it('auths from jwt with custom hubAlias');
});

0 comments on commit c50e679

Please sign in to comment.