]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/activitypub/account.ts
Optimize signature verification
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / account.ts
1 import * as validator from 'validator'
2 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
3 import { isAccountNameValid } from '../accounts'
4 import { exists, isUUIDValid } from '../misc'
5 import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
6
7 function isAccountEndpointsObjectValid (endpointObject: any) {
8 return isAccountSharedInboxValid(endpointObject.sharedInbox)
9 }
10
11 function isAccountSharedInboxValid (sharedInbox: string) {
12 return isActivityPubUrlValid(sharedInbox)
13 }
14
15 function isAccountPublicKeyObjectValid (publicKeyObject: any) {
16 return isAccountPublicKeyIdValid(publicKeyObject.id) &&
17 isAccountPublicKeyOwnerValid(publicKeyObject.owner) &&
18 isAccountPublicKeyValid(publicKeyObject.publicKeyPem)
19 }
20
21 function isAccountPublicKeyIdValid (id: string) {
22 return isActivityPubUrlValid(id)
23 }
24
25 function isAccountTypeValid (type: string) {
26 return type === 'Person' || type === 'Application'
27 }
28
29 function isAccountPublicKeyOwnerValid (owner: string) {
30 return isActivityPubUrlValid(owner)
31 }
32
33 function isAccountPublicKeyValid (publicKey: string) {
34 return exists(publicKey) &&
35 typeof publicKey === 'string' &&
36 publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
37 publicKey.endsWith('-----END PUBLIC KEY-----') &&
38 validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY)
39 }
40
41 function isAccountIdValid (id: string) {
42 return isActivityPubUrlValid(id)
43 }
44
45 function isAccountFollowingValid (id: string) {
46 return isActivityPubUrlValid(id)
47 }
48
49 function isAccountFollowersValid (id: string) {
50 return isActivityPubUrlValid(id)
51 }
52
53 function isAccountInboxValid (inbox: string) {
54 return isActivityPubUrlValid(inbox)
55 }
56
57 function isAccountOutboxValid (outbox: string) {
58 return isActivityPubUrlValid(outbox)
59 }
60
61 function isAccountPreferredUsernameValid (preferredUsername: string) {
62 return isAccountNameValid(preferredUsername)
63 }
64
65 function isAccountUrlValid (url: string) {
66 return isActivityPubUrlValid(url)
67 }
68
69 function isAccountPrivateKeyValid (privateKey: string) {
70 return exists(privateKey) &&
71 typeof privateKey === 'string' &&
72 privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
73 privateKey.endsWith('-----END RSA PRIVATE KEY-----') &&
74 validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY)
75 }
76
77 function isRemoteAccountValid (remoteAccount: any) {
78 return isAccountIdValid(remoteAccount.id) &&
79 isUUIDValid(remoteAccount.uuid) &&
80 isAccountTypeValid(remoteAccount.type) &&
81 isAccountFollowingValid(remoteAccount.following) &&
82 isAccountFollowersValid(remoteAccount.followers) &&
83 isAccountInboxValid(remoteAccount.inbox) &&
84 isAccountOutboxValid(remoteAccount.outbox) &&
85 isAccountPreferredUsernameValid(remoteAccount.preferredUsername) &&
86 isAccountUrlValid(remoteAccount.url) &&
87 isAccountPublicKeyObjectValid(remoteAccount.publicKey) &&
88 isAccountEndpointsObjectValid(remoteAccount.endpoints)
89 }
90
91 function isAccountFollowingCountValid (value: string) {
92 return exists(value) && validator.isInt('' + value, { min: 0 })
93 }
94
95 function isAccountFollowersCountValid (value: string) {
96 return exists(value) && validator.isInt('' + value, { min: 0 })
97 }
98
99 function isAccountDeleteActivityValid (activity: any) {
100 return isBaseActivityValid(activity, 'Delete')
101 }
102
103 function isAccountFollowActivityValid (activity: any) {
104 return isBaseActivityValid(activity, 'Follow') &&
105 isActivityPubUrlValid(activity.object)
106 }
107
108 function isAccountAcceptActivityValid (activity: any) {
109 return isBaseActivityValid(activity, 'Accept')
110 }
111
112 // ---------------------------------------------------------------------------
113
114 export {
115 isAccountEndpointsObjectValid,
116 isAccountSharedInboxValid,
117 isAccountPublicKeyObjectValid,
118 isAccountPublicKeyIdValid,
119 isAccountTypeValid,
120 isAccountPublicKeyOwnerValid,
121 isAccountPublicKeyValid,
122 isAccountIdValid,
123 isAccountFollowingValid,
124 isAccountFollowersValid,
125 isAccountInboxValid,
126 isAccountOutboxValid,
127 isAccountPreferredUsernameValid,
128 isAccountUrlValid,
129 isAccountPrivateKeyValid,
130 isRemoteAccountValid,
131 isAccountFollowingCountValid,
132 isAccountFollowersCountValid,
133 isAccountNameValid,
134 isAccountFollowActivityValid,
135 isAccountAcceptActivityValid,
136 isAccountDeleteActivityValid
137 }