]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/activitypub/actor.ts
Use RsaSignature2017
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / actor.ts
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 { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
6 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
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-----') &&
26 publicKey.endsWith('-----END PUBLIC KEY-----') &&
27 validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY)
28 }
29
30 function isActorPreferredUsernameValid (preferredUsername: string) {
31 return isAccountNameValid(preferredUsername) || isVideoChannelNameValid(preferredUsername)
32 }
33
34 const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+')
35 function isActorNameValid (name: string) {
36 return exists(name) && validator.matches(name, actorNameRegExp)
37 }
38
39 function isActorPrivateKeyValid (privateKey: string) {
40 return exists(privateKey) &&
41 typeof privateKey === 'string' &&
42 privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
43 privateKey.endsWith('-----END RSA PRIVATE KEY-----') &&
44 validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY)
45 }
46
47 function isRemoteActorValid (remoteActor: any) {
48 return isActivityPubUrlValid(remoteActor.id) &&
49 isUUIDValid(remoteActor.uuid) &&
50 isActorTypeValid(remoteActor.type) &&
51 isActivityPubUrlValid(remoteActor.following) &&
52 isActivityPubUrlValid(remoteActor.followers) &&
53 isActivityPubUrlValid(remoteActor.inbox) &&
54 isActivityPubUrlValid(remoteActor.outbox) &&
55 isActorNameValid(remoteActor.name) &&
56 isActorPreferredUsernameValid(remoteActor.preferredUsername) &&
57 isActivityPubUrlValid(remoteActor.url) &&
58 isActorPublicKeyObjectValid(remoteActor.publicKey) &&
59 isActorEndpointsObjectValid(remoteActor.endpoints) &&
60 (!remoteActor.summary || isVideoChannelDescriptionValid(remoteActor.summary)) &&
61 setValidAttributedTo(remoteActor) &&
62 // If this is not an account, it should be attributed to an account
63 // In PeerTube we use this to attach a video channel to a specific account
64 (remoteActor.type === 'Person' || remoteActor.attributedTo.length !== 0)
65 }
66
67 function isActorFollowingCountValid (value: string) {
68 return exists(value) && validator.isInt('' + value, { min: 0 })
69 }
70
71 function isActorFollowersCountValid (value: string) {
72 return exists(value) && validator.isInt('' + value, { min: 0 })
73 }
74
75 function isActorDeleteActivityValid (activity: any) {
76 return isBaseActivityValid(activity, 'Delete')
77 }
78
79 function isActorFollowActivityValid (activity: any) {
80 return isBaseActivityValid(activity, 'Follow') &&
81 isActivityPubUrlValid(activity.object)
82 }
83
84 function isActorAcceptActivityValid (activity: any) {
85 return isBaseActivityValid(activity, 'Accept')
86 }
87
88 // ---------------------------------------------------------------------------
89
90 export {
91 isActorEndpointsObjectValid,
92 isActorPublicKeyObjectValid,
93 isActorTypeValid,
94 isActorPublicKeyValid,
95 isActorPreferredUsernameValid,
96 isActorPrivateKeyValid,
97 isRemoteActorValid,
98 isActorFollowingCountValid,
99 isActorFollowersCountValid,
100 isActorFollowActivityValid,
101 isActorAcceptActivityValid,
102 isActorDeleteActivityValid,
103 isActorNameValid
104 }