2017-11-09 17:51:58 +01:00
|
|
|
import * as WebFinger from 'webfinger.js'
|
|
|
|
|
|
|
|
import { isTestInstance } from './core-utils'
|
|
|
|
import { isActivityPubUrlValid } from './custom-validators'
|
|
|
|
import { WebFingerData } from '../../shared'
|
2017-11-15 11:00:25 +01:00
|
|
|
import { fetchRemoteAccountAndCreateServer } from './activitypub'
|
2017-11-09 17:51:58 +01:00
|
|
|
|
|
|
|
const webfinger = new WebFinger({
|
|
|
|
webfist_fallback: false,
|
|
|
|
tls_only: isTestInstance(),
|
|
|
|
uri_fallback: false,
|
|
|
|
request_timeout: 3000
|
|
|
|
})
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
async function getAccountFromWebfinger (nameWithHost: string) {
|
|
|
|
const webfingerData: WebFingerData = await webfingerLookup(nameWithHost)
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
|
2017-11-09 17:51:58 +01:00
|
|
|
|
|
|
|
const selfLink = webfingerData.links.find(l => l.rel === 'self')
|
2017-11-14 17:31:26 +01:00
|
|
|
if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
|
|
|
|
throw new Error('Cannot find self link or href is not a valid URL.')
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-11-15 11:00:25 +01:00
|
|
|
const res = await fetchRemoteAccountAndCreateServer(selfLink.href)
|
|
|
|
if (res === undefined) throw new Error('Cannot fetch and create server of remote account ' + selfLink.href)
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
return res.account
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
getAccountFromWebfinger
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
function webfingerLookup (nameWithHost: string) {
|
2017-11-09 17:51:58 +01:00
|
|
|
return new Promise<WebFingerData>((res, rej) => {
|
2017-11-14 17:31:26 +01:00
|
|
|
webfinger.lookup(nameWithHost, (err, p) => {
|
2017-11-09 17:51:58 +01:00
|
|
|
if (err) return rej(err)
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
return res(p.object)
|
2017-11-09 17:51:58 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|