Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.1 KiB

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