index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var url = require('url')
  2. var base64 = require('./base64')
  3. var decodeBase64 = base64.decodeBase64
  4. var encodeBase64 = base64.encodeBase64
  5. var tokenKey = ':_authToken'
  6. var legacyTokenKey = ':_auth'
  7. var userKey = ':username'
  8. var passwordKey = ':_password'
  9. module.exports = function () {
  10. var checkUrl
  11. var options
  12. if (arguments.length >= 2) {
  13. checkUrl = arguments[0]
  14. options = arguments[1]
  15. } else if (typeof arguments[0] === 'string') {
  16. checkUrl = arguments[0]
  17. } else {
  18. options = arguments[0]
  19. }
  20. options = options || {}
  21. options.npmrc = options.npmrc || require('rc')('npm', { registry: 'https://registry.npmjs.org/' })
  22. checkUrl = checkUrl || options.npmrc.registry
  23. return getRegistryAuthInfo(checkUrl, options) || getLegacyAuthInfo(options.npmrc)
  24. }
  25. function getRegistryAuthInfo (checkUrl, options) {
  26. var parsed = url.parse(checkUrl, false, true)
  27. var pathname
  28. while (pathname !== '/' && parsed.pathname !== pathname) {
  29. pathname = parsed.pathname || '/'
  30. var regUrl = '//' + parsed.host + pathname.replace(/\/$/, '')
  31. var authInfo = getAuthInfoForUrl(regUrl, options.npmrc)
  32. if (authInfo) {
  33. return authInfo
  34. }
  35. // break if not recursive
  36. if (!options.recursive) {
  37. return /\/$/.test(checkUrl)
  38. ? undefined
  39. : getRegistryAuthInfo(url.resolve(checkUrl, '.'), options)
  40. }
  41. parsed.pathname = url.resolve(normalizePath(pathname), '..') || '/'
  42. }
  43. return undefined
  44. }
  45. function getLegacyAuthInfo (npmrc) {
  46. if (!npmrc._auth) {
  47. return undefined
  48. }
  49. var token = replaceEnvironmentVariable(npmrc._auth)
  50. return { token: token, type: 'Basic' }
  51. }
  52. function normalizePath (path) {
  53. return path[path.length - 1] === '/' ? path : path + '/'
  54. }
  55. function getAuthInfoForUrl (regUrl, npmrc) {
  56. // try to get bearer token
  57. var bearerAuth = getBearerToken(npmrc[regUrl + tokenKey] || npmrc[regUrl + '/' + tokenKey])
  58. if (bearerAuth) {
  59. return bearerAuth
  60. }
  61. // try to get basic token
  62. var username = npmrc[regUrl + userKey] || npmrc[regUrl + '/' + userKey]
  63. var password = npmrc[regUrl + passwordKey] || npmrc[regUrl + '/' + passwordKey]
  64. var basicAuth = getTokenForUsernameAndPassword(username, password)
  65. if (basicAuth) {
  66. return basicAuth
  67. }
  68. var basicAuthWithToken = getLegacyAuthToken(npmrc[regUrl + legacyTokenKey] || npmrc[regUrl + '/' + legacyTokenKey])
  69. if (basicAuthWithToken) {
  70. return basicAuthWithToken
  71. }
  72. return undefined
  73. }
  74. function replaceEnvironmentVariable (token) {
  75. return token.replace(/^\$\{?([^}]*)\}?$/, function (fullMatch, envVar) {
  76. return process.env[envVar]
  77. })
  78. }
  79. function getBearerToken (tok) {
  80. if (!tok) {
  81. return undefined
  82. }
  83. // check if bearer token is set as environment variable
  84. var token = replaceEnvironmentVariable(tok)
  85. return { token: token, type: 'Bearer' }
  86. }
  87. function getTokenForUsernameAndPassword (username, password) {
  88. if (!username || !password) {
  89. return undefined
  90. }
  91. // passwords are base64 encoded, so we need to decode it
  92. // See https://github.com/npm/npm/blob/v3.10.6/lib/config/set-credentials-by-uri.js#L26
  93. var pass = decodeBase64(replaceEnvironmentVariable(password))
  94. // a basic auth token is base64 encoded 'username:password'
  95. // See https://github.com/npm/npm/blob/v3.10.6/lib/config/get-credentials-by-uri.js#L70
  96. var token = encodeBase64(username + ':' + pass)
  97. // we found a basicToken token so let's exit the loop
  98. return {
  99. token: token,
  100. type: 'Basic',
  101. password: pass,
  102. username: username
  103. }
  104. }
  105. function getLegacyAuthToken (tok) {
  106. if (!tok) {
  107. return undefined
  108. }
  109. // check if legacy auth token is set as environment variable
  110. var token = replaceEnvironmentVariable(tok)
  111. return { token: token, type: 'Basic' }
  112. }