]>
Commit | Line | Data |
---|---|---|
1 | import * as validator from 'validator' | |
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers' | |
3 | import { exists } from '../misc' | |
4 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | |
5 | ||
6 | function isActorEndpointsObjectValid (endpointObject: any) { | |
7 | return isActivityPubUrlValid(endpointObject.sharedInbox) | |
8 | } | |
9 | ||
10 | function isActorPublicKeyObjectValid (publicKeyObject: any) { | |
11 | return isActivityPubUrlValid(publicKeyObject.id) && | |
12 | isActivityPubUrlValid(publicKeyObject.owner) && | |
13 | isActorPublicKeyValid(publicKeyObject.publicKeyPem) | |
14 | } | |
15 | ||
16 | function isActorTypeValid (type: string) { | |
17 | return type === 'Person' || type === 'Application' || type === 'Group' | |
18 | } | |
19 | ||
20 | function isActorPublicKeyValid (publicKey: string) { | |
21 | return exists(publicKey) && | |
22 | typeof publicKey === 'string' && | |
23 | publicKey.startsWith('-----BEGIN PUBLIC KEY-----') && | |
24 | publicKey.indexOf('-----END PUBLIC KEY-----') !== -1 && | |
25 | validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY) | |
26 | } | |
27 | ||
28 | const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+') | |
29 | function isActorPreferredUsernameValid (preferredUsername: string) { | |
30 | return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp) | |
31 | } | |
32 | ||
33 | function isActorPrivateKeyValid (privateKey: string) { | |
34 | return exists(privateKey) && | |
35 | typeof privateKey === 'string' && | |
36 | privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') && | |
37 | // Sometimes there is a \n at the end, so just assert the string contains the end mark | |
38 | privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 && | |
39 | validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY) | |
40 | } | |
41 | ||
42 | function isActorObjectValid (actor: any) { | |
43 | return exists(actor) && | |
44 | isActivityPubUrlValid(actor.id) && | |
45 | isActorTypeValid(actor.type) && | |
46 | isActivityPubUrlValid(actor.following) && | |
47 | isActivityPubUrlValid(actor.followers) && | |
48 | isActivityPubUrlValid(actor.inbox) && | |
49 | isActivityPubUrlValid(actor.outbox) && | |
50 | isActorPreferredUsernameValid(actor.preferredUsername) && | |
51 | isActivityPubUrlValid(actor.url) && | |
52 | isActorPublicKeyObjectValid(actor.publicKey) && | |
53 | isActorEndpointsObjectValid(actor.endpoints) && | |
54 | setValidAttributedTo(actor) && | |
55 | // If this is not an account, it should be attributed to an account | |
56 | // In PeerTube we use this to attach a video channel to a specific account | |
57 | (actor.type === 'Person' || actor.attributedTo.length !== 0) | |
58 | } | |
59 | ||
60 | function isActorFollowingCountValid (value: string) { | |
61 | return exists(value) && validator.isInt('' + value, { min: 0 }) | |
62 | } | |
63 | ||
64 | function isActorFollowersCountValid (value: string) { | |
65 | return exists(value) && validator.isInt('' + value, { min: 0 }) | |
66 | } | |
67 | ||
68 | function isActorDeleteActivityValid (activity: any) { | |
69 | return isBaseActivityValid(activity, 'Delete') | |
70 | } | |
71 | ||
72 | function isActorFollowActivityValid (activity: any) { | |
73 | return isBaseActivityValid(activity, 'Follow') && | |
74 | isActivityPubUrlValid(activity.object) | |
75 | } | |
76 | ||
77 | function isActorAcceptActivityValid (activity: any) { | |
78 | return isBaseActivityValid(activity, 'Accept') | |
79 | } | |
80 | ||
81 | function isActorUpdateActivityValid (activity: any) { | |
82 | return isBaseActivityValid(activity, 'Update') && | |
83 | isActorObjectValid(activity.object) | |
84 | } | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | ||
88 | export { | |
89 | isActorEndpointsObjectValid, | |
90 | isActorPublicKeyObjectValid, | |
91 | isActorTypeValid, | |
92 | isActorPublicKeyValid, | |
93 | isActorPreferredUsernameValid, | |
94 | isActorPrivateKeyValid, | |
95 | isActorObjectValid, | |
96 | isActorFollowingCountValid, | |
97 | isActorFollowersCountValid, | |
98 | isActorFollowActivityValid, | |
99 | isActorAcceptActivityValid, | |
100 | isActorDeleteActivityValid, | |
101 | isActorUpdateActivityValid | |
102 | } |