mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 09:49:19 +08:00
refacor: 重构目录架构
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { Repository } from 'typeorm';
|
||||
import { LcAdmin } from '../entities/lc-admin.entity';
|
||||
|
||||
/** 首次启动时创建默认管理员 admin / admin123 */
|
||||
@Injectable()
|
||||
export class AdminSeedService implements OnModuleInit {
|
||||
constructor(
|
||||
@InjectRepository(LcAdmin)
|
||||
private readonly adminRepo: Repository<LcAdmin>,
|
||||
) {}
|
||||
|
||||
async onModuleInit() {
|
||||
const count = await this.adminRepo.count();
|
||||
if (count > 0) return;
|
||||
const password = await bcrypt.hash('admin123', 10);
|
||||
await this.adminRepo.save({
|
||||
username: 'admin',
|
||||
password,
|
||||
nickname: '默认管理员',
|
||||
status: 1,
|
||||
});
|
||||
console.log('[seed] 默认管理员已创建: admin / admin123');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { importPKCS8, SignJWT, type KeyLike } from 'jose';
|
||||
import { join } from 'path';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
export interface SignLicenseInput {
|
||||
sub: string;
|
||||
company: string;
|
||||
machineFingerprint: string;
|
||||
edition: string;
|
||||
features: string[];
|
||||
expiresAt: Date;
|
||||
maxCompanies?: number;
|
||||
}
|
||||
|
||||
export interface SignLicenseResult {
|
||||
licenseKey: string;
|
||||
jti: string;
|
||||
issuedAt: Date;
|
||||
expiresAt: Date;
|
||||
}
|
||||
|
||||
/** 使用 Ed25519 私钥签发 License JWT */
|
||||
@Injectable()
|
||||
export class JwtSignerService implements OnModuleInit {
|
||||
private privateKey: KeyLike | null = null;
|
||||
private kid = process.env.LICENSE_KID || '2026-v1';
|
||||
|
||||
async onModuleInit() {
|
||||
const keyPath =
|
||||
process.env.LICENSE_PRIVATE_KEY_PATH || join(process.cwd(), 'keys', 'private.pem');
|
||||
try {
|
||||
const pem = await readFile(keyPath, 'utf8');
|
||||
this.privateKey = await importPKCS8(pem, 'EdDSA');
|
||||
} catch (e) {
|
||||
console.warn(`[license] 未加载私钥 ${keyPath},签发功能不可用。请运行: openssl genpkey -algorithm Ed25519 -out keys/private.pem`);
|
||||
}
|
||||
}
|
||||
|
||||
async signLicense(input: SignLicenseInput): Promise<SignLicenseResult> {
|
||||
if (!this.privateKey) {
|
||||
throw new Error('License 私钥未配置,无法签发');
|
||||
}
|
||||
const jti = uuidv4();
|
||||
const issuedAt = new Date();
|
||||
const claims: Record<string, unknown> = {
|
||||
company: input.company,
|
||||
machineFingerprint: input.machineFingerprint,
|
||||
edition: input.edition,
|
||||
features: input.features,
|
||||
};
|
||||
if (input.maxCompanies != null && input.maxCompanies > 0) {
|
||||
claims.maxCompanies = input.maxCompanies;
|
||||
}
|
||||
const licenseKey = await new SignJWT(claims)
|
||||
.setProtectedHeader({ alg: 'EdDSA', typ: 'JWT', kid: this.kid })
|
||||
.setIssuer('ecom-license-server')
|
||||
.setSubject(input.sub)
|
||||
.setJti(jti)
|
||||
.setIssuedAt(issuedAt)
|
||||
.setExpirationTime(input.expiresAt)
|
||||
.sign(this.privateKey);
|
||||
|
||||
return { licenseKey, jti, issuedAt, expiresAt: input.expiresAt };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user