Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit 3c9ac9f

Browse files
committed
include non-registry-scoped auth configs at load, scoped to default registry
1 parent e59fb32 commit 3c9ac9f

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ class Config {
282282
// symbols, as that module also does a bunch of get operations
283283
this[_loaded] = true
284284

285+
process.emit('time', 'config:load:credentials')
286+
const reg = this.get('registry')
287+
// console.error('registry', reg, this)
288+
const creds = this.getCredentialsByURI(reg)
289+
// ignore this error because a failed set will strip out anything that
290+
// might be a security hazard, which was the intention.
291+
try { this.setCredentialsByURI(reg, creds) } catch (_) {}
292+
process.emit('timeEnd', 'config:load:credentials')
293+
285294
// set proper globalPrefix now that everything is loaded
286295
this.globalPrefix = this.get('prefix')
287296

test/index.js

+68
Original file line numberDiff line numberDiff line change
@@ -851,3 +851,71 @@ t.test('setting username/password/email individually', async t => {
851851
auth: Buffer.from('admin:admin').toString('base64'),
852852
})
853853
})
854+
855+
t.test('nerfdart auths set at the top level into the registry', async t => {
856+
const registry = 'https://registry.npmjs.org/'
857+
const _auth = Buffer.from('admin:admin').toString('base64')
858+
const username = 'admin'
859+
const _password = Buffer.from('admin').toString('base64')
860+
const email = '[email protected]'
861+
const _authToken = 'deadbeefblahblah'
862+
863+
// TODO(isaacs): We should NOT be requiring that email be nerfdarted.
864+
// It's not particularly secret, and is only included in the "credentials"
865+
// concept as an accident of history, because the old couchdb reg used it.
866+
// It should be treated just like any other plain old config field.
867+
868+
// name: [ini, expect]
869+
const cases = {
870+
// TODO: should not require email
871+
// '_auth only, no email': [ `_auth=${_auth}`, {
872+
// '//registry.npmjs.org/:username': username,
873+
// '//registry.npmjs.org/:_password': _password,
874+
// }],
875+
'_auth with email': [ `_auth=${_auth}\nemail=${email}`, {
876+
'//registry.npmjs.org/:username': username,
877+
'//registry.npmjs.org/:_password': _password,
878+
// TODO: change to just 'email': email
879+
'//registry.npmjs.org/:email': email,
880+
}],
881+
'_authToken alone': [ `_authToken=${_authToken}`, {
882+
'//registry.npmjs.org/:_authToken': _authToken,
883+
}],
884+
'_authToken and email': [ `_authToken=${_authToken}\nemail=${email}`, {
885+
'//registry.npmjs.org/:_authToken': _authToken,
886+
// TODO: should include (un-nerf-darted) email
887+
// email,
888+
}],
889+
// TODO: should not require email
890+
// 'username and _password': [ `username=${username}\n_password=${_password}`, {
891+
// '//registry.npmjs.org/:username': username,
892+
// '//registry.npmjs.org/:_password': _password,
893+
// }],
894+
'username, password, email': [ `username=${username}\n_password=${_password}\nemail=${email}`, {
895+
'//registry.npmjs.org/:username': username,
896+
'//registry.npmjs.org/:_password': _password,
897+
'//registry.npmjs.org/:email': '[email protected]',
898+
}],
899+
// handled invalid cases
900+
'username, no _password': [`username=${username}`, {}],
901+
'_password, no username': [`_password=${_password}`, {}],
902+
}
903+
904+
for (const [name, [ini, expect]] of Object.entries(cases)) {
905+
//console.log({name, ini, expect})
906+
t.test(name, async t => {
907+
const path = t.testdir({ '.npmrc': ini })
908+
const opts = {
909+
shorthands: {},
910+
argv: ['node', __filename, `--userconfig=${path}/.npmrc`, `--globalconfig=${path}/npmrc`],
911+
definitions: {
912+
registry: { default: registry },
913+
},
914+
npmPath: process.cwd(),
915+
}
916+
const c = new Config(opts)
917+
await c.load()
918+
t.same(c.list[3], expect)
919+
})
920+
}
921+
})

0 commit comments

Comments
 (0)