2017-12-14 11:18:49 +01:00
|
|
|
import * as validator from 'validator'
|
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers'
|
2018-03-19 10:23:42 +01:00
|
|
|
import { normalizeActor } from '../../../lib/activitypub'
|
2017-12-19 10:34:56 +01:00
|
|
|
import { exists } from '../misc'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
|
2017-12-14 11:18:49 +01:00
|
|
|
|
|
|
|
function isActorEndpointsObjectValid (endpointObject: any) {
|
|
|
|
return isActivityPubUrlValid(endpointObject.sharedInbox)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorPublicKeyObjectValid (publicKeyObject: any) {
|
|
|
|
return isActivityPubUrlValid(publicKeyObject.id) &&
|
|
|
|
isActivityPubUrlValid(publicKeyObject.owner) &&
|
|
|
|
isActorPublicKeyValid(publicKeyObject.publicKeyPem)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorTypeValid (type: string) {
|
|
|
|
return type === 'Person' || type === 'Application' || type === 'Group'
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorPublicKeyValid (publicKey: string) {
|
|
|
|
return exists(publicKey) &&
|
|
|
|
typeof publicKey === 'string' &&
|
|
|
|
publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
|
2017-12-19 10:34:56 +01:00
|
|
|
publicKey.indexOf('-----END PUBLIC KEY-----') !== -1 &&
|
2018-01-03 11:10:40 +01:00
|
|
|
validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 10:34:56 +01:00
|
|
|
const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+')
|
2017-12-14 11:18:49 +01:00
|
|
|
function isActorPreferredUsernameValid (preferredUsername: string) {
|
2017-12-19 10:34:56 +01:00
|
|
|
return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
|
2017-12-14 17:38:41 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:18:49 +01:00
|
|
|
function isActorPrivateKeyValid (privateKey: string) {
|
|
|
|
return exists(privateKey) &&
|
|
|
|
typeof privateKey === 'string' &&
|
|
|
|
privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
|
2017-12-19 10:34:56 +01:00
|
|
|
// Sometimes there is a \n at the end, so just assert the string contains the end mark
|
|
|
|
privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 &&
|
2018-01-03 11:10:40 +01:00
|
|
|
validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:38:50 +01:00
|
|
|
function isActorObjectValid (actor: any) {
|
|
|
|
return exists(actor) &&
|
|
|
|
isActivityPubUrlValid(actor.id) &&
|
|
|
|
isActorTypeValid(actor.type) &&
|
|
|
|
isActivityPubUrlValid(actor.following) &&
|
|
|
|
isActivityPubUrlValid(actor.followers) &&
|
|
|
|
isActivityPubUrlValid(actor.inbox) &&
|
|
|
|
isActivityPubUrlValid(actor.outbox) &&
|
|
|
|
isActorPreferredUsernameValid(actor.preferredUsername) &&
|
|
|
|
isActivityPubUrlValid(actor.url) &&
|
|
|
|
isActorPublicKeyObjectValid(actor.publicKey) &&
|
|
|
|
isActorEndpointsObjectValid(actor.endpoints) &&
|
|
|
|
setValidAttributedTo(actor) &&
|
2018-03-19 10:23:42 +01:00
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
// If this is not an account, it should be attributed to an account
|
|
|
|
// In PeerTube we use this to attach a video channel to a specific account
|
2018-01-03 16:38:50 +01:00
|
|
|
(actor.type === 'Person' || actor.attributedTo.length !== 0)
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isActorFollowingCountValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt('' + value, { min: 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorFollowersCountValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt('' + value, { min: 0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorDeleteActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Delete')
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorFollowActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Follow') &&
|
|
|
|
isActivityPubUrlValid(activity.object)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActorAcceptActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Accept')
|
|
|
|
}
|
|
|
|
|
2018-01-11 17:37:49 +01:00
|
|
|
function isActorRejectActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Reject')
|
|
|
|
}
|
|
|
|
|
2018-01-03 16:38:50 +01:00
|
|
|
function isActorUpdateActivityValid (activity: any) {
|
2018-03-19 10:23:42 +01:00
|
|
|
normalizeActor(activity.object)
|
|
|
|
|
2018-01-03 16:38:50 +01:00
|
|
|
return isBaseActivityValid(activity, 'Update') &&
|
|
|
|
isActorObjectValid(activity.object)
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:18:49 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
isActorEndpointsObjectValid,
|
|
|
|
isActorPublicKeyObjectValid,
|
|
|
|
isActorTypeValid,
|
|
|
|
isActorPublicKeyValid,
|
|
|
|
isActorPreferredUsernameValid,
|
|
|
|
isActorPrivateKeyValid,
|
2018-01-03 16:38:50 +01:00
|
|
|
isActorObjectValid,
|
2017-12-14 11:18:49 +01:00
|
|
|
isActorFollowingCountValid,
|
|
|
|
isActorFollowersCountValid,
|
|
|
|
isActorFollowActivityValid,
|
|
|
|
isActorAcceptActivityValid,
|
2018-01-11 17:37:49 +01:00
|
|
|
isActorRejectActivityValid,
|
2017-12-14 17:38:41 +01:00
|
|
|
isActorDeleteActivityValid,
|
2018-01-03 16:38:50 +01:00
|
|
|
isActorUpdateActivityValid
|
2017-12-14 11:18:49 +01:00
|
|
|
}
|