egirlskey/src/remote/webfinger.ts

34 lines
683 B
TypeScript
Raw Normal View History

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