1 import * as validator from 'validator'
3 import { exists, isUUIDValid } from '../misc'
4 import { isActivityPubUrlValid } from './misc'
5 import { isUserUsernameValid } from '../users'
6 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
8 function isAccountEndpointsObjectValid (endpointObject: any) {
9 return isAccountSharedInboxValid(endpointObject.sharedInbox)
12 function isAccountSharedInboxValid (sharedInbox: string) {
13 return isActivityPubUrlValid(sharedInbox)
16 function isAccountPublicKeyObjectValid (publicKeyObject: any) {
17 return isAccountPublicKeyIdValid(publicKeyObject.id) &&
18 isAccountPublicKeyOwnerValid(publicKeyObject.owner) &&
19 isAccountPublicKeyValid(publicKeyObject.publicKeyPem)
22 function isAccountPublicKeyIdValid (id: string) {
23 return isActivityPubUrlValid(id)
26 function isAccountTypeValid (type: string) {
27 return type === 'Person' || type === 'Application'
30 function isAccountPublicKeyOwnerValid (owner: string) {
31 return isActivityPubUrlValid(owner)
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)
42 function isAccountIdValid (id: string) {
43 return isActivityPubUrlValid(id)
46 function isAccountFollowingValid (id: string) {
47 return isActivityPubUrlValid(id)
50 function isAccountFollowersValid (id: string) {
51 return isActivityPubUrlValid(id)
54 function isAccountInboxValid (inbox: string) {
55 return isActivityPubUrlValid(inbox)
58 function isAccountOutboxValid (outbox: string) {
59 return isActivityPubUrlValid(outbox)
62 function isAccountNameValid (name: string) {
63 return isUserUsernameValid(name)
66 function isAccountPreferredUsernameValid (preferredUsername: string) {
67 return isAccountNameValid(preferredUsername)
70 function isAccountUrlValid (url: string) {
71 return isActivityPubUrlValid(url)
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)
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)
96 function isAccountFollowingCountValid (value: string) {
97 return exists(value) && validator.isInt('' + value, { min: 0 })
100 function isAccountFollowersCountValid (value: string) {
101 return exists(value) && validator.isInt('' + value, { min: 0 })
104 // ---------------------------------------------------------------------------
107 isAccountEndpointsObjectValid,
108 isAccountSharedInboxValid,
109 isAccountPublicKeyObjectValid,
110 isAccountPublicKeyIdValid,
112 isAccountPublicKeyOwnerValid,
113 isAccountPublicKeyValid,
115 isAccountFollowingValid,
116 isAccountFollowersValid,
118 isAccountOutboxValid,
119 isAccountPreferredUsernameValid,
121 isAccountPrivateKeyValid,
122 isRemoteAccountValid,
123 isAccountFollowingCountValid,
124 isAccountFollowersCountValid,