]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/webfinger.ts
Rename Pod -> Server
[github/Chocobozzz/PeerTube.git] / server / helpers / webfinger.ts
CommitLineData
e4f97bab
C
1import * as WebFinger from 'webfinger.js'
2
3import { isTestInstance } from './core-utils'
4import { isActivityPubUrlValid } from './custom-validators'
5import { WebFingerData } from '../../shared'
60862425 6import { fetchRemoteAccountAndCreateServer } from './activitypub'
e4f97bab
C
7
8const webfinger = new WebFinger({
9 webfist_fallback: false,
10 tls_only: isTestInstance(),
11 uri_fallback: false,
12 request_timeout: 3000
13})
14
350e31d6
C
15async 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
60862425
C
25 const res = await fetchRemoteAccountAndCreateServer(selfLink.href)
26 if (res === undefined) throw new Error('Cannot fetch and create server of remote account ' + selfLink.href)
e4f97bab 27
350e31d6 28 return res.account
e4f97bab
C
29}
30
31// ---------------------------------------------------------------------------
32
33export {
34 getAccountFromWebfinger
35}
36
37// ---------------------------------------------------------------------------
38
350e31d6 39function 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}