2017-11-09 10:51:58 -06:00
|
|
|
import * as WebFinger from 'webfinger.js'
|
2017-11-20 03:24:29 -06:00
|
|
|
import { WebFingerData } from '../../shared'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../models/activitypub/actor'
|
2017-11-09 10:51:58 -06:00
|
|
|
import { isTestInstance } from './core-utils'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { isActivityPubUrlValid } from './custom-validators/activitypub/misc'
|
2018-08-24 04:04:02 -05:00
|
|
|
import { CONFIG } from '../initializers'
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
const webfinger = new WebFinger({
|
|
|
|
webfist_fallback: false,
|
|
|
|
tls_only: isTestInstance(),
|
|
|
|
uri_fallback: false,
|
|
|
|
request_timeout: 3000
|
|
|
|
})
|
|
|
|
|
2018-09-17 03:12:30 -05:00
|
|
|
async function loadActorUrlOrGetFromWebfinger (uriArg: string) {
|
|
|
|
// Handle strings like @toto@example.com
|
|
|
|
const uri = uriArg.startsWith('@') ? uriArg.slice(1) : uriArg
|
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
const [ name, host ] = uri.split('@')
|
2018-08-24 04:04:02 -05:00
|
|
|
let actor: ActorModel
|
|
|
|
|
|
|
|
if (host === CONFIG.WEBSERVER.HOST) {
|
|
|
|
actor = await ActorModel.loadLocalByName(name)
|
|
|
|
} else {
|
|
|
|
actor = await ActorModel.loadByNameAndHost(name, host)
|
|
|
|
}
|
2018-08-16 08:25:20 -05:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
if (actor) return actor.url
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
return getUrlFromWebfinger(uri)
|
2018-01-04 07:04:02 -06:00
|
|
|
}
|
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
async function getUrlFromWebfinger (uri: string) {
|
|
|
|
const webfingerData: WebFingerData = await webfingerLookup(uri)
|
2017-12-14 10:38:41 -06:00
|
|
|
return getLinkOrThrow(webfingerData)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-01-04 07:04:02 -06:00
|
|
|
getUrlFromWebfinger,
|
2017-12-14 10:38:41 -06:00
|
|
|
loadActorUrlOrGetFromWebfinger
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
function getLinkOrThrow (webfingerData: WebFingerData) {
|
|
|
|
if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
|
|
|
|
|
|
|
|
const selfLink = webfingerData.links.find(l => l.rel === 'self')
|
|
|
|
if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
|
|
|
|
throw new Error('Cannot find self link or href is not a valid URL.')
|
|
|
|
}
|
|
|
|
|
|
|
|
return selfLink.href
|
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
function webfingerLookup (nameWithHost: string) {
|
2017-11-09 10:51:58 -06:00
|
|
|
return new Promise<WebFingerData>((res, rej) => {
|
2017-11-14 10:31:26 -06:00
|
|
|
webfinger.lookup(nameWithHost, (err, p) => {
|
2017-11-09 10:51:58 -06:00
|
|
|
if (err) return rej(err)
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
return res(p.object)
|
2017-11-09 10:51:58 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|