get-credentials-by-uri.js 2.2 KB

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