<commit_before>import * as express from 'express';
import App from './models/app';
import User from './models/user';
import Userkey from './models/userkey';

export interface IAuthContext {
	/**
	 * App which requested
	 */
	app: any;

	/**
	 * Authenticated user
	 */
	user: any;

	/**
	 * Weather if the request is via the (Misskey Web Client or user direct) or not
	 */
	isSecure: boolean;
}

export default (req: express.Request) =>
	new Promise<IAuthContext>(async (resolve, reject) => {
	const webToken = req.body['i'];
	if (webToken) {
		const user = await User
			.findOne({ token: webToken });

		if (user === null) {
			return reject('user not found');
		}

		return resolve({
			app: null,
			user: user,
			isSecure: true
		});
	}

	const userkey = req.headers['userkey'] || req.body['_userkey'];
	if (userkey) {
		const userkeyDoc = await Userkey.findOne({
			key: userkey
		});

		if (userkeyDoc === null) {
			return reject('invalid userkey');
		}

		const app = await App
			.findOne({ _id: userkeyDoc.app_id });
<commit_msg>Refactor related to #53<commit_after>import * as express from 'express';
import App from './models/app';
import User from './models/user';
import Userkey from './models/userkey';

export interface IAuthContext {
	/**
	 * App which requested
	 */
	app: any;

	/**
	 * Authenticated user
	 */
	user: any;

	/**
	 * Weather if the request is via the (Misskey Web Client or user direct) or not
	 */
	isSecure: boolean;
}

export default (req: express.Request) =>
	new Promise<IAuthContext>(async (resolve, reject) => {
	const token = req.body['i'];
	if (token) {
		const user = await User
			.findOne({ token: token });

		if (user === null) {
			return reject('user not found');
		}

		return resolve({
			app: null,
			user: user,
			isSecure: true
		});
	}

	const userkey = req.headers['userkey'] || req.body['_userkey'];
	if (userkey) {
		const userkeyDoc = await Userkey.findOne({
			key: userkey
		});

		if (userkeyDoc === null) {
			return reject('invalid userkey');
		}

		const app = await App
			.findOne({ _id: userkeyDoc.app_id });

{'fraction_non_alphanumeric': 0.14930388862217955, 'fraction_numerical': 0.0009601536245799327, 'mean_word_length': 3.3057851239669422, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5413699', 'n_tokens_mistral': 764, 'n_tokens_neox': 682, 'n_words': 215}