<commit_before>/*jslint node : true, nomen: true, plusplus: true, vars: true, eqeq: true,*/
"use strict";

var url = require('url');
var request = require('request');
var crypto = require('crypto');
var util = require("util");

module.exports = function(log, errors, options) {
	
	var headerPrefix = options.httpHeaderPrefix || 'x-bm';
	
	function getPublicKey(principal, cb) {
		request(util.format(options.keypath, principal), function (error, response, body) {
    		if (!error && response.statusCode == 200) {		
    			cb(error, body);
    			return;
    		}
    		cb(error || new Error('Invalid status code ' + response.statusCode));
		});
	}
	
	function verify(toSign, signature, publickey) {
		var hash = crypto.createVerify('RSA-SHA256');
		hash.update(toSign);
		return hash.verify(publickey, signature, 'base64');
	}
	
	function canonicalize(req) {
		var urlParts = url.parse(req.url, true);
		var params = urlParts.query || {};
		var queryString = Object.keys(params).sort().map(function(item, index, array) {
			return item+(params[item] ? '='+params[item] : '');
		}).join('&');
		var canonicalizedResource = urlParts.pathname + (queryString ? '?'+ queryString : '')
		
		var headers = req.headers || {};
		var canonicalizedHeaders = Object.keys(headers).filter(function(item) {
			return item.startsWith(headerPrefix+'-');
		}).sort().map(function(item, index, array) {
			return item+':'+headers[item];
		}).join("\n");			
		
		return [
              req.method,
              req.headers['content-md5'] || '',
              req.headers['content-type'] || '',
              req.headers[headerPrefix+'-date'] || req.headers['date'],
              canonicalizedHeaders,
              canonicalizedResource
              ].join("\n");
	}
	
	return function check(req, res, next) {
    	
		var principal = req.authorization.credentials.principal;
		
		getPublicKey(principal, function(error, publickey) {
			if(error) {
				// Invalid principal
        		log.info(req.authorization.credentials.principal, 'Public key not found', error.message);
        		next(new errors.NotAuthorizedError('invalid signature'));
        	    return;
			}
			
			var toSign = canonicalize(req);
			
			if(!verify(toSign, req.authorization.credentials.signature, publickey)) {
				// Invalid signature
				log.warn(req.authorization.credentials.principal, 'Invalid signature');
				next(new errors.NotAuthorizedError('invalid signature'));
				return;
			}
	    	next();
	    	
	    	
		});
    }
};<commit_msg>Correction path haproxy<commit_after>/*jslint node : true, nomen: true, plusplus: true, vars: true, eqeq: true,*/
"use strict";

var url = require('url');
var request = require('request');
var crypto = require('crypto');
var util = require("util");

module.exports = function(log, errors, options) {
	
	var headerPrefix = options.httpHeaderPrefix || 'x-bm';
	
	function getPublicKey(principal, cb) {
		request(util.format(options.keypath, principal), function (error, response, body) {
    		if (!error && response.statusCode == 200) {		
    			cb(error, body);
    			return;
    		}
    		cb(error || new Error('Invalid status code ' + response.statusCode));
		});
	}
	
	function verify(toSign, signature, publickey) {
		var hash = crypto.createVerify('RSA-SHA256');
		hash.update(toSign);
		return hash.verify(publickey, signature, 'base64');
	}
	
	function canonicalize(req) {
		var urlParts = url.parse(req.url, true);
		var params = urlParts.query || {};
		var queryString = Object.keys(params).sort().map(function(item, index, array) {
			return item+(params[item] ? '='+params[item] : '');
		}).join('&');
		
		var headers = req.headers || {};
		var canonicalizedResource = (headers['x-forwarded-path'] || '') + urlParts.pathname + (queryString ? '?'+ queryString : '')
		var canonicalizedHeaders = Object.keys(headers).filter(function(item) {
			return item.startsWith(headerPrefix+'-');
		}).sort().map(function(item, index, array) {
			return item+':'+headers[item];
		}).join("\n");			
		
		return [
              req.method,
              req.headers['content-md5'] || '',
              req.headers['content-type'] || '',
              req.headers[headerPrefix+'-date'] || req.headers['date'],
              canonicalizedHeaders,
              canonicalizedResource
              ].join("\n");
	}
	
	return function check(req, res, next) {
    	
		var principal = req.authorization.credentials.principal;
		
		getPublicKey(principal, function(error, publickey) {
			if(error) {
				// Invalid principal
        		log.info(req.authorization.credentials.principal, 'Public key not found', error.message);
        		next(new errors.NotAuthorizedError('invalid signature'));
        	    return;
			}
			
			var toSign = canonicalize(req);
			
			if(!verify(toSign, req.authorization.credentials.signature, publickey)) {
				// Invalid signature
				log.warn(req.authorization.credentials.principal, 'Invalid signature');
				next(new errors.NotAuthorizedError('invalid signature'));
				return;
			}
	    	next();
	    	
	    	
		});
    }
};
{'fraction_non_alphanumeric': 0.15763449367088608, 'fraction_numerical': 0.003560126582278481, 'mean_word_length': 3.2317991631799163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2679357', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1519, 'n_words': 309}