<commit_before>/**
 * Default setup module.
 */
var defaults = require('../default');

/**
 * Utility module.
 */
var util = require('../util');

/**
 * Module for generating unique id.
 */
var uuid = require('node-uuid');

/**
 * Exporting module.
 */
module.exports = Digest;

/**
 * Digest Access Authentication.
 *
 * @param {String} authRealm authentication realm.
 * @param {Array} authUsers array of users.
 */
function Digest(authRealm, authUsers) {
	// Realm.
	this.realm = authRealm;
	// Users.
	this.users = authUsers;
	// Nonces.
	this.nonces = new Array();

	// Used for async callback.
	var self = this;

	/**
	 * Applies digest authentication and calls next after user is authenticated.
	 *
	 * @param {Request} request HTTP request object.
	 * @param {Response} response HTTP response object.
	 * @param {Function} next function(request, response) that will be called after user is authenticated.
	 */
	this.apply = function(request, response, next) {
		var requestBody = "";

		// Reading request body.
		request.on('data', function(chunk) {
			// append the current chunk of data to the requestBody variable.
			requestBody += chunk.toString();
		});
		// Processing authentication part.
		request.on('end', function() {
			var authenticated = self.isAuthenticated(request, response, requestBody);
			if(!authenticated) {
				self.ask(request, response, requestBody);
			} else {
				next(request, response);
			}
		});
	}
}

/**
 * Checks authorization header in request.
 *
 * @param {Request} request HTTP request object.
 * @param {Response} response HTTP response object.
 * @param {String} requestBody HTTP request body string.
 */
Digest.prototype.isAuthenticated = function(request, response, requestBody) {
	var authenticated = false;

	console.log(request.headers.authorization);

	// If header exists.
	if("authorization" in request.headers) {
		var header = request.headers.authorization;
		var co = this.parseAuthHeader(header);

		// Check for expiration.
		if(co.nonce in this.nonces) {
			// Sencond hash in digest access authentication.
			var ha2;
			// Calculating second hash.
			if(co.qop == "auth-int") {
				ha2 = util.md5(request.method + ":" + co.uri + ":" + util.md5(requestBody));
			} else {
				ha2 = util.md5(request.method + ":" + co.uri);
			}

			// Checking response for username.
			var ha1 = util.md5(this.users[co.username]);
			// If qop is specified.
			if(co.qop) {
				if(co.nc > this.nonces[co.nonce]) {
					// Updating nonce count.
					this.nonces[co.nonce] = co.nc;

					// Evaluating final authentication response.
					var authRes = util.md5(ha1 + ":" + co.nonce + ":" + co.nc + ":" + co.cnonce + ":" + co.qop + ":" + ha2);
					authenticated = (authRes == co.response);
				}
			} else {
				// Evaluating final authentication response.
				var authRes = util.md5(ha1 + ":" + co.nonce + ":" + ha2);
				authenticated = (authRes == co.response);
			}
		}
	}

	console.log(authenticated);

	return authenticated;
}
/**
 * Asks client for authentication.
 *
 * @param {Request} request HTTP request object.
 * @param {Response} response HTTP response object.
 * @param {String} requestBody HTTP request body string.<commit_msg>Logging added to check auth-int.<commit_after>/**
 * Default setup module.
 */
var defaults = require('../default');

/**
 * Utility module.
 */
var util = require('../util');

/**
 * Module for generating unique id.
 */
var uuid = require('node-uuid');

/**
 * Exporting module.
 */
module.exports = Digest;

/**
 * Digest Access Authentication.
 *
 * @param {String} authRealm authentication realm.
 * @param {Array} authUsers array of users.
 */
function Digest(authRealm, authUsers) {
	// Realm.
	this.realm = authRealm;
	// Users.
	this.users = authUsers;
	// Nonces.
	this.nonces = new Array();

	// Used for async callback.
	var self = this;

	/**
	 * Applies digest authentication and calls next after user is authenticated.
	 *
	 * @param {Request} request HTTP request object.
	 * @param {Response} response HTTP response object.
	 * @param {Function} next function(request, response) that will be called after user is authenticated.
	 */
	this.apply = function(request, response, next) {
		var requestBody = "";

		// Reading request body.
		request.on('data', function(chunk) {
			// append the current chunk of data to the requestBody variable.
			if(request.method == "POST") {
				requestBody += chunk.toString();
			}
		});
		// Processing authentication part.
		request.on('end', function() {
			var authenticated = self.isAuthenticated(request, response, requestBody);
			if(!authenticated) {
				self.ask(request, response, requestBody);
			} else {
				next(request, response);
			}
		});
	}
}

/**
 * Checks authorization header in request.
 *
 * @param {Request} request HTTP request object.
 * @param {Response} response HTTP response object.
 * @param {String} requestBody HTTP request body string.
 */
Digest.prototype.isAuthenticated = function(request, response, requestBody) {
	var authenticated = false;

	console.log(request.headers.authorization);

	// If header exists.
	if("authorization" in request.headers) {
		var header = request.headers.authorization;
		var co = this.parseAuthHeader(header);

		// Check for expiration.
		if(co.nonce in this.nonces) {
			// Sencond hash in digest access authentication.
			var ha2;
			// Calculating second hash.
			if(co.qop == "auth-int") {
				console.log(request.method + " {" + requestBody + "}");
				ha2 = util.md5(request.method + ":" + co.uri + ":" + util.md5(requestBody));
			} else {
				ha2 = util.md5(request.method + ":" + co.uri);
			}

			// Checking response for username.
			var ha1 = util.md5(this.users[co.username]);
			// If qop is specified.
			if(co.qop) {
				if(co.nc > this.nonces[co.nonce]) {
					// Updating nonce count.
					this.nonces[co.nonce] = co.nc;

					// Evaluating final authentication response.
					var authRes = util.md5(ha1 + ":" + co.nonce + ":" + co.nc + ":" + co.cnonce + ":" + co.qop + ":" + ha2);
					console.log("OUR: " + authRes);
					console.log("NOUR: " + co.response);
					
					authenticated = (authRes == co.response);
				}
			} else {
				// Evaluating final authentication response.
				var authRes = util.md5(ha1 + ":" + co.nonce + ":" + ha2);
				authenticated = (authRes == co.response);
			}
		}
	}

	console.log(authenticated);

	return authenticated;
}
/**
 * Asks client for authentication.
 *
 * @param {Request} request HTTP request object.
 * @param {Response} response HTTP response object.
 * @param {String} requestBody HTTP request body string.
{'fraction_non_alphanumeric': 0.1469338190649666, 'fraction_numerical': 0.004250151791135397, 'mean_word_length': 3.151858853182105, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 5, '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': '5405435', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2049, 'n_words': 565}