X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcore-utils.ts;h=73bd994c17c7618a69db3eb65de751681f8ff6aa;hb=d7ce9dca613d96889baa0c93063806268f68cce5;hp=6ebe8e2acb5a2b6dace1282a0f8598ecb2c00a24;hpb=4e56f0fff12ab9840574e7a27277fc78b195b3e2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 6ebe8e2ac..73bd994c1 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -6,9 +6,8 @@ */ import { exec, ExecOptions } from 'child_process' -import { randomBytes } from 'crypto' +import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions, scrypt } from 'crypto' import { truncate } from 'lodash' -import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem' import { pipeline } from 'stream' import { URL } from 'url' import { promisify } from 'util' @@ -165,6 +164,14 @@ function isTestInstance () { return process.env.NODE_ENV === 'test' } +function isDevInstance () { + return process.env.NODE_ENV === 'dev' +} + +function isTestOrDevInstance () { + return isTestInstance() || isDevInstance() +} + function isProdInstance () { return process.env.NODE_ENV === 'production' } @@ -234,6 +241,51 @@ function toEven (num: number) { // --------------------------------------------------------------------------- +function generateRSAKeyPairPromise (size: number) { + return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => { + const options: RSAKeyPairOptions<'pem', 'pem'> = { + modulusLength: size, + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs1', + format: 'pem' + } + } + + generateKeyPair('rsa', options, (err, publicKey, privateKey) => { + if (err) return rej(err) + + return res({ publicKey, privateKey }) + }) + }) +} + +function generateED25519KeyPairPromise () { + return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => { + const options: ED25519KeyPairOptions<'pem', 'pem'> = { + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem' + } + } + + generateKeyPair('ed25519', options, (err, publicKey, privateKey) => { + if (err) return rej(err) + + return res({ publicKey, privateKey }) + }) + }) +} + +// --------------------------------------------------------------------------- + function promisify0 (func: (cb: (err: any, result: A) => void) => void): () => Promise { return function promisified (): Promise { return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { @@ -259,9 +311,17 @@ function promisify2 (func: (arg1: T, arg2: U, cb: (err: any, result: A) } } +// eslint-disable-next-line max-len +function promisify3 (func: (arg1: T, arg2: U, arg3: V, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U, arg3: V) => Promise { + return function promisified (arg1: T, arg2: U, arg3: V): Promise { + return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { + func.apply(null, [ arg1, arg2, arg3, (err: any, res: A) => err ? reject(err) : resolve(res) ]) + }) + } +} + const randomBytesPromise = promisify1(randomBytes) -const createPrivateKey = promisify1(createPrivateKey_1) -const getPublicKey = promisify1(getPublicKey_1) +const scryptPromise = promisify3(scrypt) const execPromise2 = promisify2(exec) const execPromise = promisify1(exec) const pipelinePromise = promisify(pipeline) @@ -270,6 +330,7 @@ const pipelinePromise = promisify(pipeline) export { isTestInstance, + isTestOrDevInstance, isProdInstance, getAppNumber, @@ -288,9 +349,13 @@ export { promisify1, promisify2, + scryptPromise, + randomBytesPromise, - createPrivateKey, - getPublicKey, + + generateRSAKeyPairPromise, + generateED25519KeyPairPromise, + execPromise2, execPromise, pipelinePromise,