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
@ -112,6 +112,7 @@
|
|||||||
"content-disposition": "0.5.4",
|
"content-disposition": "0.5.4",
|
||||||
"date-fns": "2.30.0",
|
"date-fns": "2.30.0",
|
||||||
"deep-email-validator": "0.1.21",
|
"deep-email-validator": "0.1.21",
|
||||||
|
"fast-xml-parser": "^4.4.0",
|
||||||
"fastify": "4.26.2",
|
"fastify": "4.26.2",
|
||||||
"fastify-multer": "^2.0.3",
|
"fastify-multer": "^2.0.3",
|
||||||
"fastify-raw-body": "4.3.0",
|
"fastify-raw-body": "4.3.0",
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import { Injectable } from '@nestjs/common';
|
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 { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
|
|
||||||
@ -22,6 +22,8 @@ export type IWebFinger = {
|
|||||||
const urlRegex = /^https?:\/\//;
|
const urlRegex = /^https?:\/\//;
|
||||||
const mRegex = /^([^@]+)@(.*)/;
|
const mRegex = /^([^@]+)@(.*)/;
|
||||||
|
|
||||||
|
const defaultProtocol = process.env.MISSKEY_WEBFINGER_USE_HTTP?.toLowerCase() === 'true' ? 'http' : 'https';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WebfingerService {
|
export class WebfingerService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -31,25 +33,76 @@ export class WebfingerService {
|
|||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public async webfinger(query: string): Promise<IWebFinger> {
|
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');
|
return await this.httpRequestService.getJson<IWebFinger>(url, 'application/jrd+json, application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@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)) {
|
if (query.match(urlRegex)) {
|
||||||
const u = new URL(query);
|
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);
|
const m = query.match(mRegex);
|
||||||
if (m) {
|
if (m) {
|
||||||
const hostname = m[2];
|
const hostname = m[2];
|
||||||
const useHttp = process.env.MISSKEY_WEBFINGER_USE_HTTP && process.env.MISSKEY_WEBFINGER_USE_HTTP.toLowerCase() === 'true';
|
return `${defaultProtocol}//${hostname}/.well-known/webfinger?resource={uri}`;
|
||||||
return `http${useHttp ? '' : 's'}://${hostname}/.well-known/webfinger?${urlQuery({ resource: `acct:${query}` })}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Invalid query (${query})`);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -220,6 +220,9 @@ importers:
|
|||||||
deep-email-validator:
|
deep-email-validator:
|
||||||
specifier: 0.1.21
|
specifier: 0.1.21
|
||||||
version: 0.1.21
|
version: 0.1.21
|
||||||
|
fast-xml-parser:
|
||||||
|
specifier: ^4.4.0
|
||||||
|
version: 4.4.0
|
||||||
fastify:
|
fastify:
|
||||||
specifier: 4.26.2
|
specifier: 4.26.2
|
||||||
version: 4.26.2
|
version: 4.26.2
|
||||||
@ -6821,6 +6824,10 @@ packages:
|
|||||||
resolution: {integrity: sha512-B9/wizE4WngqQftFPmdaMYlXoJlJOYxGQOanC77fq9k8+Z0v5dDSVh+3glErdIROP//s/jgb7ZuxKfB8nVyo0g==}
|
resolution: {integrity: sha512-B9/wizE4WngqQftFPmdaMYlXoJlJOYxGQOanC77fq9k8+Z0v5dDSVh+3glErdIROP//s/jgb7ZuxKfB8nVyo0g==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
|
||||||
|
fast-xml-parser@4.4.0:
|
||||||
|
resolution: {integrity: sha512-kLY3jFlwIYwBNDojclKsNAC12sfD6NwW74QB2CoNGPvtVxjliYehVunB3HYyNi+n4Tt1dAcgwYvmKF/Z18flqg==}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
fastify-multer@2.0.3:
|
fastify-multer@2.0.3:
|
||||||
resolution: {integrity: sha512-QnFqrRgxmUwWHTgX9uyQSu0C/hmVCfcxopqjApZ4uaZD5W9MJ+nHUlW4+9q7Yd3BRxDIuHvgiM5mjrh6XG8cAA==}
|
resolution: {integrity: sha512-QnFqrRgxmUwWHTgX9uyQSu0C/hmVCfcxopqjApZ4uaZD5W9MJ+nHUlW4+9q7Yd3BRxDIuHvgiM5mjrh6XG8cAA==}
|
||||||
engines: {node: '>=10.17.0'}
|
engines: {node: '>=10.17.0'}
|
||||||
@ -11105,8 +11112,8 @@ packages:
|
|||||||
vue-component-type-helpers@2.0.16:
|
vue-component-type-helpers@2.0.16:
|
||||||
resolution: {integrity: sha512-qisL/iAfdO++7w+SsfYQJVPj6QKvxp4i1MMxvsNO41z/8zu3KuAw9LkhKUfP/kcOWGDxESp+pQObWppXusejCA==}
|
resolution: {integrity: sha512-qisL/iAfdO++7w+SsfYQJVPj6QKvxp4i1MMxvsNO41z/8zu3KuAw9LkhKUfP/kcOWGDxESp+pQObWppXusejCA==}
|
||||||
|
|
||||||
vue-component-type-helpers@2.0.19:
|
vue-component-type-helpers@2.0.26:
|
||||||
resolution: {integrity: sha512-cN3f1aTxxKo4lzNeQAkVopswuImUrb5Iurll9Gaw5cqpnbTAxtEMM1mgi6ou4X79OCyqYv1U1mzBHJkzmiK82w==}
|
resolution: {integrity: sha512-sO9qQ8oC520SW6kqlls0iqDak53gsTVSrYylajgjmkt1c0vcgjsGSy1KzlDrbEx8pm02IEYhlUkU5hCYf8rwtg==}
|
||||||
|
|
||||||
vue-demi@0.14.7:
|
vue-demi@0.14.7:
|
||||||
resolution: {integrity: sha512-EOG8KXDQNwkJILkx/gPcoL/7vH+hORoBaKgGe+6W7VFMvCYJfmF2dGbvgDroVnI8LU7/kTu8mbjRZGBU1z9NTA==}
|
resolution: {integrity: sha512-EOG8KXDQNwkJILkx/gPcoL/7vH+hORoBaKgGe+6W7VFMvCYJfmF2dGbvgDroVnI8LU7/kTu8mbjRZGBU1z9NTA==}
|
||||||
@ -15449,7 +15456,7 @@ snapshots:
|
|||||||
ts-dedent: 2.2.0
|
ts-dedent: 2.2.0
|
||||||
type-fest: 2.19.0
|
type-fest: 2.19.0
|
||||||
vue: 3.4.26(typescript@5.4.5)
|
vue: 3.4.26(typescript@5.4.5)
|
||||||
vue-component-type-helpers: 2.0.19
|
vue-component-type-helpers: 2.0.26
|
||||||
transitivePeerDependencies:
|
transitivePeerDependencies:
|
||||||
- encoding
|
- encoding
|
||||||
- supports-color
|
- supports-color
|
||||||
@ -18827,6 +18834,10 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
strnum: 1.0.5
|
strnum: 1.0.5
|
||||||
|
|
||||||
|
fast-xml-parser@4.4.0:
|
||||||
|
dependencies:
|
||||||
|
strnum: 1.0.5
|
||||||
|
|
||||||
fastify-multer@2.0.3:
|
fastify-multer@2.0.3:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@fastify/busboy': 1.2.1
|
'@fastify/busboy': 1.2.1
|
||||||
@ -19708,7 +19719,7 @@ snapshots:
|
|||||||
|
|
||||||
is-svg@5.0.0:
|
is-svg@5.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
fast-xml-parser: 4.2.5
|
fast-xml-parser: 4.4.0
|
||||||
|
|
||||||
is-symbol@1.0.4:
|
is-symbol@1.0.4:
|
||||||
dependencies:
|
dependencies:
|
||||||
@ -23644,7 +23655,7 @@ snapshots:
|
|||||||
|
|
||||||
vue-component-type-helpers@2.0.16: {}
|
vue-component-type-helpers@2.0.16: {}
|
||||||
|
|
||||||
vue-component-type-helpers@2.0.19: {}
|
vue-component-type-helpers@2.0.26: {}
|
||||||
|
|
||||||
vue-demi@0.14.7(vue@3.4.26(typescript@5.4.5)):
|
vue-demi@0.14.7(vue@3.4.26(typescript@5.4.5)):
|
||||||
dependencies:
|
dependencies:
|
||||||
|
Loading…
Reference in New Issue
Block a user