UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. * basic-auth
  3. * Copyright(c) 2013 TJ Holowaychuk
  4. * Copyright(c) 2014 Jonathan Ong
  5. * Copyright(c) 2015-2016 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. 'use strict'
  9. /**
  10. * Module exports.
  11. * @public
  12. */
  13. module.exports = auth
  14. module.exports.parse = parse
  15. /**
  16. * RegExp for basic auth credentials
  17. *
  18. * credentials = auth-scheme 1*SP token68
  19. * auth-scheme = "Basic" ; case insensitive
  20. * token68 = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
  21. * @private
  22. */
  23. var CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/
  24. /**
  25. * RegExp for basic auth user/pass
  26. *
  27. * user-pass = userid ":" password
  28. * userid = *<TEXT excluding ":">
  29. * password = *TEXT
  30. * @private
  31. */
  32. var USER_PASS_REGEXP = /^([^:]*):(.*)$/
  33. /**
  34. * Parse the Authorization header field of a request.
  35. *
  36. * @param {object} req
  37. * @return {object} with .name and .pass
  38. * @public
  39. */
  40. function auth (req) {
  41. if (!req) {
  42. throw new TypeError('argument req is required')
  43. }
  44. if (typeof req !== 'object') {
  45. throw new TypeError('argument req is required to be an object')
  46. }
  47. // get header
  48. var header = getAuthorization(req.req || req)
  49. // parse header
  50. return parse(header)
  51. }
  52. /**
  53. * Decode base64 string.
  54. * @private
  55. */
  56. function decodeBase64 (str) {
  57. return new Buffer(str, 'base64').toString()
  58. }
  59. /**
  60. * Get the Authorization header from request object.
  61. * @private
  62. */
  63. function getAuthorization (req) {
  64. if (!req.headers || typeof req.headers !== 'object') {
  65. throw new TypeError('argument req is required to have headers property')
  66. }
  67. return req.headers.authorization
  68. }
  69. /**
  70. * Parse basic auth to object.
  71. *
  72. * @param {string} string
  73. * @return {object}
  74. * @public
  75. */
  76. function parse (string) {
  77. if (typeof string !== 'string') {
  78. return undefined
  79. }
  80. // parse header
  81. var match = CREDENTIALS_REGEXP.exec(string)
  82. if (!match) {
  83. return undefined
  84. }
  85. // decode user pass
  86. var userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]))
  87. if (!userPass) {
  88. return undefined
  89. }
  90. // return credentials object
  91. return new Credentials(userPass[1], userPass[2])
  92. }
  93. /**
  94. * Object to represent user credentials.
  95. * @private
  96. */
  97. function Credentials (name, pass) {
  98. this.name = name
  99. this.pass = pass
  100. }