aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/activitypub/account.ts
blob: acd2b805838b465b46c91726ff7c1c192afebbc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import * as validator from 'validator'

import { exists, isUUIDValid } from '../misc'
import { isActivityPubUrlValid } from './misc'
import { isUserUsernameValid } from '../users'
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'

function isAccountEndpointsObjectValid (endpointObject: any) {
  return isAccountSharedInboxValid(endpointObject.sharedInbox)
}

function isAccountSharedInboxValid (sharedInbox: string) {
  return isActivityPubUrlValid(sharedInbox)
}

function isAccountPublicKeyObjectValid (publicKeyObject: any) {
  return isAccountPublicKeyIdValid(publicKeyObject.id) &&
    isAccountPublicKeyOwnerValid(publicKeyObject.owner) &&
    isAccountPublicKeyValid(publicKeyObject.publicKeyPem)
}

function isAccountPublicKeyIdValid (id: string) {
  return isActivityPubUrlValid(id)
}

function isAccountTypeValid (type: string) {
  return type === 'Person' || type === 'Application'
}

function isAccountPublicKeyOwnerValid (owner: string) {
  return isActivityPubUrlValid(owner)
}

function isAccountPublicKeyValid (publicKey: string) {
  return exists(publicKey) &&
    typeof publicKey === 'string' &&
    publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
    publicKey.endsWith('-----END PUBLIC KEY-----') &&
    validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY)
}

function isAccountIdValid (id: string) {
  return isActivityPubUrlValid(id)
}

function isAccountFollowingValid (id: string) {
  return isActivityPubUrlValid(id)
}

function isAccountFollowersValid (id: string) {
  return isActivityPubUrlValid(id)
}

function isAccountInboxValid (inbox: string) {
  return isActivityPubUrlValid(inbox)
}

function isAccountOutboxValid (outbox: string) {
  return isActivityPubUrlValid(outbox)
}

function isAccountNameValid (name: string) {
  return isUserUsernameValid(name)
}

function isAccountPreferredUsernameValid (preferredUsername: string) {
  return isAccountNameValid(preferredUsername)
}

function isAccountUrlValid (url: string) {
  return isActivityPubUrlValid(url)
}

function isAccountPrivateKeyValid (privateKey: string) {
  return exists(privateKey) &&
    typeof privateKey === 'string' &&
    privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
    privateKey.endsWith('-----END RSA PRIVATE KEY-----') &&
    validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY)
}

function isRemoteAccountValid (remoteAccount: any) {
  return isAccountIdValid(remoteAccount.id) &&
    isUUIDValid(remoteAccount.uuid) &&
    isAccountTypeValid(remoteAccount.type) &&
    isAccountFollowingValid(remoteAccount.following) &&
    isAccountFollowersValid(remoteAccount.followers) &&
    isAccountInboxValid(remoteAccount.inbox) &&
    isAccountOutboxValid(remoteAccount.outbox) &&
    isAccountPreferredUsernameValid(remoteAccount.preferredUsername) &&
    isAccountUrlValid(remoteAccount.url) &&
    isAccountPublicKeyObjectValid(remoteAccount.publicKey) &&
    isAccountEndpointsObjectValid(remoteAccount.endpoint)
}

function isAccountFollowingCountValid (value: string) {
  return exists(value) && validator.isInt('' + value, { min: 0 })
}

function isAccountFollowersCountValid (value: string) {
  return exists(value) && validator.isInt('' + value, { min: 0 })
}

// ---------------------------------------------------------------------------

export {
  isAccountEndpointsObjectValid,
  isAccountSharedInboxValid,
  isAccountPublicKeyObjectValid,
  isAccountPublicKeyIdValid,
  isAccountTypeValid,
  isAccountPublicKeyOwnerValid,
  isAccountPublicKeyValid,
  isAccountIdValid,
  isAccountFollowingValid,
  isAccountFollowersValid,
  isAccountInboxValid,
  isAccountOutboxValid,
  isAccountPreferredUsernameValid,
  isAccountUrlValid,
  isAccountPrivateKeyValid,
  isRemoteAccountValid,
  isAccountFollowingCountValid,
  isAccountFollowersCountValid,
  isAccountNameValid
}