Skip to content

Commit 294e2eb

Browse files
committed
Send auth when hostname matches registry, and reg has auth
PR-URL: #46 Credit: @isaacs Close: #46 Reviewed-by: @wraithgar
1 parent bca1880 commit 294e2eb

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

auth.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ const hasAuth = (regKey, opts) => (
2727
opts[`${regKey}:username`] && opts[`${regKey}:_password`]
2828
)
2929

30+
const sameHost = (a, b) => {
31+
const parsedA = new URL(a)
32+
const parsedB = new URL(b)
33+
return parsedA.host === parsedB.host
34+
}
35+
36+
const getRegistry = opts => {
37+
const { spec } = opts
38+
const { scope: specScope, subSpec } = spec ? npa(spec) : {}
39+
const subSpecScope = subSpec && subSpec.scope
40+
const scope = subSpec ? subSpecScope : specScope
41+
const scopeReg = scope && opts[`${scope}:registry`]
42+
return scopeReg || opts.registry
43+
}
44+
3045
const getAuth = (uri, opts = {}) => {
3146
const { forceAuth } = opts
3247
if (!uri)
@@ -44,19 +59,19 @@ const getAuth = (uri, opts = {}) => {
4459
})
4560
}
4661

47-
// no auth for this URI
48-
if (!regKey && opts.spec) {
49-
// If making a tarball request to a different base URI than the
50-
// registry where we logged in, but the same auth SHOULD be sent
51-
// to that artifact host, then we track where it was coming in from,
52-
// and warn the user if we get a 4xx error on it.
53-
const { spec } = opts
54-
const { scope: specScope, subSpec } = npa(spec)
55-
const subSpecScope = subSpec && subSpec.scope
56-
const scope = subSpec ? subSpecScope : specScope
57-
const scopeReg = scope && opts[`${scope}:registry`]
58-
const scopeAuthKey = scopeReg && regKeyFromURI(scopeReg, opts)
59-
return new Auth({ scopeAuthKey })
62+
// no auth for this URI, but might have it for the registry
63+
if (!regKey) {
64+
const registry = getRegistry(opts)
65+
if (registry && uri !== registry && sameHost(uri, registry))
66+
return getAuth(registry, opts)
67+
else if (registry !== opts.registry) {
68+
// If making a tarball request to a different base URI than the
69+
// registry where we logged in, but the same auth SHOULD be sent
70+
// to that artifact host, then we track where it was coming in from,
71+
// and warn the user if we get a 4xx error on it.
72+
const scopeAuthKey = regKeyFromURI(registry, opts)
73+
return new Auth({ scopeAuthKey })
74+
}
6075
}
6176

6277
const {

test/auth.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,33 @@ t.test('scopeAuthKey tests', t => {
428428

429429
t.end()
430430
})
431+
432+
t.test('registry host matches, path does not, send auth', t => {
433+
const opts = {
434+
'@other-scope:registry': 'https://other-scope-registry.com/other/scope/',
435+
'//other-scope-registry.com/other/scope/:_authToken': 'cafebad',
436+
'@scope:registry': 'https://scope-host.com/scope/host/',
437+
'//scope-host.com/scope/host/:_authToken': 'c0ffee',
438+
registry: 'https://registry.example.com/some/path/',
439+
}
440+
const uri = 'https://scope-host.com/blahblah/bloobloo/foo.tgz'
441+
t.same(getAuth(uri, { ...opts, spec: '@scope/foo' }), {
442+
scopeAuthKey: null,
443+
token: 'c0ffee',
444+
auth: null,
445+
isBasicAuth: false,
446+
})
447+
t.same(getAuth(uri, { ...opts, spec: '@other-scope/foo' }), {
448+
scopeAuthKey: '//other-scope-registry.com/other/scope/',
449+
token: null,
450+
auth: null,
451+
isBasicAuth: false,
452+
})
453+
t.same(getAuth(uri, { ...opts, registry: 'https://scope-host.com/scope/host/' }), {
454+
scopeAuthKey: null,
455+
token: 'c0ffee',
456+
auth: null,
457+
isBasicAuth: false,
458+
})
459+
t.end()
460+
})

0 commit comments

Comments
 (0)