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