forked from mirrors/misskey
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { sign } from 'node:crypto';
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
|
import * as Redis from 'ioredis';
|
|
import type { MiUser } from '@/models/User.js';
|
|
import type { UserKeypairsRepository } from '@/models/_.js';
|
|
import { RedisKVCache } from '@/misc/cache.js';
|
|
import type { MiUserKeypair } from '@/models/UserKeypair.js';
|
|
import { DI } from '@/di-symbols.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
import { ED25519_SIGN_ALGORITHM, genEd25519KeyPair } from '@/misc/gen-key-pair.js';
|
|
import { GlobalEventService, GlobalEvents } from '@/core/GlobalEventService.js';
|
|
|
|
@Injectable()
|
|
export class UserKeypairService implements OnApplicationShutdown {
|
|
private cache: RedisKVCache<MiUserKeypair>;
|
|
|
|
constructor(
|
|
@Inject(DI.redis)
|
|
private redisClient: Redis.Redis,
|
|
@Inject(DI.redisForSub)
|
|
private redisForSub: Redis.Redis,
|
|
@Inject(DI.userKeypairsRepository)
|
|
private userKeypairsRepository: UserKeypairsRepository,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
) {
|
|
this.cache = new RedisKVCache<MiUserKeypair>(this.redisClient, 'userKeypair', {
|
|
lifetime: 1000 * 60 * 60 * 24, // 24h
|
|
memoryCacheLifetime: Infinity,
|
|
fetcher: (key) => this.userKeypairsRepository.findOneByOrFail({ userId: key }),
|
|
toRedisConverter: (value) => JSON.stringify(value),
|
|
fromRedisConverter: (value) => JSON.parse(value),
|
|
});
|
|
|
|
this.redisForSub.on('message', this.onMessage);
|
|
}
|
|
|
|
@bindThis
|
|
public async getUserKeypair(userId: MiUser['id']): Promise<MiUserKeypair> {
|
|
return await this.cache.fetch(userId);
|
|
}
|
|
|
|
@bindThis
|
|
public async refresh(userId: MiUser['id']): Promise<void> {
|
|
return await this.cache.refresh(userId);
|
|
}
|
|
|
|
@bindThis
|
|
public async prepareEd25519KeyPair(userId: MiUser['id']): Promise<void> {
|
|
await this.refresh(userId);
|
|
const keypair = await this.cache.fetch(userId);
|
|
if (keypair.ed25519PublicKey != null) return;
|
|
const ed25519 = await genEd25519KeyPair();
|
|
const ed25519PublicKeySignature = sign(ED25519_SIGN_ALGORITHM, Buffer.from(ed25519.publicKey), keypair.privateKey).toString('base64');
|
|
await this.userKeypairsRepository.update({ userId }, {
|
|
ed25519PublicKey: ed25519.publicKey,
|
|
ed25519PrivateKey: ed25519.privateKey,
|
|
ed25519PublicKeySignature,
|
|
ed25519SignatureAlgorithm: `rsa-${ED25519_SIGN_ALGORITHM}`,
|
|
});
|
|
this.globalEventService.publishInternalEvent('userKeypairUpdated', { userId });
|
|
}
|
|
|
|
@bindThis
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
|
const obj = JSON.parse(data);
|
|
|
|
if (obj.channel === 'internal') {
|
|
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
|
|
switch (type) {
|
|
case 'userKeypairUpdated': {
|
|
this.refresh(body.userId);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@bindThis
|
|
public dispose(): void {
|
|
this.cache.dispose();
|
|
}
|
|
|
|
@bindThis
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
this.dispose();
|
|
}
|
|
}
|