diff options
Diffstat (limited to 'server/helpers/custom-validators/activitypub/actor.ts')
-rw-r--r-- | server/helpers/custom-validators/activitypub/actor.ts | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts new file mode 100644 index 000000000..28551c96c --- /dev/null +++ b/server/helpers/custom-validators/activitypub/actor.ts | |||
@@ -0,0 +1,91 @@ | |||
1 | import * as validator from 'validator' | ||
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers' | ||
3 | import { isAccountNameValid } from '../accounts' | ||
4 | import { exists, isUUIDValid } from '../misc' | ||
5 | import { isActivityPubUrlValid, isBaseActivityValid } from './misc' | ||
6 | |||
7 | function isActorEndpointsObjectValid (endpointObject: any) { | ||
8 | return isActivityPubUrlValid(endpointObject.sharedInbox) | ||
9 | } | ||
10 | |||
11 | function isActorPublicKeyObjectValid (publicKeyObject: any) { | ||
12 | return isActivityPubUrlValid(publicKeyObject.id) && | ||
13 | isActivityPubUrlValid(publicKeyObject.owner) && | ||
14 | isActorPublicKeyValid(publicKeyObject.publicKeyPem) | ||
15 | } | ||
16 | |||
17 | function isActorTypeValid (type: string) { | ||
18 | return type === 'Person' || type === 'Application' || type === 'Group' | ||
19 | } | ||
20 | |||
21 | function isActorPublicKeyValid (publicKey: string) { | ||
22 | return exists(publicKey) && | ||
23 | typeof publicKey === 'string' && | ||
24 | publicKey.startsWith('-----BEGIN PUBLIC KEY-----') && | ||
25 | publicKey.endsWith('-----END PUBLIC KEY-----') && | ||
26 | validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY) | ||
27 | } | ||
28 | |||
29 | function isActorPreferredUsernameValid (preferredUsername: string) { | ||
30 | return isAccountNameValid(preferredUsername) | ||
31 | } | ||
32 | |||
33 | function isActorPrivateKeyValid (privateKey: string) { | ||
34 | return exists(privateKey) && | ||
35 | typeof privateKey === 'string' && | ||
36 | privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') && | ||
37 | privateKey.endsWith('-----END RSA PRIVATE KEY-----') && | ||
38 | validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY) | ||
39 | } | ||
40 | |||
41 | function isRemoteActorValid (remoteActor: any) { | ||
42 | return isActivityPubUrlValid(remoteActor.id) && | ||
43 | isUUIDValid(remoteActor.uuid) && | ||
44 | isActorTypeValid(remoteActor.type) && | ||
45 | isActivityPubUrlValid(remoteActor.following) && | ||
46 | isActivityPubUrlValid(remoteActor.followers) && | ||
47 | isActivityPubUrlValid(remoteActor.inbox) && | ||
48 | isActivityPubUrlValid(remoteActor.outbox) && | ||
49 | isActorPreferredUsernameValid(remoteActor.preferredUsername) && | ||
50 | isActivityPubUrlValid(remoteActor.url) && | ||
51 | isActorPublicKeyObjectValid(remoteActor.publicKey) && | ||
52 | isActorEndpointsObjectValid(remoteActor.endpoints) | ||
53 | } | ||
54 | |||
55 | function isActorFollowingCountValid (value: string) { | ||
56 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
57 | } | ||
58 | |||
59 | function isActorFollowersCountValid (value: string) { | ||
60 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
61 | } | ||
62 | |||
63 | function isActorDeleteActivityValid (activity: any) { | ||
64 | return isBaseActivityValid(activity, 'Delete') | ||
65 | } | ||
66 | |||
67 | function isActorFollowActivityValid (activity: any) { | ||
68 | return isBaseActivityValid(activity, 'Follow') && | ||
69 | isActivityPubUrlValid(activity.object) | ||
70 | } | ||
71 | |||
72 | function isActorAcceptActivityValid (activity: any) { | ||
73 | return isBaseActivityValid(activity, 'Accept') | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | export { | ||
79 | isActorEndpointsObjectValid, | ||
80 | isActorPublicKeyObjectValid, | ||
81 | isActorTypeValid, | ||
82 | isActorPublicKeyValid, | ||
83 | isActorPreferredUsernameValid, | ||
84 | isActorPrivateKeyValid, | ||
85 | isRemoteActorValid, | ||
86 | isActorFollowingCountValid, | ||
87 | isActorFollowersCountValid, | ||
88 | isActorFollowActivityValid, | ||
89 | isActorAcceptActivityValid, | ||
90 | isActorDeleteActivityValid | ||
91 | } | ||