<commit_before>const reqlib = require('app-root-path').require;

const { BadRequest } = reqlib('src/errors');
const oauth2 = require('../../oauth2/hooks');

module.exports = {
	disableFilter: (context) => {
		context.safeAttributes = ['username'];
		return context;
	},

	tokenIsActive: (context) =>
		context.app
			.service('/oauth2/introspect')
			.create({ token: context.params.headers.authorization.replace('Bearer ', '') })
			.then((introspection) => {
				if (introspection.active) {
					context.params.tokenInfo = introspection;
					return context;
				}
				throw new BadRequest('Access token invalid');
			})
			.catch((error) => {
				throw new Error(error);
			}),

	userIsMatching: (context) => {
		if (
			context.params.tokenInfo.obfuscated_subject === decodeURIComponent(context.params.route.user) ||
			context.params.tokenInfo.sub === context.params.user
		) {
			return context;
		}
		throw new BadRequest('No permissions for the user');
	},

	stripIframe: (context) => {
		const regEx = /oauth2\/username\/(.*?)"/;
		const pseudonym = context.params.route.user;
		context.params.pseudonym = pseudonym.includes('iframe') ? pseudonym.match(regEx)[1] : pseudonym;
		return context;
	},

	injectOriginToolIds: (context) => {
		if (!context.params.tokenInfo) throw new Error('Token info is missing in params'); // first call isTokenActive
		const toolService = context.app.service('ltiTools');
		return toolService
			.find({
				query: {
					oAuthClientId: context.params.tokenInfo.client_id,
					isLocal: true,
				},
			})
			.then((originTools) =>
				toolService
					.find({
						query: {
							originTool: originTools.data[0]._id,
						},
					})
					.then((tools) => {
						context.params.toolIds = [originTools.data[0]._id]; // don't forget actual requested tool id
						context.params.toolIds = context.params.toolIds.concat(tools.data.map((tool) => tool._id));
						return context;
					})
			);
	},

	groupContainsUser: (context) => {
		if (!context.result.data) return context;
		const users = context.result.data.students.concat(context.result.data.teachers);
		if (
			users.find(
				(user) =>
					user.user_id ===
						oauth2.getSubject(context.params.tokenInfo.obfuscated_subject, context.app.settings.services.web) ||
					user.user_id === context.params.tokenInfo.obfuscated_subject
			)
		)
			return context;
		throw new BadRequest('Current user is not part of group');
	},

};
<commit_msg>codacy<commit_after>const reqlib = require('app-root-path').require;

const { BadRequest } = reqlib('src/errors');
const oauth2 = require('../../oauth2/hooks');

module.exports = {
	tokenIsActive: (context) =>
		context.app
			.service('/oauth2/introspect')
			.create({ token: context.params.headers.authorization.replace('Bearer ', '') })
			.then((introspection) => {
				if (introspection.active) {
					context.params.tokenInfo = introspection;
					return context;
				}
				throw new BadRequest('Access token invalid');
			})
			.catch((error) => {
				throw new Error(error);
			}),

	disableFilter: (context) => {
		context.safeAttributes = ['username'];
		return context;
	},

	userIsMatching: (context) => {
		if (
			context.params.tokenInfo.obfuscated_subject === decodeURIComponent(context.params.route.user) ||
			context.params.tokenInfo.sub === context.params.user
		) {
			return context;
		}
		throw new BadRequest('No permissions for the user');
	},

	stripIframe: (context) => {
		const regEx = /oauth2\/username\/(.*?)"/;
		const pseudonym = context.params.route.user;
		context.params.pseudonym = pseudonym.includes('iframe') ? pseudonym.match(regEx)[1] : pseudonym;
		return context;
	},

	injectOriginToolIds: (context) => {
		if (!context.params.tokenInfo) throw new Error('Token info is missing in params'); // first call isTokenActive
		const toolService = context.app.service('ltiTools');
		return toolService
			.find({
				query: {
					oAuthClientId: context.params.tokenInfo.client_id,
					isLocal: true,
				},
			})
			.then((originTools) =>
				toolService
					.find({
						query: {
							originTool: originTools.data[0]._id,
						},
					})
					.then((tools) => {
						context.params.toolIds = [originTools.data[0]._id]; // don't forget actual requested tool id
						context.params.toolIds = context.params.toolIds.concat(tools.data.map((tool) => tool._id));
						return context;
					})
			);
	},

	groupContainsUser: (context) => {
		if (!context.result.data) return context;
		const users = context.result.data.students.concat(context.result.data.teachers);
		if (
			users.find(
				(user) =>
					user.user_id ===
						oauth2.getSubject(context.params.tokenInfo.obfuscated_subject, context.app.settings.services.web) ||
					user.user_id === context.params.tokenInfo.obfuscated_subject
			)
		)
			return context;
		throw new BadRequest('Current user is not part of group');
	},
};

{'fraction_non_alphanumeric': 0.1546349466776046, 'fraction_numerical': 0.003281378178835111, 'mean_word_length': 3.4255898366606172, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '5089472', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1558, 'n_words': 286}