From 7baf7cbe21da6bcd40fb0ed503e4aed26d707093 Mon Sep 17 00:00:00 2001 From: Ramon Henrique Ornelas Date: Thu, 28 Sep 2017 22:28:58 -0300 Subject: [PATCH] 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; --- src/lib/storage/up-storage.js | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/lib/storage/up-storage.js b/src/lib/storage/up-storage.js index 57d5b2180..f46fdd767 100644 --- a/src/lib/storage/up-storage.js +++ b/src/lib/storage/up-storage.js @@ -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; }