-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathnpmHttpUtils.ts
143 lines (113 loc) Β· 4.23 KB
/
npmHttpUtils.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {Configuration, Ident, httpUtils} from '@berry/core';
import {MessageName, ReportError} from '@berry/core';
import inquirer from 'inquirer';
import {resolve as resolveUrl} from 'url';
import * as npmConfigUtils from './npmConfigUtils';
import {MapLike} from './npmConfigUtils';
export enum AuthType {
NO_AUTH,
BEST_EFFORT,
CONFIGURATION,
ALWAYS_AUTH,
}
type AuthOptions = {
authType?: AuthType,
};
type RegistryOptions = {
ident: Ident,
registry?: void,
} | {
ident?: void,
registry: string;
};
export type Options = httpUtils.Options & AuthOptions & RegistryOptions;
export function getIdentUrl(ident: Ident) {
if (ident.scope) {
return `/@${ident.scope}%2f${ident.name}`;
} else {
return `/${ident.name}`;
}
}
export async function get(path: string, {configuration, headers, ident, authType, registry, ...rest}: Options) {
if (ident)
registry = npmConfigUtils.getScopeRegistry(ident.scope, {configuration});
if (ident && ident.scope && typeof authType === `undefined`)
authType = AuthType.BEST_EFFORT;
if (typeof registry !== `string`)
throw new Error(`Assertion failed: The registry should be a string`);
const auth = getAuthenticationHeader(registry, {authType, configuration});
if (auth)
headers = {...headers, authorization: auth};
return await httpUtils.get(resolveUrl(registry, path), {configuration, headers, ...rest});
}
export async function put(path: string, body: httpUtils.Body, {configuration, headers, ident, authType = AuthType.ALWAYS_AUTH, registry, ...rest}: Options) {
if (ident)
registry = npmConfigUtils.getScopeRegistry(ident.scope, {configuration});
if (typeof registry !== `string`)
throw new Error(`Assertion failed: The registry should be a string`);
const auth = getAuthenticationHeader(registry, {authType, configuration});
if (auth)
headers = {...headers, authorization: auth};
try {
return await httpUtils.put(resolveUrl(registry, path), body, {configuration, headers, ...rest});
} catch (error) {
if (!isOtpError(error))
throw error;
const otp = await askForOtp();
const headersWithOtp = {...headers, ...getOtpHeaders(otp)};
// Retrying request with OTP
return await httpUtils.put(`${registry}${path}`, body, {configuration, headers: headersWithOtp, ...rest});
}
}
function getAuthenticationHeader(registry: string, {authType = AuthType.CONFIGURATION, configuration}: {authType?: AuthType, configuration: Configuration}) {
const registryConfiguration = npmConfigUtils.getRegistryConfiguration(registry, {configuration});
const effectiveConfiguration = registryConfiguration || configuration;
const mustAuthenticate = shouldAuthenticate(effectiveConfiguration, authType);
if (!mustAuthenticate)
return null;
if (effectiveConfiguration.get(`npmAuthToken`))
return `Bearer ${effectiveConfiguration.get(`npmAuthToken`)}`;
if (effectiveConfiguration.get(`npmAuthIdent`))
return `Basic ${effectiveConfiguration.get(`npmAuthIdent`)}`;
if (mustAuthenticate && authType !== AuthType.BEST_EFFORT) {
throw new ReportError(MessageName.AUTHENTICATION_NOT_FOUND ,`No authentication configured for request`);
} else {
return null;
}
}
function shouldAuthenticate(authConfiguration: MapLike, authType: AuthType) {
switch (authType) {
case AuthType.CONFIGURATION:
return authConfiguration.get(`npmAlwaysAuth`);
case AuthType.BEST_EFFORT:
case AuthType.ALWAYS_AUTH:
return true;
case AuthType.NO_AUTH:
return false;
}
}
async function askForOtp() {
if (process.env.TEST_ENV)
return process.env.TEST_NPM_2FA_TOKEN || '';
const prompt = inquirer.createPromptModule();
const {otp} = await prompt({
type: `input`,
name: `otp`,
message: `One-time password:`,
validate: (input: string) => input.length > 0 ? true : `One-time password is required`,
});
return otp as string;
}
function isOtpError(error: any) {
try {
const authMethods = error.headers['www-authenticate'].split(/,\s*/).map((s: string) => s.toLowerCase());
return authMethods.includes('otp');
} catch (e) {
return false;
}
}
function getOtpHeaders(otp: string) {
return {
[`npm-otp`]: otp,
};
}