-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): get auth working with all the little details
- Loading branch information
Showing
3 changed files
with
301 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,46 @@ | ||
'use strict' | ||
|
||
module.exports = getAuth | ||
function getAuth (conf) { | ||
const AUTH = {} | ||
const iterator = typeof conf.forEach === 'function' | ||
? conf | ||
: conf.keys | ||
iterator.forEach((k) => { | ||
const authMatchGlobal = k.match( | ||
/^(_authToken|username|_password|password|email|always-auth|_auth)$/ | ||
) | ||
const authMatchScoped = k[0] === '/' && k.match( | ||
/(.*):(_authToken|username|_password|password|email|always-auth|_auth)$/ | ||
) | ||
const url = require('url') | ||
|
||
// if it matches scoped it will also match global | ||
if (authMatchGlobal || authMatchScoped) { | ||
let nerfDart = null | ||
let key = null | ||
let val = null | ||
module.exports = getAuth | ||
function getAuth (registry, conf) { | ||
if (!registry) { throw new Error('registry is required') } | ||
if (!conf) { conf = new Map() } | ||
let AUTH = {} | ||
const regKey = registry && registryKey(registry) | ||
const doKey = (key, alias) => addKey(conf, AUTH, regKey, key, alias) | ||
doKey('_authToken', 'token') | ||
doKey('username') | ||
doKey('password') | ||
doKey('_password', 'password') | ||
doKey('email') | ||
doKey('_auth') | ||
doKey('otp') | ||
doKey('always-auth', 'alwaysAuth') | ||
if (AUTH.password) { | ||
AUTH.password = Buffer.from(AUTH.password, 'base64').toString('utf8') | ||
} | ||
AUTH.alwaysAuth = AUTH.alwaysAuth === 'false' ? false : !!AUTH.alwaysAuth | ||
return AUTH | ||
} | ||
|
||
if (authMatchScoped) { | ||
nerfDart = authMatchScoped[1] | ||
key = authMatchScoped[2] | ||
val = conf.get(k) | ||
if (!AUTH[nerfDart]) { | ||
AUTH[nerfDart] = { | ||
alwaysAuth: !!conf.get('always-auth') | ||
} | ||
} | ||
} else { | ||
key = authMatchGlobal[1] | ||
val = conf.get(k) | ||
AUTH.alwaysAuth = !!conf.get('always-auth') | ||
} | ||
function addKey (conf, obj, scope, key, objKey) { | ||
if (conf.get(key)) { | ||
obj[objKey || key] = conf.get(key) | ||
} | ||
if (scope && conf.get(`${scope}:${key}`)) { | ||
obj[objKey || key] = conf.get(`${scope}:${key}`) | ||
} | ||
} | ||
|
||
const auth = authMatchScoped ? AUTH[nerfDart] : AUTH | ||
if (key === '_authToken') { | ||
auth.token = val | ||
} else if (key.match(/password$/i)) { | ||
auth.password = | ||
// the config file stores password auth already-encoded. pacote expects | ||
// the actual username/password pair. | ||
Buffer.from(val, 'base64').toString('utf8') | ||
} else if (key === 'always-auth') { | ||
auth.alwaysAuth = val === 'false' ? false : !!val | ||
} else { | ||
auth[key] = val | ||
} | ||
} | ||
// Called a nerf dart in the main codebase. Used as a "safe" | ||
// key when fetching registry info from config. | ||
function registryKey (registry) { | ||
const parsed = url.parse(registry) | ||
const formatted = url.format({ | ||
host: parsed.host, | ||
pathname: parsed.pathname, | ||
slashes: parsed.slashes | ||
}) | ||
return AUTH | ||
return url.resolve(formatted, '.') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.