1var assert = require('assert') 2 3var toNerfDart = require('./nerf-dart.js') 4 5module.exports = getCredentialsByURI 6 7function getCredentialsByURI (uri) { 8 assert(uri && typeof uri === 'string', 'registry URL is required') 9 var nerfed = toNerfDart(uri) 10 var defnerf = toNerfDart(this.get('registry')) 11 12 // hidden class micro-optimization 13 var c = { 14 scope: nerfed, 15 token: undefined, 16 password: undefined, 17 username: undefined, 18 email: undefined, 19 auth: undefined, 20 alwaysAuth: undefined 21 } 22 23 // used to override scope matching for tokens as well as legacy auth 24 if (this.get(nerfed + ':always-auth') !== undefined) { 25 var val = this.get(nerfed + ':always-auth') 26 c.alwaysAuth = val === 'false' ? false : !!val 27 } else if (this.get('always-auth') !== undefined) { 28 c.alwaysAuth = this.get('always-auth') 29 } 30 31 if (this.get(nerfed + ':_authToken')) { 32 c.token = this.get(nerfed + ':_authToken') 33 // the bearer token is enough, don't confuse things 34 return c 35 } 36 37 if (this.get(nerfed + ':-authtoken')) { 38 c.token = this.get(nerfed + ':-authtoken') 39 // the bearer token is enough, don't confuse things 40 return c 41 } 42 43 // Handle the old-style _auth=<base64> style for the default 44 // registry, if set. 45 var authDef = this.get('_auth') 46 var userDef = this.get('username') 47 var passDef = this.get('_password') 48 if (authDef && !(userDef && passDef)) { 49 authDef = Buffer.from(authDef, 'base64').toString() 50 authDef = authDef.split(':') 51 userDef = authDef.shift() 52 passDef = authDef.join(':') 53 } 54 55 if (this.get(nerfed + ':_password')) { 56 c.password = Buffer.from(this.get(nerfed + ':_password'), 'base64').toString('utf8') 57 } else if (nerfed === defnerf && passDef) { 58 c.password = passDef 59 } 60 61 if (this.get(nerfed + ':username')) { 62 c.username = this.get(nerfed + ':username') 63 } else if (nerfed === defnerf && userDef) { 64 c.username = userDef 65 } 66 67 if (this.get(nerfed + ':email')) { 68 c.email = this.get(nerfed + ':email') 69 } else if (this.get('email')) { 70 c.email = this.get('email') 71 } 72 73 if (c.username && c.password) { 74 c.auth = Buffer.from(c.username + ':' + c.password).toString('base64') 75 } 76 77 return c 78} 79