Skip to content

Commit 517b727

Browse files
committed
Fixes auth
1 parent 5e3b560 commit 517b727

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ export const startPackageServer = (): Promise<string> => {
292292

293293
const {username} = parsedRequest;
294294
const otp = request.headers['npm-otp'];
295-
const user = validLogins[username];
296295

296+
const user = validLogins[username];
297297
if (!user)
298298
return processError(response, 401, `Unauthorized`);
299299

@@ -452,7 +452,7 @@ export const startPackageServer = (): Promise<string> => {
452452
const {authorization} = req.headers;
453453
if (authorization != null) {
454454
if (!validAuthorizations.includes(authorization)) {
455-
sendError(res, 403, `Forbidden`);
455+
sendError(res, 401, `Invalid token`);
456456
return;
457457
}
458458
} else if (needsAuth(parsedRequest)) {

packages/plugin-npm-cli/sources/commands/npm/login.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,17 @@ async function getCredentials(prompt: any) {
103103
};
104104
}
105105

106-
const {username, password} = await prompt([
107-
{
108-
type: `input`,
109-
name: `username`,
110-
message: `Username:`,
111-
validate: (input: string) => validateRequiredInput(input, `Username`),
112-
},
113-
{
114-
type: `password`,
115-
name: `password`,
116-
message: `Password:`,
117-
validate: (input: string) => validateRequiredInput(input, `Password`),
118-
},
119-
]);
106+
const {username, password} = await prompt([{
107+
type: `input`,
108+
name: `username`,
109+
message: `Username:`,
110+
validate: (input: string) => validateRequiredInput(input, `Username`),
111+
}, {
112+
type: `password`,
113+
name: `password`,
114+
message: `Password:`,
115+
validate: (input: string) => validateRequiredInput(input, `Password`),
116+
}]);
120117

121118
return {
122119
name: username,

packages/plugin-npm-cli/sources/commands/npm/publish.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export default class NpmPublishCommand extends BaseCommand {
8282
} catch (error) {
8383
if (error.name !== `HTTPError`) {
8484
throw error;
85-
} else if (error.statusCode !== 404) {
86-
throw new ReportError(MessageName.NETWORK_ERROR, `The remote server answered with HTTP ${error.statusCode} ${error.statusMessage}`);
85+
} else if (error.response.statusCode !== 404) {
86+
throw new ReportError(MessageName.NETWORK_ERROR, `The remote server answered with HTTP ${error.response.statusCode} ${error.response.statusMessage}`);
8787
}
8888
}
8989
}
@@ -109,14 +109,14 @@ export default class NpmPublishCommand extends BaseCommand {
109109
json: true,
110110
});
111111
} catch (error) {
112-
if (error.name === `HTTPError`) {
113-
const message = error.body && error.body.error
114-
? error.body.error
115-
: `The remote server answered with HTTP ${error.statusCode} ${error.statusMessage}`;
112+
if (error.name !== `HTTPError`) {
113+
throw error;
114+
} else {
115+
const message = error.response.body && error.body.response.error
116+
? error.response.body.error
117+
: `The remote server answered with HTTP ${error.response.statusCode} ${error.response.statusMessage}`;
116118

117119
report.reportError(MessageName.NETWORK_ERROR, message);
118-
} else {
119-
throw error;
120120
}
121121
}
122122
});

packages/plugin-npm-cli/sources/commands/npm/whoami.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ export default class NpmWhoamiCommand extends BaseCommand {
5959

6060
report.reportInfo(MessageName.UNNAMED, response.username);
6161
} catch (err) {
62-
if (err.statusCode === 401 || err.statusCode === 403) {
62+
if (err.name !== `HTTPError`) {
63+
throw err;
64+
} else if (err.response.statusCode === 401 || err.response.statusCode === 403) {
6365
report.reportError(MessageName.AUTHENTICATION_INVALID, `Authentication failed - your credentials may have expired`);
6466
} else {
6567
report.reportError(MessageName.AUTHENTICATION_INVALID, err.toString());

packages/plugin-npm/sources/npmHttpUtils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ async function askForOtp() {
133133
}
134134

135135
function isOtpError(error: any) {
136-
try {
137-
const authMethods = error.headers['www-authenticate'].split(/,\s*/).map((s: string) => s.toLowerCase());
136+
if (error.name !== `HTTPError`)
137+
return false;
138138

139+
try {
140+
const authMethods = error.response.headers['www-authenticate'].split(/,\s*/).map((s: string) => s.toLowerCase());
139141
return authMethods.includes('otp');
140142
} catch (e) {
141143
return false;

0 commit comments

Comments
 (0)