mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2026-09-03 20:53:09 -05:00
More robust actor keys creation
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
|
import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
|
||||||
import { MActor } from '@server/types/models/index.js'
|
import { MActor } from '@server/types/models/index.js'
|
||||||
|
|
||||||
// Set account keys, this could be long so process after the account creation and do not block the client
|
export async function generateAndSaveActorKeys<T extends MActor> (actor: T) {
|
||||||
async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
|
|
||||||
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
|
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
|
||||||
|
|
||||||
actor.publicKey = publicKey
|
actor.publicKey = publicKey
|
||||||
@@ -10,7 +9,3 @@ async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
|
|||||||
|
|
||||||
return actor.save()
|
return actor.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
|
||||||
generateAndSaveActorKeys
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { ActorModel } from '@server/models/actor/actor.js'
|
|||||||
import { ActorKeysPayload } from '@peertube/peertube-models'
|
import { ActorKeysPayload } from '@peertube/peertube-models'
|
||||||
import { logger } from '../../../helpers/logger.js'
|
import { logger } from '../../../helpers/logger.js'
|
||||||
|
|
||||||
async function processActorKeys (job: Job) {
|
export async function processActorKeys (job: Job) {
|
||||||
const payload = job.data as ActorKeysPayload
|
const payload = job.data as ActorKeysPayload
|
||||||
logger.info('Processing actor keys in job %s.', job.id)
|
logger.info('Processing actor keys in job %s.', job.id)
|
||||||
|
|
||||||
@@ -12,9 +12,3 @@ async function processActorKeys (job: Job) {
|
|||||||
|
|
||||||
await generateAndSaveActorKeys(actor)
|
await generateAndSaveActorKeys(actor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
export {
|
|
||||||
processActorKeys
|
|
||||||
}
|
|
||||||
|
|||||||
+14
-11
@@ -26,6 +26,7 @@ import { buildActorInstance, findAvailableLocalActorName } from './local-actor.j
|
|||||||
import { Redis } from './redis.js'
|
import { Redis } from './redis.js'
|
||||||
import { createLocalVideoChannelWithoutKeys } from './video-channel.js'
|
import { createLocalVideoChannelWithoutKeys } from './video-channel.js'
|
||||||
import { createWatchLaterPlaylist } from './video-playlist.js'
|
import { createWatchLaterPlaylist } from './video-playlist.js'
|
||||||
|
import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
|
||||||
|
|
||||||
type ChannelNames = { name: string, displayName: string }
|
type ChannelNames = { name: string, displayName: string }
|
||||||
|
|
||||||
@@ -91,7 +92,10 @@ export async function createUserAccountAndChannelAndPlaylist (parameters: {
|
|||||||
}): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> {
|
}): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> {
|
||||||
const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
|
const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
|
||||||
|
|
||||||
const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
|
const accountKeys = await createPrivateAndPublicKeys()
|
||||||
|
const channelKeys = await createPrivateAndPublicKeys()
|
||||||
|
|
||||||
|
return sequelizeTypescript.transaction(async t => {
|
||||||
const userOptions = {
|
const userOptions = {
|
||||||
transaction: t,
|
transaction: t,
|
||||||
validate: validateUser
|
validate: validateUser
|
||||||
@@ -107,25 +111,23 @@ export async function createUserAccountAndChannelAndPlaylist (parameters: {
|
|||||||
applicationId: null,
|
applicationId: null,
|
||||||
t
|
t
|
||||||
})
|
})
|
||||||
|
accountCreated.Actor.publicKey = accountKeys.publicKey
|
||||||
|
accountCreated.Actor.privateKey = accountKeys.privateKey
|
||||||
|
await accountCreated.Actor.save({ transaction: t })
|
||||||
|
|
||||||
userCreated.Account = accountCreated
|
userCreated.Account = accountCreated
|
||||||
|
|
||||||
const channelAttributes = await buildChannelAttributes({ user: userCreated, transaction: t, channelNames })
|
const channelAttributes = await buildChannelAttributes({ user: userCreated, transaction: t, channelNames })
|
||||||
const videoChannel = await createLocalVideoChannelWithoutKeys(channelAttributes, accountCreated, t)
|
const videoChannel = await createLocalVideoChannelWithoutKeys(channelAttributes, accountCreated, t)
|
||||||
|
|
||||||
|
videoChannel.Actor.publicKey = channelKeys.publicKey
|
||||||
|
videoChannel.Actor.privateKey = channelKeys.privateKey
|
||||||
|
await videoChannel.Actor.save({ transaction: t })
|
||||||
|
|
||||||
const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
|
const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
|
||||||
|
|
||||||
return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
|
return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
|
||||||
})
|
})
|
||||||
|
|
||||||
const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([
|
|
||||||
generateAndSaveActorKeys(account.Actor),
|
|
||||||
generateAndSaveActorKeys(videoChannel.Actor)
|
|
||||||
])
|
|
||||||
|
|
||||||
account.Actor = accountActorWithKeys
|
|
||||||
videoChannel.Actor = channelActorWithKeys
|
|
||||||
|
|
||||||
return { user, account, videoChannel }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createLocalAccountWithoutKeys (parameters: {
|
export async function createLocalAccountWithoutKeys (parameters: {
|
||||||
@@ -147,6 +149,7 @@ export async function createLocalAccountWithoutKeys (parameters: {
|
|||||||
const url = getLocalAccountActivityPubUrl(name)
|
const url = getLocalAccountActivityPubUrl(name)
|
||||||
const actor = buildActorInstance(type, url, name)
|
const actor = buildActorInstance(type, url, name)
|
||||||
actor.accountId = account.id
|
actor.accountId = account.id
|
||||||
|
|
||||||
await actor.save({ transaction: t })
|
await actor.save({ transaction: t })
|
||||||
|
|
||||||
return Object.assign(account, { Actor: actor })
|
return Object.assign(account, { Actor: actor })
|
||||||
|
|||||||
Reference in New Issue
Block a user