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

Remove cli-lib/http dep + use local-dev-lib for OAuth #987

Merged
merged 8 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion packages/cli/commands/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ exports.handler = async options => {
process.exit(EXIT_CODES.ERROR);
}

const accountName = updatedConfig.name || validName;
const accountName =
(updatedConfig && updatedConfig.name) || validName || configData.name;

const setAsDefault = await setAsDefaultAccountPrompt(accountName);

Expand Down
4 changes: 4 additions & 0 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,10 @@ en:
setupError: "Failed to setup local dev server: {{ message }}"
startError: "Failed to start local dev server: {{ message }}"
fileChangeError: "Failed to notify local dev server of file change: {{ message }}"
oauth:
logCallbacks:
init: "Updating configuration"
success: "Configuration updated"
projects:
config:
srcOutsideProjectDir: "Invalid value for 'srcDir' in {{ projectConfig }}: {{#bold}}srcDir: \"{{ srcDir }}\"{{/bold}}\n\t'srcDir' must be a relative path to a folder under the project root, such as \".\" or \"./src\""
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/lib/DevServerManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const httpClient = require('@hubspot/cli-lib/http');
const { logger } = require('@hubspot/cli-lib/logger');
const { COMPONENT_TYPES } = require('./projectStructure');
const { i18n } = require('./lang');
Expand Down Expand Up @@ -91,7 +90,6 @@ class DevServerManager {
await serverInterface.start({
accountId,
debug: this.debug,
httpClient,
projectConfig,
requestPorts,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/__tests__/validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { getOauthManager } = require('@hubspot/cli-lib/oauth');
const { getOauthManager } = require('@hubspot/local-dev-lib/oauth');
const {
accessTokenForPersonalAccessKey,
} = require('@hubspot/local-dev-lib/personalAccessKey');
Expand All @@ -10,7 +10,7 @@ const { validateAccount } = require('../validation');
jest.mock('@hubspot/cli-lib');
jest.mock('@hubspot/local-dev-lib/config');
jest.mock('@hubspot/cli-lib/logger');
jest.mock('@hubspot/cli-lib/oauth');
jest.mock('@hubspot/local-dev-lib/oauth');
jest.mock('@hubspot/local-dev-lib/personalAccessKey');
jest.mock('../commonOpts');

Expand Down
42 changes: 26 additions & 16 deletions packages/cli/lib/oauth.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
const express = require('express');
const open = require('open');
const OAuth2Manager = require('@hubspot/cli-lib/lib/models/OAuth2Manager');
const {
default: OAuth2Manager,
} = require('@hubspot/local-dev-lib/models/OAuth2Manager');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { addOauthToAccountConfig } = require('@hubspot/cli-lib/oauth');
const { addOauthToAccountConfig } = require('@hubspot/local-dev-lib/oauth');
const { handleExit } = require('./process');
const { getHubSpotWebsiteOrigin } = require('@hubspot/local-dev-lib/urls');
const { logger } = require('@hubspot/cli-lib/logger');
const { ENVIRONMENTS } = require('@hubspot/cli-lib/lib/constants');
const { buildLogCallbacks } = require('./logCallbacks');

const PORT = 3000;
const redirectUri = `http://localhost:${PORT}/oauth-callback`;

const i18nKey = 'cli.lib.oauth';

const oauthLogCallbacks = buildLogCallbacks({
init: `${i18nKey}.logCallbacks.init`,
success: {
key: `${i18nKey}.logCallbacks.success`,
logger: logger.success,
},
});

const buildAuthUrl = oauthManager => {
return (
`${getHubSpotWebsiteOrigin(oauthManager.env)}/oauth/${
oauthManager.accountId
`${getHubSpotWebsiteOrigin(oauthManager.account.env)}/oauth/${
oauthManager.account.accountId
}/authorize` +
`?client_id=${encodeURIComponent(oauthManager.clientId)}` + // app's client ID
`&scope=${encodeURIComponent(oauthManager.scopes.join(' '))}` + // scopes being requested by the app
`?client_id=${encodeURIComponent(oauthManager.account.clientId)}` + // app's client ID
`&scope=${encodeURIComponent(oauthManager.account.scopes.join(' '))}` + // scopes being requested by the app
`&redirect_uri=${encodeURIComponent(redirectUri)}` // where to send the user after the consent page
);
};
Expand All @@ -44,8 +57,8 @@ const authorize = async oauthManager => {
if (req.query.code) {
const authCodeProof = {
grant_type: 'authorization_code',
client_id: oauthManager.clientId,
client_secret: oauthManager.clientSecret,
client_id: oauthManager.account.clientId,
client_secret: oauthManager.account.clientSecret,
redirect_uri: redirectUri,
code: req.query.code,
};
Expand Down Expand Up @@ -84,20 +97,17 @@ const authorize = async oauthManager => {
const setupOauth = accountConfig => {
const accountId = parseInt(accountConfig.portalId, 10);
const config = getAccountConfig(accountId) || {};
return new OAuth2Manager(
{
...accountConfig,
environment: accountConfig.env || config.env || ENVIRONMENTS.PROD,
},
logger
);
return new OAuth2Manager({
...accountConfig,
environment: accountConfig.env || config.env || ENVIRONMENTS.PROD,
});
};

const authenticateWithOauth = async configData => {
const oauthManager = setupOauth(configData);
logger.log('Authorizing');
await authorize(oauthManager);
addOauthToAccountConfig(oauthManager);
addOauthToAccountConfig(oauthManager, oauthLogCallbacks);
};

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
loadConfigFromEnvironment,
} = require('@hubspot/local-dev-lib/config');
const { getAbsoluteFilePath } = require('@hubspot/local-dev-lib/path');
const { getOauthManager } = require('@hubspot/cli-lib/oauth');
const { getOauthManager } = require('@hubspot/local-dev-lib/oauth');
const {
accessTokenForPersonalAccessKey,
} = require('@hubspot/local-dev-lib/personalAccessKey');
Expand Down Expand Up @@ -118,7 +118,7 @@ async function validateAccount(options) {
logger.error(
`The OAuth2 configuration for account ${accountId} is incorrect`
);
logger.error('Run "hscms auth oauth2" to reauthenticate');
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow hscms, this really hasn't been touched in a long time

logger.error('Run "hs auth --type=oauth2" to reauthenticate');
return false;
}

Expand Down
Loading