Skip to content

Commit 2d63536

Browse files
plumpNationGavin Kingaduh95
authored
fix: Incorrect authorization prefix for basic auth, and undocumented env var (#454)
Co-authored-by: Gavin King <gavin.king@cgi.com> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 73d9a1e commit 2d63536

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

sources/httpUtils.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,25 @@ async function fetch(input: string | URL, init?: RequestInit) {
1616
input = new URL(input);
1717

1818
let headers = init?.headers;
19-
const {username, password} = input;
19+
20+
const username: string | undefined = input.username ?? process.env.COREPACK_NPM_USERNAME;
21+
const password: string | undefined = input.password ?? process.env.COREPACK_NPM_PASSWORD;
22+
2023
if (username || password) {
2124
headers = {
2225
...headers,
23-
authorization: `Bearer ${Buffer.from(`${username}:${password}`).toString(`base64`)}`,
26+
authorization: `Basic ${Buffer.from(`${username}:${password}`).toString(`base64`)}`,
2427
};
28+
2529
input.username = input.password = ``;
26-
} else if (input.origin === process.env.COREPACK_NPM_REGISTRY || DEFAULT_NPM_REGISTRY_URL) {
27-
if (process.env.COREPACK_NPM_TOKEN) {
28-
headers = {
29-
...headers,
30-
authorization: `Bearer ${process.env.COREPACK_NPM_TOKEN}`,
31-
};
32-
} else if (`COREPACK_NPM_PASSWORD` in process.env) {
33-
headers = {
34-
...headers,
35-
authorization: `Bearer ${Buffer.from(`${process.env.COREPACK_NPM_USER}:${process.env.COREPACK_NPM_PASSWORD}`).toString(`base64`)}`,
36-
};
37-
}
3830
}
3931

32+
if (input.origin === (process.env.COREPACK_NPM_REGISTRY || DEFAULT_NPM_REGISTRY_URL) && process.env.COREPACK_NPM_TOKEN) {
33+
headers = {
34+
...headers,
35+
authorization: `Bearer ${process.env.COREPACK_NPM_TOKEN}`,
36+
};
37+
}
4038

4139
let response;
4240
try {

tests/_registryServer.mjs

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ function generateVersionMetadata(packageName, version) {
6767

6868
const server = createServer((req, res) => {
6969
const auth = req.headers.authorization;
70-
if (!auth?.startsWith(`Bearer `) || Buffer.from(auth.slice(`Bearer `.length), `base64`).toString() !== `user:pass`) {
70+
71+
if (auth?.startsWith(`Basic `) && Buffer.from(auth.slice(`Basic `.length), `base64`).toString() !== `user:pass`) {
7172
res.writeHead(401).end(`Unauthorized`);
7273
return;
7374
}
@@ -163,7 +164,7 @@ switch (process.env.AUTH_TYPE) {
163164

164165
case `COREPACK_NPM_PASSWORD`:
165166
process.env.COREPACK_NPM_REGISTRY = `http://${address.includes(`:`) ? `[${address}]` : address}:${port}`;
166-
process.env.COREPACK_NPM_USER = `user`;
167+
process.env.COREPACK_NPM_USERNAME = `user`;
167168
process.env.COREPACK_NPM_PASSWORD = `pass`;
168169
break;
169170

0 commit comments

Comments
 (0)