]>
Commit | Line | Data |
---|---|---|
e4f97bab | 1 | import * as WebFinger from 'webfinger.js' |
892211e8 | 2 | import { WebFingerData } from '../../shared' |
0f91ae62 | 3 | import { fetchRemoteAccount } from '../lib/activitypub/account' |
e4f97bab C |
4 | import { isTestInstance } from './core-utils' |
5 | import { isActivityPubUrlValid } from './custom-validators' | |
e4f97bab C |
6 | |
7 | const webfinger = new WebFinger({ | |
8 | webfist_fallback: false, | |
9 | tls_only: isTestInstance(), | |
10 | uri_fallback: false, | |
11 | request_timeout: 3000 | |
12 | }) | |
13 | ||
350e31d6 C |
14 | async function getAccountFromWebfinger (nameWithHost: string) { |
15 | const webfingerData: WebFingerData = await webfingerLookup(nameWithHost) | |
e4f97bab | 16 | |
350e31d6 | 17 | if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.') |
e4f97bab C |
18 | |
19 | const selfLink = webfingerData.links.find(l => l.rel === 'self') | |
350e31d6 C |
20 | if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) { |
21 | throw new Error('Cannot find self link or href is not a valid URL.') | |
22 | } | |
e4f97bab | 23 | |
0f91ae62 C |
24 | const account = await fetchRemoteAccount(selfLink.href) |
25 | if (account === undefined) throw new Error('Cannot fetch remote account ' + selfLink.href) | |
e4f97bab | 26 | |
0f91ae62 | 27 | return account |
e4f97bab C |
28 | } |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
32 | export { | |
33 | getAccountFromWebfinger | |
34 | } | |
35 | ||
36 | // --------------------------------------------------------------------------- | |
37 | ||
350e31d6 | 38 | function webfingerLookup (nameWithHost: string) { |
e4f97bab | 39 | return new Promise<WebFingerData>((res, rej) => { |
350e31d6 | 40 | webfinger.lookup(nameWithHost, (err, p) => { |
e4f97bab C |
41 | if (err) return rej(err) |
42 | ||
350e31d6 | 43 | return res(p.object) |
e4f97bab C |
44 | }) |
45 | }) | |
46 | } |