Skip to content

Commit 2fd21f1

Browse files
committed
fixup! feat(ncu-config): add support for partially encrypted config files
lazy auth
1 parent 724ae84 commit 2fd21f1

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

lib/auth.js

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,70 @@ function encode(name, token) {
6060
return Buffer.from(`${name}:${token}`).toString('base64');
6161
}
6262

63+
function setOwnProperty(target, key, value) {
64+
return Object.defineProperty(target, key, {
65+
__proto__: null,
66+
configurable: true,
67+
enumerable: true,
68+
value
69+
});
70+
}
71+
6372
// TODO: support jenkins only...or not necessary?
6473
// TODO: make this a class with dependency (CLI) injectable for testing
6574
async function auth(
6675
options = { github: true },
6776
githubAuth = ghauth) {
68-
const result = {};
69-
if (options.github) {
70-
let username;
71-
let token;
72-
try {
73-
({ username, token } = getMergedConfig());
74-
} catch (e) {
75-
// Ignore error and prompt
76-
}
77+
const result = {
78+
get github() {
79+
let username;
80+
let token;
81+
try {
82+
({ username, token } = getMergedConfig());
83+
} catch (e) {
84+
// Ignore error and prompt
85+
}
86+
87+
check(username, token);
88+
const github = encode(username, token);
89+
setOwnProperty(result, 'github', github);
90+
return github;
91+
},
7792

78-
if (!username || !token) {
93+
get jenkins() {
94+
const { username, jenkins_token } = getMergedConfig();
95+
if (!username || !jenkins_token) {
96+
errorExit(
97+
'Get your Jenkins API token in https://ci.nodejs.org/me/security ' +
98+
'and run the following command to add it to your ncu config: ' +
99+
'ncu-config --global set -x jenkins_token'
100+
);
101+
};
102+
check(username, jenkins_token);
103+
const jenkins = encode(username, jenkins_token);
104+
setOwnProperty(result, 'jenkins', jenkins);
105+
return jenkins;
106+
},
107+
108+
get h1() {
109+
const { h1_username, h1_token } = getMergedConfig();
110+
check(h1_username, h1_token);
111+
const h1 = encode(h1_username, h1_token);
112+
setOwnProperty(result, 'h1', h1);
113+
return h1;
114+
}
115+
};
116+
if (options.github) {
117+
const config = getMergedConfig();
118+
if (!Object.hasOwn(config, 'token') || !Object.hasOwn(config, 'username')) {
79119
process.stdout.write(
80120
'If this is your first time running this command, ' +
81121
'follow the instructions to create an access token' +
82122
'. If you prefer to create it yourself on Github, ' +
83123
'see https://github.com/nodejs/node-core-utils/blob/main/README.md.\n');
84124
const credentials = await tryCreateGitHubToken(githubAuth);
85-
username = credentials.user;
125+
const username = credentials.user;
126+
let token;
86127
try {
87128
token = await encryptValue(credentials.token);
88129
} catch (err) {
@@ -95,38 +136,9 @@ async function auth(
95136
});
96137
// Try again reading the file
97138
clearCachedConfig();
98-
({ username, token } = getMergedConfig());
99139
}
100-
check(username, token);
101-
result.github = encode(username, token);
102140
}
103141

104-
if (options.jenkins) {
105-
const { username, jenkins_token } = getMergedConfig();
106-
if (!username || !jenkins_token) {
107-
errorExit(
108-
'Get your Jenkins API token in https://ci.nodejs.org/me/security ' +
109-
'and run the following command to add it to your ncu config: ' +
110-
'ncu-config --global set -x jenkins_token'
111-
);
112-
};
113-
check(username, jenkins_token);
114-
result.jenkins = encode(username, jenkins_token);
115-
}
116-
117-
if (options.h1) {
118-
const { h1_username, h1_token } = getMergedConfig();
119-
if (!h1_username || !h1_token) {
120-
errorExit(
121-
'Get your HackerOne API token in ' +
122-
'https://docs.hackerone.com/organizations/api-tokens.html ' +
123-
'and run the following command to add it to your ncu config: ' +
124-
'ncu-config --global set -x h1_token or ' +
125-
'ncu-config --global set h1_username USERNAME'
126-
);
127-
};
128-
result.h1 = encode(h1_username, h1_token);
129-
}
130142
return result;
131143
}
132144

0 commit comments

Comments
 (0)