-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth.js
73 lines (63 loc) · 1.72 KB
/
oauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const {
updateAccountConfig,
writeConfig,
} = require('@hubspot/local-dev-lib/config');
const OAuth2Manager = require('./lib/models/OAuth2Manager');
const { logger } = require('./logger');
const { logErrorInstance } = require('./errorHandlers/standardErrors');
const { AUTH_METHODS } = require('./lib/constants');
const oauthManagers = new Map();
const writeOauthTokenInfo = (AccountConfig, tokenInfo) => {
const { portalId, authType, auth, env, name, apiKey } = AccountConfig;
logger.debug(`Updating Oauth2 token info for portalId: ${portalId}`);
updateAccountConfig({
name,
apiKey,
environment: env,
portalId,
authType,
...auth,
tokenInfo,
});
writeConfig();
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const getOauthManager = (accountId, accountConfig) => {
if (!oauthManagers.has(accountId)) {
const writeTokenInfo = tokenInfo => {
writeOauthTokenInfo(accountConfig, tokenInfo);
};
oauthManagers.set(
accountId,
OAuth2Manager.fromConfig(accountId, accountConfig, logger, writeTokenInfo)
);
}
return oauthManagers.get(accountId);
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const addOauthToAccountConfig = oauth => {
logger.log('Updating configuration');
try {
updateAccountConfig({
...oauth.toObj(),
authType: AUTH_METHODS.oauth.value,
portalId: oauth.accountId,
});
writeConfig();
logger.log('Configuration updated');
} catch (err) {
logErrorInstance(err);
}
};
module.exports = {
getOauthManager,
addOauthToAccountConfig,
};