@@ -27,6 +27,21 @@ const hasAuth = (regKey, opts) => (
27
27
opts [ `${ regKey } :username` ] && opts [ `${ regKey } :_password` ]
28
28
)
29
29
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
+
30
45
const getAuth = ( uri , opts = { } ) => {
31
46
const { forceAuth } = opts
32
47
if ( ! uri )
@@ -44,19 +59,19 @@ const getAuth = (uri, opts = {}) => {
44
59
} )
45
60
}
46
61
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
+ }
60
75
}
61
76
62
77
const {
0 commit comments