Skip to content

Commit 2c953de

Browse files
committed
Use auth tokens for NPM registry requests
The NPM registry and NPM compatible registries support privately hosted modules. To access meta data for these modules and the modules' tarballs themselves, bearer tokens can be used. To support this, NPM's `_auth` property need to be raed from the config and this option must be used for meta data and tarball requests as the bearer token.
1 parent 0829a74 commit 2c953de

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

src/fetchers/tarball-fetcher.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,18 @@ export default class TarballFetcher extends BaseFetcher {
131131
fetchFromExternal(): Promise<FetchedOverride> {
132132
const {reference: ref} = this;
133133

134+
const headers = {
135+
'Accept-Encoding': 'gzip',
136+
'Accept': 'application/octet-stream',
137+
};
138+
139+
if (this.remote.auth && this.remote.auth.token) {
140+
headers.authorization = `Bearer ${this.remote.auth.token}`;
141+
}
142+
134143
return this.config.requestManager.request({
135144
url: ref,
136-
headers: {
137-
'Accept-Encoding': 'gzip',
138-
'Accept': 'application/octet-stream',
139-
},
145+
headers,
140146
buffer: true,
141147
process: (req, resolve, reject) => {
142148
// should we save this to the offline cache?

src/registries/npm-registry.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export default class NpmRegistry extends Registry {
126126
await fs.mkdirp(mirrorLoc);
127127
}
128128

129+
if (config['_auth']) {
130+
this.token = config['_auth'];
131+
}
132+
129133
defaults(this.config, config);
130134
}
131135
}

src/resolvers/registries/npm-resolver.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ export default class NpmResolver extends RegistryResolver {
162162
reference: dist.tarball,
163163
hash: dist.shasum,
164164
registry: 'npm',
165+
auth: {
166+
token: this.config.registries.npm.token,
167+
},
165168
};
166169
}
167170

src/types.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export type PackageRemote = {
4444
reference: string,
4545
resolved?: ?string,
4646
hash?: ?string,
47+
auth?: {
48+
email?: string,
49+
username?: string,
50+
password?: string,
51+
token?: string,
52+
}
4753
};
4854

4955
// `dependencies` field in package info

0 commit comments

Comments
 (0)