2018-04-20 18:26:37 -07:00
|
|
|
'use strict'
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const defaultOpts = require('./default-opts.js')
|
2018-04-20 18:26:37 -07:00
|
|
|
const url = require('url')
|
|
|
|
|
|
|
|
module.exports = getAuth
|
2020-10-02 17:52:19 -04:00
|
|
|
function getAuth (registry, opts_ = {}) {
|
2020-11-03 20:39:24 -05:00
|
|
|
if (!registry)
|
|
|
|
throw new Error('registry is required')
|
2020-10-02 17:52:19 -04:00
|
|
|
const opts = opts_.forceAuth ? opts_.forceAuth : { ...defaultOpts, ...opts_ }
|
|
|
|
const AUTH = {}
|
2018-04-20 18:26:37 -07:00
|
|
|
const regKey = registry && registryKey(registry)
|
|
|
|
const doKey = (key, alias) => addKey(opts, AUTH, regKey, key, alias)
|
2019-01-29 14:43:00 -08:00
|
|
|
doKey('token')
|
2018-04-20 18:26:37 -07:00
|
|
|
doKey('_authToken', 'token')
|
|
|
|
doKey('username')
|
|
|
|
doKey('password')
|
|
|
|
doKey('_password', 'password')
|
|
|
|
doKey('email')
|
|
|
|
doKey('_auth')
|
|
|
|
doKey('otp')
|
|
|
|
doKey('always-auth', 'alwaysAuth')
|
2020-11-03 20:39:24 -05:00
|
|
|
if (AUTH.password)
|
2018-04-20 18:26:37 -07:00
|
|
|
AUTH.password = Buffer.from(AUTH.password, 'base64').toString('utf8')
|
2020-11-03 20:39:24 -05:00
|
|
|
|
2019-04-05 15:17:30 -04:00
|
|
|
if (AUTH._auth && !(AUTH.username && AUTH.password)) {
|
|
|
|
let auth = Buffer.from(AUTH._auth, 'base64').toString()
|
|
|
|
auth = auth.split(':')
|
|
|
|
AUTH.username = auth.shift()
|
|
|
|
AUTH.password = auth.join(':')
|
|
|
|
}
|
2018-04-20 18:26:37 -07:00
|
|
|
AUTH.alwaysAuth = AUTH.alwaysAuth === 'false' ? false : !!AUTH.alwaysAuth
|
|
|
|
return AUTH
|
|
|
|
}
|
|
|
|
|
|
|
|
function addKey (opts, obj, scope, key, objKey) {
|
2020-11-03 20:39:24 -05:00
|
|
|
if (opts[key])
|
2019-01-29 14:43:00 -08:00
|
|
|
obj[objKey || key] = opts[key]
|
2020-11-03 20:39:24 -05:00
|
|
|
|
|
|
|
if (scope && opts[`${scope}:${key}`])
|
2019-01-29 14:43:00 -08:00
|
|
|
obj[objKey || key] = opts[`${scope}:${key}`]
|
2018-04-20 18:26:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called a nerf dart in the main codebase. Used as a "safe"
|
|
|
|
// key when fetching registry info from config.
|
|
|
|
function registryKey (registry) {
|
2020-10-02 17:52:19 -04:00
|
|
|
const parsed = new url.URL(registry)
|
2018-04-20 18:26:37 -07:00
|
|
|
const formatted = url.format({
|
2020-10-02 17:52:19 -04:00
|
|
|
protocol: parsed.protocol,
|
2018-04-20 18:26:37 -07:00
|
|
|
host: parsed.host,
|
|
|
|
pathname: parsed.pathname,
|
2020-11-03 20:39:24 -05:00
|
|
|
slashes: true,
|
2018-04-20 18:26:37 -07:00
|
|
|
})
|
2020-10-02 17:52:19 -04:00
|
|
|
return url.format(new url.URL('.', formatted)).replace(/^[^:]+:/, '')
|
2018-04-20 18:26:37 -07:00
|
|
|
}
|