<commit_before>"use strict";
var inherits = require('util').inherits;

var errors = require('../errors');
var HandlerSync = require('./core/handler_sync');


var UnexpectedAuth = function () {
};
inherits(UnexpectedAuth, errors.Conflict);

UnexpectedAuth.prototype.name = 'UnexpectedAuth';

UnexpectedAuth.prototype.getMessage = function () {
	return this.status + ' Unexpected Auth Identity';
};


var ChildContextAuthProhibited = function () {
};
inherits(ChildContextAuthProhibited, errors.Conflict);

ChildContextAuthProhibited.prototype.name = 'ChildContextAuthProhibited';

ChildContextAuthProhibited.prototype.getMessage = function () {
	return this.status + ' Child Context Authentication Prohibited';
};


var Auth = function (tokener, isAuthOptional) {
	this.tokener = tokener;
	this.isAuthOptional = !!isAuthOptional;
};
inherits(Auth, HandlerSync);

Auth.prototype.name = 'Auth';

Auth.prototype.extractData = function (ctx) {
	var result;
	var headers = ctx.mechanicsCtx.headers;
	if (headers != null && headers.auth != null) {
		result = {
			token: headers.auth,
			expectedIdentity: headers.authExpected
		};
	}
	else {
		var req = ctx.mechanicsCtx.req;
		if (req != null) {
			result = this.tokener.getAuthData(req);
		}
	}
	return result;
};

Auth.prototype.handleRequest = function (ctx) {
	var auth = null;
	var parentCtx = ctx.mechanicsCtx.parentCtx;

	var data = this.extractData(ctx);
	if (data != null) {
		if (parentCtx != null) {
			throw new ChildContextAuthProhibited();
		}

		var authResult = this.tokener.auth(data.token, data.additionalToken, data.expectedIdentity, ctx.mechanicsCtx.res == null);
		if (authResult != null) {
			if (authResult.unexpectedIdentity) {
				throw new UnexpectedAuth();
			}

			var identity = authResult.auth.identity;
			var renewal = authResult.renewal;

			if (renewal != null) {
				this.applyRenewal(ctx, renewal);
			}

			auth = {
				identity: identity
			};
		}
	}
	else if (parentCtx != null && parentCtx.auth != null) {
		auth = parentCtx.auth;
	}

	if (auth != null) {
		ctx.auth = auth;
	}
	else if (!this.isAuthOptional) {
		throw new errors.AuthRequired();
	}
};

Auth.prototype.applyRenewal = function (ctx, renewal) {
	var res = ctx.mechanicsCtx.res;
	if (res != null) {
		this.tokener.applyRenewal(res, renewal);
	}
	else {
		ctx.mechanicsCtx.responseHeaders.authRenewal = renewal.result;
	}
};

Auth.auth = function (tokener) {
	var result = new Auth(tokener);
	result.opt = new Auth(tokener, true);
	return result;
};

Auth.UnexpectedAuth = UnexpectedAuth;

Auth.ChildContextAuthProhibited = ChildContextAuthProhibited;


module.exports = Auth;<commit_msg>introduced some methods to use in children classes<commit_after>"use strict";
var inherits = require('util').inherits;

var errors = require('../errors');
var HandlerSync = require('./core/handler_sync');


var UnexpectedAuth = function () {
};
inherits(UnexpectedAuth, errors.Conflict);

UnexpectedAuth.prototype.name = 'UnexpectedAuth';

UnexpectedAuth.prototype.getMessage = function () {
	return this.status + ' Unexpected Auth Identity';
};


var ChildContextAuthProhibited = function () {
};
inherits(ChildContextAuthProhibited, errors.Conflict);

ChildContextAuthProhibited.prototype.name = 'ChildContextAuthProhibited';

ChildContextAuthProhibited.prototype.getMessage = function () {
	return this.status + ' Child Context Authentication Prohibited';
};


var Auth = function (tokener, isAuthOptional) {
	this.tokener = tokener;
	this.isAuthOptional = !!isAuthOptional;
};
inherits(Auth, HandlerSync);

Auth.prototype.name = 'Auth';

Auth.prototype.extractData = function (ctx) {
	var result;
	var headers = ctx.mechanicsCtx.headers;
	if (headers != null && headers.auth != null) {
		result = {
			token: headers.auth,
			expectedIdentity: headers.authExpected
		};
	}
	else {
		var req = ctx.mechanicsCtx.req;
		if (req != null) {
			result = this.tokener.getAuthData(req);
		}
	}
	return result;
};

Auth.prototype.handleRequest = function (ctx) {
	var auth = null;
	var parentCtx = ctx.mechanicsCtx.parentCtx;

	var data = this.extractData(ctx);
	if (data != null) {
		if (parentCtx != null) {
			throw new ChildContextAuthProhibited();
		}

		var authResult = this.tokener.auth(data.token, data.additionalToken, data.expectedIdentity, ctx.mechanicsCtx.res == null);
		if (authResult != null) {
			if (authResult.unexpectedIdentity) {
				throw new UnexpectedAuth();
			}

			this.checkAuthResult(ctx, authResult);

			var identity = authResult.auth.identity;
			var renewal = authResult.renewal;

			if (renewal != null) {
				this.applyRenewal(ctx, renewal);
			}

			auth = this.createCtxAuth(ctx, authResult.auth);
		}
	}
	else if (parentCtx != null && parentCtx.auth != null) {
		auth = parentCtx.auth;
		this.checkCtxAuth(ctx, auth);
	}

	if (auth != null) {
		ctx.auth = auth;
	}
	else if (!this.isAuthOptional) {
		throw new errors.AuthRequired();
	}
};

Auth.prototype.checkAuthResult = function (ctx, authResult) {
};

Auth.prototype.checkCtxAuth = function (ctx, auth) {
};

Auth.prototype.createCtxAuth = function (ctx, token) {
	return { identity: token.identity };
};

Auth.prototype.applyRenewal = function (ctx, renewal) {
	var res = ctx.mechanicsCtx.res;
	if (res != null) {
		this.tokener.applyRenewal(res, renewal);
	}
	else {
		ctx.mechanicsCtx.responseHeaders.authRenewal = renewal.result;
	}
};

Auth.auth = function (tokener) {
	var result = new Auth(tokener);
	result.opt = new Auth(tokener, true);
	return result;
};

Auth.UnexpectedAuth = UnexpectedAuth;

Auth.ChildContextAuthProhibited = ChildContextAuthProhibited;


module.exports = Auth;
{'fraction_non_alphanumeric': 0.13178984861976847, 'fraction_numerical': 0.0, 'mean_word_length': 4.389635316698657, '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': '5176021', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1842, 'n_words': 395}