utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Load modules
  2. var Sntp = require('sntp');
  3. var Boom = require('boom');
  4. // Declare internals
  5. var internals = {};
  6. exports.version = function () {
  7. return require('../package.json').version;
  8. };
  9. // Extract host and port from request
  10. // $1 $2
  11. internals.hostHeaderRegex = /^(?:(?:\r\n)?\s)*((?:[^:]+)|(?:\[[^\]]+\]))(?::(\d+))?(?:(?:\r\n)?\s)*$/; // (IPv4, hostname)|(IPv6)
  12. exports.parseHost = function (req, hostHeaderName) {
  13. hostHeaderName = (hostHeaderName ? hostHeaderName.toLowerCase() : 'host');
  14. var hostHeader = req.headers[hostHeaderName];
  15. if (!hostHeader) {
  16. return null;
  17. }
  18. var hostParts = hostHeader.match(internals.hostHeaderRegex);
  19. if (!hostParts) {
  20. return null;
  21. }
  22. return {
  23. name: hostParts[1],
  24. port: (hostParts[2] ? hostParts[2] : (req.connection && req.connection.encrypted ? 443 : 80))
  25. };
  26. };
  27. // Parse Content-Type header content
  28. exports.parseContentType = function (header) {
  29. if (!header) {
  30. return '';
  31. }
  32. return header.split(';')[0].trim().toLowerCase();
  33. };
  34. // Convert node's to request configuration object
  35. exports.parseRequest = function (req, options) {
  36. if (!req.headers) {
  37. return req;
  38. }
  39. // Obtain host and port information
  40. if (!options.host || !options.port) {
  41. var host = exports.parseHost(req, options.hostHeaderName);
  42. if (!host) {
  43. return new Error('Invalid Host header');
  44. }
  45. }
  46. var request = {
  47. method: req.method,
  48. url: req.url,
  49. host: options.host || host.name,
  50. port: options.port || host.port,
  51. authorization: req.headers.authorization,
  52. contentType: req.headers['content-type'] || ''
  53. };
  54. return request;
  55. };
  56. exports.now = function (localtimeOffsetMsec) {
  57. return Sntp.now() + (localtimeOffsetMsec || 0);
  58. };
  59. exports.nowSecs = function (localtimeOffsetMsec) {
  60. return Math.floor(exports.now(localtimeOffsetMsec) / 1000);
  61. };
  62. // Parse Hawk HTTP Authorization header
  63. exports.parseAuthorizationHeader = function (header, keys) {
  64. keys = keys || ['id', 'ts', 'nonce', 'hash', 'ext', 'mac', 'app', 'dlg'];
  65. if (!header) {
  66. return Boom.unauthorized(null, 'Hawk');
  67. }
  68. var headerParts = header.match(/^(\w+)(?:\s+(.*))?$/); // Header: scheme[ something]
  69. if (!headerParts) {
  70. return Boom.badRequest('Invalid header syntax');
  71. }
  72. var scheme = headerParts[1];
  73. if (scheme.toLowerCase() !== 'hawk') {
  74. return Boom.unauthorized(null, 'Hawk');
  75. }
  76. var attributesString = headerParts[2];
  77. if (!attributesString) {
  78. return Boom.badRequest('Invalid header syntax');
  79. }
  80. var attributes = {};
  81. var errorMessage = '';
  82. var verify = attributesString.replace(/(\w+)="([^"\\]*)"\s*(?:,\s*|$)/g, function ($0, $1, $2) {
  83. // Check valid attribute names
  84. if (keys.indexOf($1) === -1) {
  85. errorMessage = 'Unknown attribute: ' + $1;
  86. return;
  87. }
  88. // Allowed attribute value characters: !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
  89. if ($2.match(/^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~]+$/) === null) {
  90. errorMessage = 'Bad attribute value: ' + $1;
  91. return;
  92. }
  93. // Check for duplicates
  94. if (attributes.hasOwnProperty($1)) {
  95. errorMessage = 'Duplicate attribute: ' + $1;
  96. return;
  97. }
  98. attributes[$1] = $2;
  99. return '';
  100. });
  101. if (verify !== '') {
  102. return Boom.badRequest(errorMessage || 'Bad header format');
  103. }
  104. return attributes;
  105. };
  106. exports.unauthorized = function (message, attributes) {
  107. return Boom.unauthorized(message, 'Hawk', attributes);
  108. };