1
0
forked from mirror/misskey
misskey/src/remote/webfinger.ts

39 lines
741 B
TypeScript
Raw Normal View History

2018-03-31 19:55:00 +09:00
const WebFinger = require('webfinger.js');
2018-04-01 23:33:48 +09:00
const webFinger = new WebFinger({ });
2018-03-31 19:55:00 +09:00
type ILink = {
href: string;
rel: string;
2018-04-01 21:24:25 +09:00
};
2018-03-31 19:55:00 +09:00
type IWebFinger = {
2018-04-01 21:24:25 +09:00
links: ILink[];
2018-03-31 19:55:00 +09:00
subject: string;
2018-04-01 21:24:25 +09:00
};
2018-03-31 19:55:00 +09:00
2018-04-02 18:36:47 +09:00
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;
2018-04-02 19:37:00 +09:00
const subject = finger.subject.toLowerCase().replace(/^acct:/, '');
2018-04-02 18:36:47 +09:00
2018-04-02 19:37:00 +09:00
if (typeof verifier === 'string') {
if (subject !== verifier) {
2018-04-03 12:28:34 +09:00
throw new Error();
2018-04-02 18:36:47 +09:00
}
2018-03-31 19:55:00 +09:00
2018-04-02 18:36:47 +09:00
return finger;
2018-03-31 19:55:00 +09:00
}
2018-04-02 19:37:00 +09:00
if (typeof subject === 'string') {
return resolve(subject, subject);
}
2018-04-03 12:28:34 +09:00
throw new Error();
2018-04-02 18:36:47 +09:00
}