Skip to content

Commit

Permalink
feat: support authorization (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored and fengmk2 committed Apr 28, 2019
1 parent c41abc0 commit 4464362
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/cnpm_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const path = require('path');
const fs = require('fs');
const config = {};

function createConfigs() {
let root;
if (process.platform === 'win32') {
root = process.env.USERPROFILE || process.env.APPDATA || process.env.TMP || process.env.TEMP;
} else {
root = process.env.HOME || process.env.TMPDIR || '/tmp';
}
const userConfig = path.join(root, '.cnpmrc');
if (!fs.existsSync(userConfig)) return;
const userConfigContent = fs.readFileSync(userConfig).toString();
const configs = typeof userConfigContent === 'string' && userConfigContent.split('\n');
configs.reduce((pre, next) => {
if (typeof next === 'string') {
const map = next.split('=');
const key = map[0];
let value = map[1];
if (value === 'true') value = true;
if (value === 'false') value = false;
pre[key] = value;
}
return pre;
}, config);

}

createConfigs();

module.exports = {
get(key) {
return config[key];
},
};
10 changes: 10 additions & 0 deletions lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const destroy = require('destroy');
const HttpAgent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent;
const utils = require('./utils');
const cnpmConfig = require('./cnpm_config');
const urlParser = require('url');

module.exports = get;

Expand Down Expand Up @@ -41,6 +43,14 @@ function* get(url, options, globalOptions) {
debug('enableProxy: %s', options.proxy);
}
}
// need auth
const registryUrl = cnpmConfig.get('registry');
const registryUri = registryUrl && registryUrl.replace(urlParser.parse(registryUrl).protocol, '') || '';
const authed = registryUri && url.indexOf(registryUri) !== -1 || false;
if (authed || cnpmConfig.get(registryUri + ':always-auth') || cnpmConfig.get('always-auth')) {
const authToken = (`${cnpmConfig.get(registryUri + ':username')}:${Buffer.from(cnpmConfig.get(registryUri + ':_password'), 'base64').toString()}`);
options.headers.Authorization = `Basic ${Buffer.from(authToken).toString('base64')}`;
}
const retry = options.retry || options.retry === 0 ? options.retry : MAX_RETRY;
options.retry = undefined;
debug('GET %s with headers: %j', url, options.headers);
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/auth/.cnpmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
registry=https://registry-mock.org/
//registry-mock.org/:always-auth=true
//registry-mock.org/:_password="bW9jaw=="
//registry-mock.org/:username=hyj19911120
32 changes: 32 additions & 0 deletions test/get.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
'use strict';

const fs = require('fs');
const assert = require('assert');
const mm = require('mm');
const path = require('path');
const mockCnpmrc = path.join(__dirname, './fixtures/auth/');
if (!fs.existsSync(mockCnpmrc + '.cnpmrc')) {
fs.writeFileSync(mockCnpmrc + '.cnpmrc', 'registry=https://registry-mock.org/\n//registry-mock.org/:always-auth=true\n//registry-mock.org/:_password="bW9jaw=="\n//registry-mock.org/:username=hyj19911120');
}
mm(process.env, 'HOME', mockCnpmrc);
mm(process.env, 'USERPROFILE', mockCnpmrc);
// clean cnpm_config.js cache
delete require.cache[require.resolve('../lib/get')];
delete require.cache[require.resolve('../lib/cnpm_config')];
const get = require('../lib/get');
mm.restore();

describe('test/get.test.js', () => {
it('should retry on JSON parse error', function* () {
Expand All @@ -18,4 +31,23 @@ describe('test/get.test.js', () => {
assert(err.res.requestUrls.length === 5);
}
});

it('should set auth info into header', function* () {
const logger = {
warn(msg) {
assert(msg.indexOf('[npminstall:get] retry GET') === 0);
},
};
const options = { dataType: 'json' };
assert(fs.existsSync(mockCnpmrc + '.cnpmrc'));
try {
yield get('https://registry-mock.org/mock', options, { console: logger });
assert(false, 'should not run this');
} catch (err) {
const headers = options.headers;
assert(headers.Authorization);
assert(err.name === 'RequestError');
assert(err.res.requestUrls.length === 5);
}
});
});

0 comments on commit 4464362

Please sign in to comment.