]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/webfinger.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / helpers / webfinger.ts
CommitLineData
e4f97bab 1import * as WebFinger from 'webfinger.js'
892211e8 2import { WebFingerData } from '../../shared'
0f91ae62 3import { fetchRemoteAccount } from '../lib/activitypub/account'
e4f97bab
C
4import { isTestInstance } from './core-utils'
5import { isActivityPubUrlValid } from './custom-validators'
e4f97bab
C
6
7const webfinger = new WebFinger({
8 webfist_fallback: false,
9 tls_only: isTestInstance(),
10 uri_fallback: false,
11 request_timeout: 3000
12})
13
350e31d6
C
14async 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
32export {
33 getAccountFromWebfinger
34}
35
36// ---------------------------------------------------------------------------
37
350e31d6 38function 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}