merge: [feat] port fetching host-meta before the webfinger endpoint from iceshrimp - fixes #539 (!567)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/567 Closes #539 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <marie@kaifa.ch>
This commit is contained in:
commit
4d0a340620
3 changed files with 76 additions and 11 deletions
|
@ -112,6 +112,7 @@
|
|||
"content-disposition": "0.5.4",
|
||||
"date-fns": "2.30.0",
|
||||
"deep-email-validator": "0.1.21",
|
||||
"fast-xml-parser": "^4.4.0",
|
||||
"fastify": "4.26.2",
|
||||
"fastify-multer": "^2.0.3",
|
||||
"fastify-raw-body": "4.3.0",
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
import { URL } from 'node:url';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { query as urlQuery } from '@/misc/prelude/url.js';
|
||||
import { XMLParser } from 'fast-xml-parser';
|
||||
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
|
||||
|
@ -22,6 +22,8 @@ export type IWebFinger = {
|
|||
const urlRegex = /^https?:\/\//;
|
||||
const mRegex = /^([^@]+)@(.*)/;
|
||||
|
||||
const defaultProtocol = process.env.MISSKEY_WEBFINGER_USE_HTTP?.toLowerCase() === 'true' ? 'http' : 'https';
|
||||
|
||||
@Injectable()
|
||||
export class WebfingerService {
|
||||
constructor(
|
||||
|
@ -31,25 +33,76 @@ export class WebfingerService {
|
|||
|
||||
@bindThis
|
||||
public async webfinger(query: string): Promise<IWebFinger> {
|
||||
const url = this.genUrl(query);
|
||||
const hostMetaUrl = this.queryToHostMetaUrl(query);
|
||||
const template = await this.fetchWebFingerTemplateFromHostMeta(hostMetaUrl) ?? this.queryToWebFingerTemplate(query);
|
||||
const url = this.genUrl(query, template);
|
||||
|
||||
return await this.httpRequestService.getJson<IWebFinger>(url, 'application/jrd+json, application/json');
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private genUrl(query: string): string {
|
||||
private genUrl(query: string, template: string): string {
|
||||
if (template.indexOf('{uri}') < 0) throw new Error(`Invalid webFingerUrl: ${template}`);
|
||||
|
||||
if (query.match(urlRegex)) {
|
||||
return template.replace('{uri}', encodeURIComponent(query));
|
||||
}
|
||||
|
||||
const m = query.match(mRegex);
|
||||
if (m) {
|
||||
return template.replace('{uri}', encodeURIComponent(`acct:${query}`));
|
||||
}
|
||||
|
||||
throw new Error(`Invalid query (${query})`);
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private queryToWebFingerTemplate(query: string): string {
|
||||
if (query.match(urlRegex)) {
|
||||
const u = new URL(query);
|
||||
return `${u.protocol}//${u.hostname}/.well-known/webfinger?` + urlQuery({ resource: query });
|
||||
return `${u.protocol}//${u.hostname}/.well-known/webfinger?resource={uri}`;
|
||||
}
|
||||
|
||||
const m = query.match(mRegex);
|
||||
if (m) {
|
||||
const hostname = m[2];
|
||||
const useHttp = process.env.MISSKEY_WEBFINGER_USE_HTTP && process.env.MISSKEY_WEBFINGER_USE_HTTP.toLowerCase() === 'true';
|
||||
return `http${useHttp ? '' : 's'}://${hostname}/.well-known/webfinger?${urlQuery({ resource: `acct:${query}` })}`;
|
||||
return `${defaultProtocol}//${hostname}/.well-known/webfinger?resource={uri}`;
|
||||
}
|
||||
|
||||
throw new Error(`Invalid query (${query})`);
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private queryToHostMetaUrl(query: string): string {
|
||||
if (query.match(urlRegex)) {
|
||||
const u = new URL(query);
|
||||
return `${u.protocol}//${u.hostname}/.well-known/host-meta`;
|
||||
}
|
||||
|
||||
const m = query.match(mRegex);
|
||||
if (m) {
|
||||
const hostname = m[2];
|
||||
return `${defaultProtocol}://${hostname}/.well-known/host-meta`;
|
||||
}
|
||||
|
||||
throw new Error(`Invalid query (${query})`);
|
||||
}
|
||||
|
||||
@bindThis
|
||||
private async fetchWebFingerTemplateFromHostMeta(url: string): Promise<string | null> {
|
||||
try {
|
||||
const res = await this.httpRequestService.getHtml(url, 'application/xrd+xml');
|
||||
const options = {
|
||||
ignoreAttributes: false,
|
||||
isArray: (_name: string, jpath: string) => jpath === 'XRD.Link',
|
||||
};
|
||||
const parser = new XMLParser(options);
|
||||
const hostMeta = parser.parse(res);
|
||||
const template = (hostMeta['XRD']['Link'] as Array<any>).filter(p => p['@_rel'] === 'lrdd')[0]['@_template'];
|
||||
return template.indexOf('{uri}') < 0 ? null : template;
|
||||
} catch (err) {
|
||||
console.error(`error while request host-meta for ${url}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue