This commit is contained in:
syuilo 2019-04-08 03:35:02 +09:00
parent d9a5e06b5b
commit 142d59be85
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
4 changed files with 29 additions and 18 deletions

View File

@ -20,5 +20,10 @@ export class UserKeypair {
@Column('varchar', { @Column('varchar', {
length: 4096, length: 4096,
}) })
public keyPem: string; public publicKey: string;
@Column('varchar', {
length: 4096,
})
public privateKey: string;
} }

View File

@ -1,4 +1,3 @@
import { createPublicKey } from 'crypto';
import config from '../../../config'; import config from '../../../config';
import { ILocalUser } from '../../../models/entities/user'; import { ILocalUser } from '../../../models/entities/user';
import { UserKeypair } from '../../../models/entities/user-keypair'; import { UserKeypair } from '../../../models/entities/user-keypair';
@ -7,5 +6,5 @@ export default (user: ILocalUser, key: UserKeypair) => ({
id: `${config.url}/users/${user.id}/publickey`, id: `${config.url}/users/${user.id}/publickey`,
type: 'Key', type: 'Key',
owner: `${config.url}/users/${user.id}`, owner: `${config.url}/users/${user.id}`,
publicKeyPem: createPublicKey(key.keyPem) publicKeyPem: key.publicKey
}); });

View File

@ -67,7 +67,7 @@ export default async (user: ILocalUser, url: string, object: any) => {
sign(req, { sign(req, {
authorizationHeaderName: 'Signature', authorizationHeaderName: 'Signature',
key: keypair.keyPem, key: keypair.privateKey,
keyId: `${config.url}/users/${user.id}/publickey`, keyId: `${config.url}/users/${user.id}/publickey`,
headers: ['date', 'host', 'digest'] headers: ['date', 'host', 'digest']
}); });

View File

@ -10,6 +10,7 @@ import { genId } from '../../../misc/gen-id';
import { usersChart } from '../../../services/chart'; import { usersChart } from '../../../services/chart';
import { UserServiceLinking } from '../../../models/entities/user-service-linking'; import { UserServiceLinking } from '../../../models/entities/user-service-linking';
import { User } from '../../../models/entities/user'; import { User } from '../../../models/entities/user';
import { UserKeypair } from '../../../models/entities/user-keypair';
export default async (ctx: Koa.BaseContext) => { export default async (ctx: Koa.BaseContext) => {
const body = ctx.request.body as any; const body = ctx.request.body as any;
@ -80,6 +81,23 @@ export default async (ctx: Koa.BaseContext) => {
return; return;
} }
const keyPair = await new Promise<string[]>((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({ const account = await Users.save({
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),
@ -95,21 +113,10 @@ export default async (ctx: Koa.BaseContext) => {
await UserKeypairs.save({ await UserKeypairs.save({
id: genId(), id: genId(),
keyPem: await new Promise<string>((s, j) => generateKeyPair('rsa', { publicKey: keyPair[0],
modulusLength: 4096, privateKey: keyPair[1],
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
cipher: undefined,
passphrase: undefined
}
}, (e, _, x) => e ? j(e) : s(x))),
userId: account.id userId: account.id
}); } as UserKeypair);
await UserServiceLinkings.save({ await UserServiceLinkings.save({
id: genId(), id: genId(),