mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
feat: header authorization uplink
- define auth type basic or bearer; - assigns the header get process.env var NPM_TOKEN; - assigns the header get process.env by set name; - assigns the header raw token;
This commit is contained in:
parent
eca7cf1a01
commit
7baf7cbe21
1 changed files with 40 additions and 0 deletions
|
@ -227,6 +227,46 @@ class ProxyStorage {
|
|||
headers[acceptEncoding] = headers[acceptEncoding] || 'gzip';
|
||||
// registry.npmjs.org will only return search result if user-agent include string 'npm'
|
||||
headers[userAgent] = headers[userAgent] || `npm (${this.userAgent})`;
|
||||
|
||||
// copy headers to normalize keys
|
||||
let copyHeaders = {};
|
||||
Object.keys(headers).map((value) => {
|
||||
copyHeaders[value.toLowerCase()] = headers[value];
|
||||
});
|
||||
|
||||
if (!copyHeaders['authorization']) {
|
||||
let token = null;
|
||||
// define authorization token to use in the Authorization Header
|
||||
if (this.config.auth.token) {
|
||||
token = this.config.auth.token;
|
||||
} else if (this.config.auth.token_env) {
|
||||
// get NPM_TOKEN http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
|
||||
// or get other variable export in env
|
||||
if (this.config.auth.token_env === true) {
|
||||
token = process.env.NPM_TOKEN;
|
||||
} else {
|
||||
token = process.env[this.config.auth.token_env];
|
||||
}
|
||||
}
|
||||
|
||||
if (token) {
|
||||
// define type Auth allow basic and bearer
|
||||
const type = this.config.auth.type;
|
||||
switch (type.toLowerCase()) {
|
||||
case 'basic':
|
||||
headers['Authorization'] = `Basic ${token}`;
|
||||
break;
|
||||
case 'bearer':
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Auth type '${type}' not allowed.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
copyHeaders = null;
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue