Skip to content

Commit

Permalink
feat: header authorization uplink
Browse files Browse the repository at this point in the history
- 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;
  • Loading branch information
ramonornela committed Sep 29, 2017
1 parent eca7cf1 commit 7baf7cb
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/lib/storage/up-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 7baf7cb

Please sign in to comment.