<commit_before>const WebFinger = require('webfinger.js');

const webFinger = new WebFinger({ });

type ILink = {
  href: string;
  rel: string;
};

type IWebFinger = {
  links: ILink[];
  subject: string;
};

export default async function resolve(query, verifier?: string): Promise<IWebFinger> {
	const finger = await new Promise((res, rej) => webFinger.lookup(query, (error, result) => {
		if (error) {
			return rej(error);
		}

		res(result.object);
	})) as IWebFinger;

	if (verifier) {
		if (finger.subject.toLowerCase().replace(/^acct:/, '') !== verifier) {
			throw 'WebFinger verfification failed';
		}

		return finger;
	}

	return resolve(finger.subject, finger.subject.toLowerCase());
}<commit_msg>Improve WebFinger verification<commit_after>const WebFinger = require('webfinger.js');

const webFinger = new WebFinger({ });

type ILink = {
  href: string;
  rel: string;
};

type IWebFinger = {
  links: ILink[];
  subject: string;
};

export default async function resolve(query, verifier?: string): Promise<IWebFinger> {
	const finger = await new Promise((res, rej) => webFinger.lookup(query, (error, result) => {
		if (error) {
			return rej(error);
		}

		res(result.object);
	})) as IWebFinger;
	const subject = finger.subject.toLowerCase().replace(/^acct:/, '');

	if (typeof verifier === 'string') {
		if (subject !== verifier) {
			throw new Error;
		}

		return finger;
	}

	if (typeof subject === 'string') {
		return resolve(subject, subject);
	}

	throw new Error;
}
{'fraction_non_alphanumeric': 0.1584956346541303, 'fraction_numerical': 0.0, 'mean_word_length': 4.283687943262412, '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': '3272541', 'n_tokens_mistral': 534, 'n_tokens_neox': 507, 'n_words': 127}