From 142d59be85cd06b11d9df1375bcd0a84c0425e22 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 8 Apr 2019 03:35:02 +0900 Subject: [PATCH] Fix bug --- src/models/entities/user-keypair.ts | 7 +++++- src/remote/activitypub/renderer/key.ts | 3 +-- src/remote/activitypub/request.ts | 2 +- src/server/api/private/signup.ts | 35 +++++++++++++++----------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/models/entities/user-keypair.ts b/src/models/entities/user-keypair.ts index 06b98d2536..be264641f7 100644 --- a/src/models/entities/user-keypair.ts +++ b/src/models/entities/user-keypair.ts @@ -20,5 +20,10 @@ export class UserKeypair { @Column('varchar', { length: 4096, }) - public keyPem: string; + public publicKey: string; + + @Column('varchar', { + length: 4096, + }) + public privateKey: string; } diff --git a/src/remote/activitypub/renderer/key.ts b/src/remote/activitypub/renderer/key.ts index fb5975a6c4..334e5e00cd 100644 --- a/src/remote/activitypub/renderer/key.ts +++ b/src/remote/activitypub/renderer/key.ts @@ -1,4 +1,3 @@ -import { createPublicKey } from 'crypto'; import config from '../../../config'; import { ILocalUser } from '../../../models/entities/user'; import { UserKeypair } from '../../../models/entities/user-keypair'; @@ -7,5 +6,5 @@ export default (user: ILocalUser, key: UserKeypair) => ({ id: `${config.url}/users/${user.id}/publickey`, type: 'Key', owner: `${config.url}/users/${user.id}`, - publicKeyPem: createPublicKey(key.keyPem) + publicKeyPem: key.publicKey }); diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts index a089ed371c..c50d05e2a6 100644 --- a/src/remote/activitypub/request.ts +++ b/src/remote/activitypub/request.ts @@ -67,7 +67,7 @@ export default async (user: ILocalUser, url: string, object: any) => { sign(req, { authorizationHeaderName: 'Signature', - key: keypair.keyPem, + key: keypair.privateKey, keyId: `${config.url}/users/${user.id}/publickey`, headers: ['date', 'host', 'digest'] }); diff --git a/src/server/api/private/signup.ts b/src/server/api/private/signup.ts index 1d304b8e11..8ab702bd8a 100644 --- a/src/server/api/private/signup.ts +++ b/src/server/api/private/signup.ts @@ -10,6 +10,7 @@ import { genId } from '../../../misc/gen-id'; import { usersChart } from '../../../services/chart'; import { UserServiceLinking } from '../../../models/entities/user-service-linking'; import { User } from '../../../models/entities/user'; +import { UserKeypair } from '../../../models/entities/user-keypair'; export default async (ctx: Koa.BaseContext) => { const body = ctx.request.body as any; @@ -80,6 +81,23 @@ export default async (ctx: Koa.BaseContext) => { return; } + const keyPair = await new Promise((s, j) => + generateKeyPair('rsa', { + modulusLength: 4096, + publicKeyEncoding: { + type: 'pkcs1', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs1', + format: 'pem', + cipher: undefined, + passphrase: undefined + } + }, (e, publicKey, privateKey) => + e ? j(e) : s([publicKey, privateKey]) + )); + const account = await Users.save({ id: genId(), createdAt: new Date(), @@ -95,21 +113,10 @@ export default async (ctx: Koa.BaseContext) => { await UserKeypairs.save({ id: genId(), - keyPem: await new Promise((s, j) => generateKeyPair('rsa', { - modulusLength: 4096, - publicKeyEncoding: { - type: 'pkcs1', - format: 'pem' - }, - privateKeyEncoding: { - type: 'pkcs1', - format: 'pem', - cipher: undefined, - passphrase: undefined - } - }, (e, _, x) => e ? j(e) : s(x))), + publicKey: keyPair[0], + privateKey: keyPair[1], userId: account.id - }); + } as UserKeypair); await UserServiceLinkings.save({ id: genId(),