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