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