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