aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/actors
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/actors')
-rw-r--r--server/lib/activitypub/actors/get.ts2
-rw-r--r--server/lib/activitypub/actors/index.ts1
-rw-r--r--server/lib/activitypub/actors/refresh.ts4
-rw-r--r--server/lib/activitypub/actors/shared/index.ts2
-rw-r--r--server/lib/activitypub/actors/webfinger.ts67
5 files changed, 72 insertions, 4 deletions
diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts
index 0d5bea789..e7e87a967 100644
--- a/server/lib/activitypub/actors/get.ts
+++ b/server/lib/activitypub/actors/get.ts
@@ -1,9 +1,9 @@
1 1
2import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' 2import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub'
3import { ActorFetchByUrlType, fetchActorByUrl } from '@server/helpers/actor'
4import { retryTransactionWrapper } from '@server/helpers/database-utils' 3import { retryTransactionWrapper } from '@server/helpers/database-utils'
5import { logger } from '@server/helpers/logger' 4import { logger } from '@server/helpers/logger'
6import { JobQueue } from '@server/lib/job-queue' 5import { JobQueue } from '@server/lib/job-queue'
6import { ActorFetchByUrlType, fetchActorByUrl } from '@server/lib/model-loaders'
7import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' 7import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models'
8import { ActivityPubActor } from '@shared/models' 8import { ActivityPubActor } from '@shared/models'
9import { refreshActorIfNeeded } from './refresh' 9import { refreshActorIfNeeded } from './refresh'
diff --git a/server/lib/activitypub/actors/index.ts b/server/lib/activitypub/actors/index.ts
index a54da6798..5ee2a6f1a 100644
--- a/server/lib/activitypub/actors/index.ts
+++ b/server/lib/activitypub/actors/index.ts
@@ -3,3 +3,4 @@ export * from './image'
3export * from './keys' 3export * from './keys'
4export * from './refresh' 4export * from './refresh'
5export * from './updater' 5export * from './updater'
6export * from './webfinger'
diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts
index ff3b249d0..9f2289bfa 100644
--- a/server/lib/activitypub/actors/refresh.ts
+++ b/server/lib/activitypub/actors/refresh.ts
@@ -1,12 +1,12 @@
1import { ActorFetchByUrlType } from '@server/helpers/actor'
2import { logger } from '@server/helpers/logger' 1import { logger } from '@server/helpers/logger'
3import { PeerTubeRequestError } from '@server/helpers/requests' 2import { PeerTubeRequestError } from '@server/helpers/requests'
4import { getUrlFromWebfinger } from '@server/helpers/webfinger' 3import { ActorFetchByUrlType } from '@server/lib/model-loaders'
5import { ActorModel } from '@server/models/actor/actor' 4import { ActorModel } from '@server/models/actor/actor'
6import { MActorAccountChannelId, MActorFull } from '@server/types/models' 5import { MActorAccountChannelId, MActorFull } from '@server/types/models'
7import { HttpStatusCode } from '@shared/core-utils' 6import { HttpStatusCode } from '@shared/core-utils'
8import { fetchRemoteActor } from './shared' 7import { fetchRemoteActor } from './shared'
9import { APActorUpdater } from './updater' 8import { APActorUpdater } from './updater'
9import { getUrlFromWebfinger } from './webfinger'
10 10
11async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( 11async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (
12 actorArg: T, 12 actorArg: T,
diff --git a/server/lib/activitypub/actors/shared/index.ts b/server/lib/activitypub/actors/shared/index.ts
index a2ff468cf..52af1a8e1 100644
--- a/server/lib/activitypub/actors/shared/index.ts
+++ b/server/lib/activitypub/actors/shared/index.ts
@@ -1,3 +1,3 @@
1export * from './creator' 1export * from './creator'
2export * from './url-to-object'
3export * from './object-to-model-attributes' 2export * from './object-to-model-attributes'
3export * from './url-to-object'
diff --git a/server/lib/activitypub/actors/webfinger.ts b/server/lib/activitypub/actors/webfinger.ts
new file mode 100644
index 000000000..cf8eddfc7
--- /dev/null
+++ b/server/lib/activitypub/actors/webfinger.ts
@@ -0,0 +1,67 @@
1import * as WebFinger from 'webfinger.js'
2import { isTestInstance } from '@server/helpers/core-utils'
3import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4import { WEBSERVER } from '@server/initializers/constants'
5import { ActorModel } from '@server/models/actor/actor'
6import { MActorFull } from '@server/types/models'
7import { WebFingerData } from '@shared/models'
8
9const webfinger = new WebFinger({
10 webfist_fallback: false,
11 tls_only: isTestInstance(),
12 uri_fallback: false,
13 request_timeout: 3000
14})
15
16async function loadActorUrlOrGetFromWebfinger (uriArg: string) {
17 // Handle strings like @toto@example.com
18 const uri = uriArg.startsWith('@') ? uriArg.slice(1) : uriArg
19
20 const [ name, host ] = uri.split('@')
21 let actor: MActorFull
22
23 if (!host || host === WEBSERVER.HOST) {
24 actor = await ActorModel.loadLocalByName(name)
25 } else {
26 actor = await ActorModel.loadByNameAndHost(name, host)
27 }
28
29 if (actor) return actor.url
30
31 return getUrlFromWebfinger(uri)
32}
33
34async function getUrlFromWebfinger (uri: string) {
35 const webfingerData: WebFingerData = await webfingerLookup(uri)
36 return getLinkOrThrow(webfingerData)
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 getUrlFromWebfinger,
43 loadActorUrlOrGetFromWebfinger
44}
45
46// ---------------------------------------------------------------------------
47
48function getLinkOrThrow (webfingerData: WebFingerData) {
49 if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
50
51 const selfLink = webfingerData.links.find(l => l.rel === 'self')
52 if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
53 throw new Error('Cannot find self link or href is not a valid URL.')
54 }
55
56 return selfLink.href
57}
58
59function webfingerLookup (nameWithHost: string) {
60 return new Promise<WebFingerData>((res, rej) => {
61 webfinger.lookup(nameWithHost, (err, p) => {
62 if (err) return rej(err)
63
64 return res(p.object)
65 })
66 })
67}