[6a3a178] | 1 | 'use strict'
|
---|
| 2 | const npa = require('npm-package-arg')
|
---|
| 3 |
|
---|
| 4 | // Find the longest registry key that is used for some kind of auth
|
---|
| 5 | // in the options.
|
---|
| 6 | const regKeyFromURI = (uri, opts) => {
|
---|
| 7 | const parsed = new URL(uri)
|
---|
| 8 | // try to find a config key indicating we have auth for this registry
|
---|
| 9 | // can be one of :_authToken, :_auth, or :_password and :username
|
---|
| 10 | // We walk up the "path" until we're left with just //<host>[:<port>],
|
---|
| 11 | // stopping when we reach '//'.
|
---|
| 12 | let regKey = `//${parsed.host}${parsed.pathname}`
|
---|
| 13 | while (regKey.length > '//'.length) {
|
---|
| 14 | // got some auth for this URI
|
---|
| 15 | if (hasAuth(regKey, opts))
|
---|
| 16 | return regKey
|
---|
| 17 |
|
---|
| 18 | // can be either //host/some/path/:_auth or //host/some/path:_auth
|
---|
| 19 | // walk up by removing EITHER what's after the slash OR the slash itself
|
---|
| 20 | regKey = regKey.replace(/([^/]+|\/)$/, '')
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | const hasAuth = (regKey, opts) => (
|
---|
| 25 | opts[`${regKey}:_authToken`] ||
|
---|
| 26 | opts[`${regKey}:_auth`] ||
|
---|
| 27 | opts[`${regKey}:username`] && opts[`${regKey}:_password`]
|
---|
| 28 | )
|
---|
| 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 |
|
---|
| 45 | const getAuth = (uri, opts = {}) => {
|
---|
| 46 | const { forceAuth } = opts
|
---|
| 47 | if (!uri)
|
---|
| 48 | throw new Error('URI is required')
|
---|
| 49 | const regKey = regKeyFromURI(uri, forceAuth || opts)
|
---|
| 50 |
|
---|
| 51 | // we are only allowed to use what's in forceAuth if specified
|
---|
| 52 | if (forceAuth && !regKey) {
|
---|
| 53 | return new Auth({
|
---|
| 54 | scopeAuthKey: null,
|
---|
| 55 | token: forceAuth._authToken || forceAuth.token,
|
---|
| 56 | username: forceAuth.username,
|
---|
| 57 | password: forceAuth._password || forceAuth.password,
|
---|
| 58 | auth: forceAuth._auth || forceAuth.auth,
|
---|
| 59 | })
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 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 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | const {
|
---|
| 78 | [`${regKey}:_authToken`]: token,
|
---|
| 79 | [`${regKey}:username`]: username,
|
---|
| 80 | [`${regKey}:_password`]: password,
|
---|
| 81 | [`${regKey}:_auth`]: auth,
|
---|
| 82 | } = opts
|
---|
| 83 |
|
---|
| 84 | return new Auth({
|
---|
| 85 | scopeAuthKey: null,
|
---|
| 86 | token,
|
---|
| 87 | auth,
|
---|
| 88 | username,
|
---|
| 89 | password,
|
---|
| 90 | })
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | class Auth {
|
---|
| 94 | constructor ({ token, auth, username, password, scopeAuthKey }) {
|
---|
| 95 | this.scopeAuthKey = scopeAuthKey
|
---|
| 96 | this.token = null
|
---|
| 97 | this.auth = null
|
---|
| 98 | this.isBasicAuth = false
|
---|
| 99 | if (token)
|
---|
| 100 | this.token = token
|
---|
| 101 | else if (auth)
|
---|
| 102 | this.auth = auth
|
---|
| 103 | else if (username && password) {
|
---|
| 104 | const p = Buffer.from(password, 'base64').toString('utf8')
|
---|
| 105 | this.auth = Buffer.from(`${username}:${p}`, 'utf8').toString('base64')
|
---|
| 106 | this.isBasicAuth = true
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | module.exports = getAuth
|
---|