diff options
Diffstat (limited to 'server/lib/activitypub/actors')
-rw-r--r-- | server/lib/activitypub/actors/get.ts | 2 | ||||
-rw-r--r-- | server/lib/activitypub/actors/index.ts | 1 | ||||
-rw-r--r-- | server/lib/activitypub/actors/refresh.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/actors/shared/index.ts | 2 | ||||
-rw-r--r-- | server/lib/activitypub/actors/webfinger.ts | 67 |
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 | ||
2 | import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' | 2 | import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' |
3 | import { ActorFetchByUrlType, fetchActorByUrl } from '@server/helpers/actor' | ||
4 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | 3 | import { retryTransactionWrapper } from '@server/helpers/database-utils' |
5 | import { logger } from '@server/helpers/logger' | 4 | import { logger } from '@server/helpers/logger' |
6 | import { JobQueue } from '@server/lib/job-queue' | 5 | import { JobQueue } from '@server/lib/job-queue' |
6 | import { ActorFetchByUrlType, fetchActorByUrl } from '@server/lib/model-loaders' | ||
7 | import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' | 7 | import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' |
8 | import { ActivityPubActor } from '@shared/models' | 8 | import { ActivityPubActor } from '@shared/models' |
9 | import { refreshActorIfNeeded } from './refresh' | 9 | import { 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' | |||
3 | export * from './keys' | 3 | export * from './keys' |
4 | export * from './refresh' | 4 | export * from './refresh' |
5 | export * from './updater' | 5 | export * from './updater' |
6 | export * 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 @@ | |||
1 | import { ActorFetchByUrlType } from '@server/helpers/actor' | ||
2 | import { logger } from '@server/helpers/logger' | 1 | import { logger } from '@server/helpers/logger' |
3 | import { PeerTubeRequestError } from '@server/helpers/requests' | 2 | import { PeerTubeRequestError } from '@server/helpers/requests' |
4 | import { getUrlFromWebfinger } from '@server/helpers/webfinger' | 3 | import { ActorFetchByUrlType } from '@server/lib/model-loaders' |
5 | import { ActorModel } from '@server/models/actor/actor' | 4 | import { ActorModel } from '@server/models/actor/actor' |
6 | import { MActorAccountChannelId, MActorFull } from '@server/types/models' | 5 | import { MActorAccountChannelId, MActorFull } from '@server/types/models' |
7 | import { HttpStatusCode } from '@shared/core-utils' | 6 | import { HttpStatusCode } from '@shared/core-utils' |
8 | import { fetchRemoteActor } from './shared' | 7 | import { fetchRemoteActor } from './shared' |
9 | import { APActorUpdater } from './updater' | 8 | import { APActorUpdater } from './updater' |
9 | import { getUrlFromWebfinger } from './webfinger' | ||
10 | 10 | ||
11 | async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( | 11 | async 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 @@ | |||
1 | export * from './creator' | 1 | export * from './creator' |
2 | export * from './url-to-object' | ||
3 | export * from './object-to-model-attributes' | 2 | export * from './object-to-model-attributes' |
3 | export * 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 @@ | |||
1 | import * as WebFinger from 'webfinger.js' | ||
2 | import { isTestInstance } from '@server/helpers/core-utils' | ||
3 | import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' | ||
4 | import { WEBSERVER } from '@server/initializers/constants' | ||
5 | import { ActorModel } from '@server/models/actor/actor' | ||
6 | import { MActorFull } from '@server/types/models' | ||
7 | import { WebFingerData } from '@shared/models' | ||
8 | |||
9 | const webfinger = new WebFinger({ | ||
10 | webfist_fallback: false, | ||
11 | tls_only: isTestInstance(), | ||
12 | uri_fallback: false, | ||
13 | request_timeout: 3000 | ||
14 | }) | ||
15 | |||
16 | async 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 | |||
34 | async function getUrlFromWebfinger (uri: string) { | ||
35 | const webfingerData: WebFingerData = await webfingerLookup(uri) | ||
36 | return getLinkOrThrow(webfingerData) | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | export { | ||
42 | getUrlFromWebfinger, | ||
43 | loadActorUrlOrGetFromWebfinger | ||
44 | } | ||
45 | |||
46 | // --------------------------------------------------------------------------- | ||
47 | |||
48 | function 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 | |||
59 | function 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 | } | ||