3 var Sntp = require('sntp');
4 var Boom = require('boom');
12 exports.version = function () {
14 return require('../package.json').version;
19 maxMatchLength: 4096 // Limit the length of uris and headers to avoid a DoS attack on string matching
23 // Extract host and port from request
26 internals.hostHeaderRegex = /^(?:(?:\r\n)?\s)*((?:[^:]+)|(?:\[[^\]]+\]))(?::(\d+))?(?:(?:\r\n)?\s)*$/; // (IPv4, hostname)|(IPv6)
29 exports.parseHost = function (req, hostHeaderName) {
31 hostHeaderName = (hostHeaderName ? hostHeaderName.toLowerCase() : 'host');
32 var hostHeader = req.headers[hostHeaderName];
37 if (hostHeader.length > exports.limits.maxMatchLength) {
41 var hostParts = hostHeader.match(internals.hostHeaderRegex);
48 port: (hostParts[2] ? hostParts[2] : (req.connection && req.connection.encrypted ? 443 : 80))
53 // Parse Content-Type header content
55 exports.parseContentType = function (header) {
61 return header.split(';')[0].trim().toLowerCase();
65 // Convert node's to request configuration object
67 exports.parseRequest = function (req, options) {
73 // Obtain host and port information
79 host = exports.parseHost(req, options.hostHeaderName);
81 return new Error('Invalid Host header');
88 host: options.host || host.name,
89 port: options.port || host.port,
90 authorization: req.headers.authorization,
91 contentType: req.headers['content-type'] || ''
98 exports.now = function (localtimeOffsetMsec) {
100 return Sntp.now() + (localtimeOffsetMsec || 0);
104 exports.nowSecs = function (localtimeOffsetMsec) {
106 return Math.floor(exports.now(localtimeOffsetMsec) / 1000);
110 internals.authHeaderRegex = /^(\w+)(?:\s+(.*))?$/; // Header: scheme[ something]
111 internals.attributeRegex = /^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~]+$/; // !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
114 // Parse Hawk HTTP Authorization header
116 exports.parseAuthorizationHeader = function (header, keys) {
118 keys = keys || ['id', 'ts', 'nonce', 'hash', 'ext', 'mac', 'app', 'dlg'];
121 return Boom.unauthorized(null, 'Hawk');
124 if (header.length > exports.limits.maxMatchLength) {
125 return Boom.badRequest('Header length too long');
128 var headerParts = header.match(internals.authHeaderRegex);
130 return Boom.badRequest('Invalid header syntax');
133 var scheme = headerParts[1];
134 if (scheme.toLowerCase() !== 'hawk') {
135 return Boom.unauthorized(null, 'Hawk');
138 var attributesString = headerParts[2];
139 if (!attributesString) {
140 return Boom.badRequest('Invalid header syntax');
144 var errorMessage = '';
145 var verify = attributesString.replace(/(\w+)="([^"\\]*)"\s*(?:,\s*|$)/g, function ($0, $1, $2) {
147 // Check valid attribute names
149 if (keys.indexOf($1) === -1) {
150 errorMessage = 'Unknown attribute: ' + $1;
154 // Allowed attribute value characters
156 if ($2.match(internals.attributeRegex) === null) {
157 errorMessage = 'Bad attribute value: ' + $1;
161 // Check for duplicates
163 if (attributes.hasOwnProperty($1)) {
164 errorMessage = 'Duplicate attribute: ' + $1;
173 return Boom.badRequest(errorMessage || 'Bad header format');
180 exports.unauthorized = function (message, attributes) {
182 return Boom.unauthorized(message, 'Hawk', attributes);