2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { URL } from 'node:url';
|
2023-08-05 03:33:00 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2024-07-09 04:31:25 +02:00
|
|
|
import { XMLParser } from 'fast-xml-parser';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-12-04 09:05:32 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2024-09-06 13:45:00 +02:00
|
|
|
import type Logger from '@/logger.js';
|
2024-09-05 19:29:03 +02:00
|
|
|
import { RemoteLoggerService } from './RemoteLoggerService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-19 06:01:14 +02:00
|
|
|
export type ILink = {
|
2022-09-17 20:27:08 +02:00
|
|
|
href: string;
|
|
|
|
rel?: string;
|
|
|
|
};
|
|
|
|
|
2023-07-19 06:01:14 +02:00
|
|
|
export type IWebFinger = {
|
2022-09-17 20:27:08 +02:00
|
|
|
links: ILink[];
|
|
|
|
subject: string;
|
|
|
|
};
|
|
|
|
|
2023-05-18 11:45:49 +02:00
|
|
|
const urlRegex = /^https?:\/\//;
|
|
|
|
const mRegex = /^([^@]+)@(.*)/;
|
|
|
|
|
2024-08-06 20:07:01 +02:00
|
|
|
// we have the colons here, because URL.protocol does as well, so it's
|
|
|
|
// more uniform in the places we use both
|
|
|
|
const defaultProtocol = process.env.MISSKEY_WEBFINGER_USE_HTTP?.toLowerCase() === 'true' ? 'http:' : 'https:';
|
2024-07-12 14:46:23 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export class WebfingerService {
|
2024-09-05 19:29:03 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
private httpRequestService: HttpRequestService,
|
2024-09-05 19:29:03 +02:00
|
|
|
private remoteLoggerService: RemoteLoggerService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
2024-09-05 19:29:03 +02:00
|
|
|
this.logger = this.remoteLoggerService.logger.createSubLogger('webfinger');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async webfinger(query: string): Promise<IWebFinger> {
|
2024-07-09 04:31:25 +02:00
|
|
|
const hostMetaUrl = this.queryToHostMetaUrl(query);
|
2024-07-14 15:11:02 +02:00
|
|
|
const template = await this.fetchWebFingerTemplateFromHostMeta(hostMetaUrl) ?? this.queryToWebFingerTemplate(query);
|
2024-07-09 04:31:25 +02:00
|
|
|
const url = this.genUrl(query, template);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-01-12 13:03:02 +01:00
|
|
|
return await this.httpRequestService.getJson<IWebFinger>(url, 'application/jrd+json, application/json');
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-07-09 04:31:25 +02:00
|
|
|
private genUrl(query: string, template: string): string {
|
|
|
|
if (template.indexOf('{uri}') < 0) throw new Error(`Invalid webFingerUrl: ${template}`);
|
|
|
|
|
2024-07-12 13:28:19 +02:00
|
|
|
if (query.match(urlRegex)) {
|
2024-07-09 04:31:25 +02:00
|
|
|
return template.replace('{uri}', encodeURIComponent(query));
|
|
|
|
}
|
|
|
|
|
2024-07-12 13:28:19 +02:00
|
|
|
const m = query.match(mRegex);
|
2024-07-09 04:31:25 +02:00
|
|
|
if (m) {
|
|
|
|
return template.replace('{uri}', encodeURIComponent(`acct:${query}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Invalid query (${query})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
private queryToWebFingerTemplate(query: string): string {
|
2024-07-12 13:28:19 +02:00
|
|
|
if (query.match(urlRegex)) {
|
2024-07-09 04:31:25 +02:00
|
|
|
const u = new URL(query);
|
2024-07-12 14:46:23 +02:00
|
|
|
return `${u.protocol}//${u.hostname}/.well-known/webfinger?resource={uri}`;
|
2024-07-09 04:31:25 +02:00
|
|
|
}
|
|
|
|
|
2024-07-12 13:28:19 +02:00
|
|
|
const m = query.match(mRegex);
|
2024-07-09 04:31:25 +02:00
|
|
|
if (m) {
|
|
|
|
const hostname = m[2];
|
2024-07-12 14:46:23 +02:00
|
|
|
return `${defaultProtocol}//${hostname}/.well-known/webfinger?resource={uri}`;
|
2024-07-09 04:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Invalid query (${query})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
private queryToHostMetaUrl(query: string): string {
|
2023-05-18 11:45:49 +02:00
|
|
|
if (query.match(urlRegex)) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const u = new URL(query);
|
2024-07-12 14:46:23 +02:00
|
|
|
return `${u.protocol}//${u.hostname}/.well-known/host-meta`;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 11:45:49 +02:00
|
|
|
const m = query.match(mRegex);
|
2022-09-17 20:27:08 +02:00
|
|
|
if (m) {
|
|
|
|
const hostname = m[2];
|
2024-08-06 20:07:01 +02:00
|
|
|
return `${defaultProtocol}//${hostname}/.well-known/host-meta`;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Invalid query (${query})`);
|
|
|
|
}
|
2024-07-09 04:31:25 +02:00
|
|
|
|
|
|
|
@bindThis
|
2024-07-14 15:11:02 +02:00
|
|
|
private async fetchWebFingerTemplateFromHostMeta(url: string): Promise<string | null> {
|
2024-07-09 04:31:25 +02:00
|
|
|
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) {
|
2024-09-05 19:29:03 +02:00
|
|
|
this.logger.error(`error while request host-meta for ${url}: ${err}`);
|
2024-07-09 04:31:25 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|