<commit_before>'use strict';

const Application = require('../../documentModels/application');
const ApplicationAccess = require('../../documentModels/applicationAccess');

module.exports = (request, response, next) => {
	(async () => {
		try {
			const route = request.directoryRouter.findRoute(request.method, request.route.path);
			const extensions = route.extensions;

			if ('permissions' in extensions && extensions.permissions.length !== 0) {
				const applicationKey = request.get('X-Application-Key');
				const accessKey = request.get('X-Access-Key');

				if (applicationKey == null) {
					response.status(400).send({error: {message: 'X-Application-Key header is empty'}});
					return;
				}

				if (accessKey == null) {
					response.status(400).send({error: {message: 'X-Access-Key header is empty'}});
					return;
				}

				if (!await Application.verifyKeyAsync(applicationKey, request.db, request.config)) {
					response.status(400).send({error: {message: 'X-Application-Key header is invalid'}});
					return;
				}

				if (!await ApplicationAccess.verifyKeyAsync(accessKey, request.db, request.config)) {
					response.status(400).send({error: {message: 'X-Access-Key header is invalid'}});
					return;
				}

				const applicationId = Application.splitKey(applicationKey, request.db, request.config).applicationId;
				const userId = ApplicationAccess.splitKey(accessKey, request.db, request.config).userId;

				const application = (await request.db.applications.findIdAsync(applicationId));
				request.application = application.document;
				request.user = (await request.db.users.findIdAsync(userId)).document;

				for (const permission of extensions.permissions) {
					if (!application.hasPermission(permission)) {
						response.status(403).send({error: {message: 'you do not have any permissions'}});
						return;
					}
				}

				next();
			}
			else {
				next();
			}
		}
		catch(err) {
			console.log(`checkPermission failed (${err})`);
			throw err;
		}
	})();
};<commit_msg>refactoring<commit_after>'use strict';

const Application = require('../../documentModels/application');
const ApplicationAccess = require('../../documentModels/applicationAccess');

module.exports = (request, response, next) => {
	(async () => {
		try {
			const route = request.directoryRouter.findRoute(request.method, request.route.path);
			const extensions = route.extensions;

			if ('permissions' in extensions && extensions.permissions.length !== 0) {
				const applicationKey = request.get('X-Application-Key');
				const accessKey = request.get('X-Access-Key');

				if (applicationKey == null) {
					response.status(400).send({error: {message: 'X-Application-Key header is empty'}});
					return;
				}

				if (accessKey == null) {
					response.status(400).send({error: {message: 'X-Access-Key header is empty'}});
					return;
				}

				if (!await Application.verifyKeyAsync(applicationKey, request.db, request.config)) {
					response.status(400).send({error: {message: 'X-Application-Key header is invalid'}});
					return;
				}

				if (!await ApplicationAccess.verifyKeyAsync(accessKey, request.db, request.config)) {
					response.status(400).send({error: {message: 'X-Access-Key header is invalid'}});
					return;
				}

				const applicationId = Application.splitKey(applicationKey, request.db, request.config).applicationId;
				const userId = ApplicationAccess.splitKey(accessKey, request.db, request.config).userId;

				request.application = (await request.db.applications.findIdAsync(applicationId));
				request.user = (await request.db.users.findIdAsync(userId));

				const hasPermissions = extensions.permissions.every(p => request.application.hasPermission(p));
				if (!hasPermissions) {
					response.status(403).send({error: {message: 'you do not have any permissions'}});
					return;
				}
			}
			next();
		}
		catch(err) {
			console.log(`checkPermission failed (${err})`);
			throw err;
		}
	})();
};
{'fraction_non_alphanumeric': 0.15270065623422513, 'fraction_numerical': 0.008076728924785462, 'mean_word_length': 3.9047029702970297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '516468', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1140, 'n_words': 236}