1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00

test: fix test and done some improvements in code

This commit is contained in:
Ramon Henrique Ornelas 2017-09-29 11:02:00 -03:00
parent 3f20290ee8
commit cc55c21303

@ -228,44 +228,55 @@ class ProxyStorage {
// registry.npmjs.org will only return search result if user-agent include string 'npm'
headers[userAgent] = headers[userAgent] || `npm (${this.userAgent})`;
if (!this.config.auth) {
return headers;
}
// 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 header Authorization assigns this has precedence
if (copyHeaders['authorization']) {
return headers;
}
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.`);
}
if (typeof this.config.auth !== 'object') {
throw new Error('Auth invalid');
}
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];
}
}
copyHeaders = null;
if (!token) {
throw new Error('Token is required');
}
// 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`);
}
return headers;
}