fix(license): keep signed offline keys across restarts and cloud sign-out
This commit is contained in:
parent
95aa95e986
commit
9d5d043e8b
7 changed files with 1157 additions and 482 deletions
118
apps/desktop/src/main/services/license/license-store.ts
Normal file
118
apps/desktop/src/main/services/license/license-store.ts
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
// src/main/services/license/license-store.ts
|
||||
// Port + file adapter for license persistence. AppConfig has no license fields, so the license
|
||||
// lives in its own JSON file (userData/d3ro-license.json).
|
||||
|
||||
import { getLogger } from '../LoggerService'
|
||||
|
||||
const logger = getLogger('license')
|
||||
|
||||
export const LICENSE_STORE_FILE = 'd3ro-license.json'
|
||||
|
||||
/** Every key the license store may hold. */
|
||||
export type LicenseStoreField =
|
||||
// machine
|
||||
| 'licenseMachineId'
|
||||
// key entitlement
|
||||
| 'licenseKey'
|
||||
| 'licenseKeyTier'
|
||||
| 'licenseActivatedAt'
|
||||
| 'licenseExpiresAt'
|
||||
| 'licenseCustomerEmail'
|
||||
| 'licenseIsTrial'
|
||||
| 'licenseTrialExpiresAt'
|
||||
| 'licenseOfflineGraceUntil'
|
||||
| 'licenseTrialEverStarted'
|
||||
// cloud entitlement
|
||||
| 'licenseCloudTier'
|
||||
| 'licenseCloudVerifiedAt'
|
||||
// combined view (kept for older app versions that read the same file)
|
||||
| 'licenseTier'
|
||||
| 'licenseLastVerifiedAt'
|
||||
|
||||
export type LicenseStoreEntries = Partial<Record<LicenseStoreField, unknown>>
|
||||
|
||||
export interface LicenseStore {
|
||||
read<T>(field: LicenseStoreField): T | null
|
||||
/** Writes several fields and persists them once. */
|
||||
writeAll(entries: LicenseStoreEntries): void
|
||||
}
|
||||
|
||||
function licenseFilePath(): string {
|
||||
const path = require('path') as typeof import('path')
|
||||
const electron = require('electron') as typeof import('electron')
|
||||
return path.join(electron.app.getPath('userData'), LICENSE_STORE_FILE)
|
||||
}
|
||||
|
||||
export class FileLicenseStore implements LicenseStore {
|
||||
private readonly _values = new Map<string, unknown>()
|
||||
private _loaded = false
|
||||
|
||||
read<T>(field: LicenseStoreField): T | null {
|
||||
this._ensureLoaded()
|
||||
const value = this._values.get(field)
|
||||
return (value as T | undefined) ?? null
|
||||
}
|
||||
|
||||
writeAll(entries: LicenseStoreEntries): void {
|
||||
this._ensureLoaded()
|
||||
for (const [field, value] of Object.entries(entries)) {
|
||||
this._values.set(field, value)
|
||||
}
|
||||
this._save()
|
||||
}
|
||||
|
||||
private _ensureLoaded(): void {
|
||||
if (this._loaded) return
|
||||
try {
|
||||
const fs = require('fs') as typeof import('fs')
|
||||
const filePath = licenseFilePath()
|
||||
if (fs.existsSync(filePath)) {
|
||||
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8')) as Record<string, unknown>
|
||||
for (const [k, v] of Object.entries(data)) {
|
||||
this._values.set(k, v)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// 파일 없으면 빈 상태로 시작
|
||||
}
|
||||
this._loaded = true
|
||||
}
|
||||
|
||||
private _save(): void {
|
||||
try {
|
||||
const fs = require('fs') as typeof import('fs')
|
||||
const obj: Record<string, unknown> = Object.fromEntries(this._values.entries())
|
||||
fs.writeFileSync(licenseFilePath(), JSON.stringify(obj, null, 2), 'utf-8')
|
||||
} catch (err) {
|
||||
logger.warn(`Failed to save license store: ${err}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** In-memory store — for tests and for callers that must not touch the disk. */
|
||||
export class MemoryLicenseStore implements LicenseStore {
|
||||
private readonly _values = new Map<string, unknown>()
|
||||
|
||||
read<T>(field: LicenseStoreField): T | null {
|
||||
return (this._values.get(field) as T | undefined) ?? null
|
||||
}
|
||||
|
||||
writeAll(entries: LicenseStoreEntries): void {
|
||||
for (const [field, value] of Object.entries(entries)) {
|
||||
this._values.set(field, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes the license file — test isolation only. */
|
||||
export function deleteLicenseStoreFile(): void {
|
||||
try {
|
||||
const fs = require('fs') as typeof import('fs')
|
||||
const filePath = licenseFilePath()
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.unlinkSync(filePath)
|
||||
}
|
||||
} catch {
|
||||
// 테스트 격리용 — 파일 없으면 무시
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue