@ksevendev/sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/dist/KSevenWebLicenseSdk.d.ts +147 -0
- package/dist/KSevenWebLicenseSdk.js +1208 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +156 -0
- package/dist/crypto/KSevenCodec.d.ts +98 -0
- package/dist/crypto/KSevenCodec.js +502 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/storage/EncryptedStateStore.d.ts +8 -0
- package/dist/storage/EncryptedStateStore.js +40 -0
- package/dist/types.d.ts +318 -0
- package/dist/types.js +16 -0
- package/package.json +38 -0
- package/src/KSevenWebLicenseSdk.ts +1565 -0
- package/src/cli.ts +197 -0
- package/src/crypto/KSevenCodec.ts +808 -0
- package/src/index.ts +4 -0
- package/src/storage/EncryptedStateStore.ts +41 -0
- package/src/types.ts +353 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { KSevenCodec } from '../crypto/KSevenCodec.js';
|
|
5
|
+
import { WebSdkPersistedState, WebSdkStateStore } from '../types.js';
|
|
6
|
+
|
|
7
|
+
export class EncryptedStateStore implements WebSdkStateStore {
|
|
8
|
+
private readonly filePath: string;
|
|
9
|
+
private readonly secret?: string;
|
|
10
|
+
|
|
11
|
+
constructor(filePath: string, secret?: string) {
|
|
12
|
+
this.filePath = path.resolve(filePath);
|
|
13
|
+
this.secret = secret;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public async read(): Promise<WebSdkPersistedState | null> {
|
|
17
|
+
try {
|
|
18
|
+
const content = await fs.readFile(this.filePath, 'utf8');
|
|
19
|
+
if (!content.trim()) return null;
|
|
20
|
+
return KSevenCodec.decodeEncryptedState<WebSdkPersistedState>(content, this.secret);
|
|
21
|
+
} catch (err: any) {
|
|
22
|
+
if (err?.code === 'ENOENT') return null;
|
|
23
|
+
throw err;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public async write(state: WebSdkPersistedState | null): Promise<void> {
|
|
28
|
+
if (!state) {
|
|
29
|
+
await fs.unlink(this.filePath).catch((err: any) => {
|
|
30
|
+
if (err?.code !== 'ENOENT') throw err;
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
|
|
35
|
+
const sealed = KSevenCodec.encodeEncryptedState(state, this.secret);
|
|
36
|
+
const tmpPath = `${this.filePath}.tmp-${crypto.randomBytes(4).toString('hex')}`;
|
|
37
|
+
await fs.writeFile(tmpPath, sealed, { encoding: 'utf8', mode: 0o600 });
|
|
38
|
+
await fs.rename(tmpPath, this.filePath);
|
|
39
|
+
await fs.chmod(this.filePath, 0o600).catch(() => {});
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
export type WebSdkPlatform = 'nodejs' | 'php' | 'wordpress' | 'universal';
|
|
2
|
+
export type WebSdkChannel = 'stable' | 'beta';
|
|
3
|
+
export type WebSdkEnvironment = 'production' | 'staging' | 'development';
|
|
4
|
+
|
|
5
|
+
export interface WebKeyTransitionProof {
|
|
6
|
+
schema: 'k7web-key-transition|v1';
|
|
7
|
+
fromKeyId: string;
|
|
8
|
+
fromKeyVersion: number;
|
|
9
|
+
toKeyId: string;
|
|
10
|
+
toKeyVersion: number;
|
|
11
|
+
toPublicKeyRawHex: string;
|
|
12
|
+
issuedAt: string;
|
|
13
|
+
expiresAt: string;
|
|
14
|
+
reason: string;
|
|
15
|
+
signatureBase64: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface WebSigningKeyEntry {
|
|
19
|
+
keyId: string;
|
|
20
|
+
role: 'ACTIVE' | 'SECONDARY' | 'RETIRED';
|
|
21
|
+
keyVersion: number;
|
|
22
|
+
algorithm: 'Ed25519';
|
|
23
|
+
publicKeyPem: string;
|
|
24
|
+
publicKeyRawHex: string;
|
|
25
|
+
rootProof: WebKeyTransitionProof | null;
|
|
26
|
+
transitionProof: WebKeyTransitionProof | null;
|
|
27
|
+
createdAt?: string;
|
|
28
|
+
rotatedAt?: string | null;
|
|
29
|
+
expiresAt?: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WebPublicKeyBundle {
|
|
33
|
+
schema: 'k7-web-keys-v1';
|
|
34
|
+
algorithm: 'Ed25519';
|
|
35
|
+
protocolVersion: string;
|
|
36
|
+
rootKey: {
|
|
37
|
+
keyId: string;
|
|
38
|
+
algorithm: 'Ed25519';
|
|
39
|
+
publicKeyPem: string;
|
|
40
|
+
publicKeyRawHex: string;
|
|
41
|
+
mode?: string;
|
|
42
|
+
};
|
|
43
|
+
activeKey: WebSigningKeyEntry;
|
|
44
|
+
secondaryKeys: WebSigningKeyEntry[];
|
|
45
|
+
keys: WebSigningKeyEntry[];
|
|
46
|
+
issuedAt: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface WebRuntimeSignatureEnvelope {
|
|
50
|
+
schema: 'k7-web-response-sig-v1';
|
|
51
|
+
protocolVersion: string;
|
|
52
|
+
algorithm: 'Ed25519';
|
|
53
|
+
keyId: string;
|
|
54
|
+
keyVersion: number;
|
|
55
|
+
rootKeyId: string;
|
|
56
|
+
requestId: string;
|
|
57
|
+
nonce: string;
|
|
58
|
+
method: string;
|
|
59
|
+
path: string;
|
|
60
|
+
timestamp: string;
|
|
61
|
+
httpStatus: number;
|
|
62
|
+
decision: 'ALLOW' | 'DENY';
|
|
63
|
+
code: string;
|
|
64
|
+
productSlug: string | null;
|
|
65
|
+
licenseId: number | null;
|
|
66
|
+
installationId: string | null;
|
|
67
|
+
domain: string | null;
|
|
68
|
+
licenseStatus: string | null;
|
|
69
|
+
expiresAt: string | null;
|
|
70
|
+
entitlementHash: string | null;
|
|
71
|
+
payloadSha256: string;
|
|
72
|
+
signatureBase64: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface WebEcdhHandshakeSignature {
|
|
76
|
+
schema: 'k7-web-ecdh-sig-v1';
|
|
77
|
+
algorithm: 'Ed25519';
|
|
78
|
+
keyId: string;
|
|
79
|
+
keyVersion: number;
|
|
80
|
+
rootKeyId: string;
|
|
81
|
+
timestamp: string;
|
|
82
|
+
nonce: string;
|
|
83
|
+
signatureBase64: string;
|
|
84
|
+
publicKeyPem: string;
|
|
85
|
+
publicKeyRawHex: string;
|
|
86
|
+
rootProof: WebKeyTransitionProof | null;
|
|
87
|
+
transitionProof: WebKeyTransitionProof | null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type WebSdkReleaseKind = 'PRODUCT' | 'UPDATE' | 'PLUGIN';
|
|
91
|
+
export type WebSdkDomainEnvironment = 'PRODUCTION' | 'STAGING' | 'TEST';
|
|
92
|
+
export type WebSdkLicenseKind = 'FREE' | 'TRIAL' | 'ONE_TIME' | 'SUBSCRIPTION' | 'LIFETIME';
|
|
93
|
+
|
|
94
|
+
export interface WebOfflineValidationPolicy {
|
|
95
|
+
allowed: boolean;
|
|
96
|
+
gracePeriodHours: number;
|
|
97
|
+
graceUntil: string | null;
|
|
98
|
+
hardRevokeStatuses?: string[];
|
|
99
|
+
maxOfflineDays?: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface WebSdkPersistedState {
|
|
103
|
+
apiBaseUrl: string;
|
|
104
|
+
installationId: string;
|
|
105
|
+
installationCredential: string;
|
|
106
|
+
credentialPrefix?: string;
|
|
107
|
+
credentialVersion?: number;
|
|
108
|
+
domain: string;
|
|
109
|
+
platform: WebSdkPlatform;
|
|
110
|
+
appVersion: string;
|
|
111
|
+
environment: string;
|
|
112
|
+
instanceFingerprint: string;
|
|
113
|
+
installedAt: string;
|
|
114
|
+
updatedAt: string;
|
|
115
|
+
pinnedPublicKeys?: Record<string, string>;
|
|
116
|
+
pinnedRootPublicKeyPem?: string;
|
|
117
|
+
clientPublicKeyPem?: string;
|
|
118
|
+
clientPrivateKeyPem?: string;
|
|
119
|
+
requireSealedTransport?: boolean;
|
|
120
|
+
protocolVersion?: string;
|
|
121
|
+
lastValidatedAt?: string;
|
|
122
|
+
graceUntil?: string | null;
|
|
123
|
+
lastSignedEntitlement?: Record<string, any> | null;
|
|
124
|
+
lastSignedValidationResponse?: Record<string, any> | null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface WebSdkStateStore {
|
|
128
|
+
read: () => Promise<WebSdkPersistedState | null>;
|
|
129
|
+
write: (state: WebSdkPersistedState | null) => Promise<void>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface KSevenWebSdkConfig {
|
|
133
|
+
apiBaseUrl: string;
|
|
134
|
+
/** Caminho do arquivo local cifrado (padrão: .kseven-web-license.dat) */
|
|
135
|
+
stateFilePath?: string;
|
|
136
|
+
platform?: WebSdkPlatform;
|
|
137
|
+
timeoutMs?: number;
|
|
138
|
+
/** Permite HTTP sem TLS apenas em desenvolvimento/testes locais */
|
|
139
|
+
allowInsecureLocalHttp?: boolean;
|
|
140
|
+
/** Chave/segredo adicional para cifrar o estado local (AES-256-GCM). Se omitido, deriva chave vinculada ao host. */
|
|
141
|
+
stateSecret?: string;
|
|
142
|
+
/** Habilita envelope cifrado ECDH P-256 + AES-256-GCM em trânsito (padrão: true). */
|
|
143
|
+
useEncryptedTransport?: boolean;
|
|
144
|
+
/** Quando true, falha fechado se a negociação ECDH k7seal.v1 não puder ser concluída. */
|
|
145
|
+
requireSealedTransport?: boolean;
|
|
146
|
+
/** Chave pública raiz Ed25519 pinada do domínio web para validar cadeia ativa/secundária e assinaturas de runtime. */
|
|
147
|
+
pinnedRootPublicKeyPem?: string;
|
|
148
|
+
/** Verifica signatureEnvelope Ed25519 em respostas ALLOW/DENY e handshake ECDH (padrão: true). */
|
|
149
|
+
verifyResponseSignatures?: boolean;
|
|
150
|
+
/** Chave(s) pública(s) Ed25519 pinadas por keyId (opcional; caso omitido busca /keys) */
|
|
151
|
+
pinnedPublicKeys?: Record<string, string>;
|
|
152
|
+
/** Chave pública/privada Ed25519 opcional da instalação para assinatura assimétrica de requisições. */
|
|
153
|
+
clientPublicKeyPem?: string;
|
|
154
|
+
clientPrivateKeyPem?: string;
|
|
155
|
+
/** Persistência customizada (ex.: banco/cofre) em substituição ao arquivo local */
|
|
156
|
+
stateStore?: WebSdkStateStore;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface InstallOptions {
|
|
160
|
+
licenseKey: string;
|
|
161
|
+
domain: string;
|
|
162
|
+
appVersion: string;
|
|
163
|
+
platform?: WebSdkPlatform;
|
|
164
|
+
environment?: WebSdkEnvironment;
|
|
165
|
+
instanceFingerprint?: string;
|
|
166
|
+
idempotencyKey?: string;
|
|
167
|
+
/** Gera automaticamente par Ed25519 por instalação e registra a chave pública no servidor. */
|
|
168
|
+
generateClientKeyPair?: boolean;
|
|
169
|
+
clientPublicKeyPem?: string;
|
|
170
|
+
clientPrivateKeyPem?: string;
|
|
171
|
+
/** Exige transporte selado k7seal.v1 para esta instalação no servidor. */
|
|
172
|
+
requireSealedTransport?: boolean;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface ValidateOptions {
|
|
176
|
+
/** Quando true, permite validar localmente usando o último entitlement assinado dentro do graceUntil em caso de falha de rede/503. */
|
|
177
|
+
allowOfflineGrace?: boolean;
|
|
178
|
+
/** Timestamp customizado em ms (útil para testes). */
|
|
179
|
+
nowMs?: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface OfflineValidationResult {
|
|
183
|
+
valid: boolean;
|
|
184
|
+
offline: true;
|
|
185
|
+
inGracePeriod: boolean;
|
|
186
|
+
graceUntil: string;
|
|
187
|
+
lastValidatedAt: string | null;
|
|
188
|
+
installationId: string;
|
|
189
|
+
domain: string;
|
|
190
|
+
entitlement: Record<string, any>;
|
|
191
|
+
signatureKeyId: string;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface TransferInstallationOptions {
|
|
195
|
+
toDomain: string;
|
|
196
|
+
reason?: string;
|
|
197
|
+
immediateActivate?: boolean;
|
|
198
|
+
platform?: WebSdkPlatform;
|
|
199
|
+
appVersion?: string;
|
|
200
|
+
instanceFingerprint?: string;
|
|
201
|
+
clientPublicKeyPem?: string;
|
|
202
|
+
requireSealedTransport?: boolean;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface CheckVersionOptions {
|
|
206
|
+
channel?: WebSdkChannel;
|
|
207
|
+
currentVersion?: string;
|
|
208
|
+
releaseKind?: WebSdkReleaseKind;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface ReleaseSummary {
|
|
212
|
+
releaseId: number;
|
|
213
|
+
productSlug: string;
|
|
214
|
+
version: string;
|
|
215
|
+
channel: WebSdkChannel;
|
|
216
|
+
platform: string;
|
|
217
|
+
releaseKind?: WebSdkReleaseKind;
|
|
218
|
+
minCompatibleVersion: string | null;
|
|
219
|
+
maxCompatibleVersion?: string | null;
|
|
220
|
+
allowRollback?: boolean;
|
|
221
|
+
requiredAddonCodes?: string[];
|
|
222
|
+
changelog: string;
|
|
223
|
+
packageSizeBytes: number;
|
|
224
|
+
packageSha256: string;
|
|
225
|
+
manifestJson: string;
|
|
226
|
+
manifestSignature: string;
|
|
227
|
+
signingKeyId: string;
|
|
228
|
+
publicKeyPem?: string;
|
|
229
|
+
publishedAt: string | null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export interface UpdateOptions {
|
|
233
|
+
channel?: WebSdkChannel;
|
|
234
|
+
currentVersion?: string;
|
|
235
|
+
releaseKind?: WebSdkReleaseKind;
|
|
236
|
+
destDir?: string;
|
|
237
|
+
downloadFileName?: string;
|
|
238
|
+
/** Handler opcional para aplicar pacote verificado (ex.: extração/restart) */
|
|
239
|
+
onVerifiedPackage?: (ctx: {
|
|
240
|
+
packageBuffer: Buffer;
|
|
241
|
+
release: ReleaseSummary;
|
|
242
|
+
verifiedSha256: string;
|
|
243
|
+
}) => Promise<void>;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export interface RollbackOptions {
|
|
247
|
+
channel?: WebSdkChannel;
|
|
248
|
+
destDir?: string;
|
|
249
|
+
downloadFileName?: string;
|
|
250
|
+
onVerifiedPackage?: UpdateOptions['onVerifiedPackage'];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface CheckVersionResult {
|
|
254
|
+
updateAvailable: boolean;
|
|
255
|
+
channel: WebSdkChannel;
|
|
256
|
+
currentVersion: string;
|
|
257
|
+
latestRelease: ReleaseSummary | null;
|
|
258
|
+
availableRollbacks?: ReleaseSummary[];
|
|
259
|
+
updatesActive?: boolean;
|
|
260
|
+
updatesExpiresAt?: string | null;
|
|
261
|
+
signatureVerified: boolean;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface UpdateResult {
|
|
265
|
+
updated: boolean;
|
|
266
|
+
previousVersion: string;
|
|
267
|
+
currentVersion: string;
|
|
268
|
+
release: ReleaseSummary | null;
|
|
269
|
+
savedFilePath?: string;
|
|
270
|
+
verifiedSha256?: string;
|
|
271
|
+
signatureVerified: boolean;
|
|
272
|
+
rollback?: boolean;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export interface AutoUpdateMigration {
|
|
276
|
+
id: string;
|
|
277
|
+
run: (ctx: { targetDir: string; fromVersion: string; toVersion: string }) => Promise<void>;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export interface AutoUpdateOptions {
|
|
281
|
+
channel?: WebSdkChannel;
|
|
282
|
+
currentVersion?: string;
|
|
283
|
+
releaseKind?: WebSdkReleaseKind;
|
|
284
|
+
/** Diretório raiz da aplicação alvo a ser atualizado de forma transacional. */
|
|
285
|
+
targetDir: string;
|
|
286
|
+
/** Diretório opcional para backup pré-atualização (padrão: sibling temp backup dir). */
|
|
287
|
+
backupDir?: string;
|
|
288
|
+
/** Caminhos relativos preservados durante a aplicação (ex.: ['.env', '.kseven-web-license.dat']). */
|
|
289
|
+
preserveRelativePaths?: string[];
|
|
290
|
+
/** Função que extrai/aplica o pacote verificado no diretório temporário ou alvo. */
|
|
291
|
+
applyPackage: (ctx: {
|
|
292
|
+
stagingPackagePath: string;
|
|
293
|
+
packageBuffer: Buffer;
|
|
294
|
+
targetDir: string;
|
|
295
|
+
release: ReleaseSummary;
|
|
296
|
+
verifiedSha256: string;
|
|
297
|
+
}) => Promise<void>;
|
|
298
|
+
/** Migrações tipadas declaradas executadas em ordem após aplicação; erro aciona rollback completo. */
|
|
299
|
+
migrations?: AutoUpdateMigration[];
|
|
300
|
+
/** Reporta progresso e resultado para o painel via /updates/auto-update/report (padrão: true). */
|
|
301
|
+
reportProgress?: boolean;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export interface AutoUpdateResult {
|
|
305
|
+
runUid: string;
|
|
306
|
+
updated: boolean;
|
|
307
|
+
status: 'UP_TO_DATE' | 'COMPLETED' | 'ROLLED_BACK' | 'FAILED';
|
|
308
|
+
previousVersion: string;
|
|
309
|
+
currentVersion: string;
|
|
310
|
+
targetVersion: string | null;
|
|
311
|
+
release: ReleaseSummary | null;
|
|
312
|
+
signatureVerified: boolean;
|
|
313
|
+
sha256Verified: boolean;
|
|
314
|
+
backupCreated: boolean;
|
|
315
|
+
rollbackPerformed: boolean;
|
|
316
|
+
migrationsExecuted: string[];
|
|
317
|
+
sanitizedError: string | null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export interface VerifiedManifestPayload {
|
|
321
|
+
schema: string;
|
|
322
|
+
webProductId: number;
|
|
323
|
+
productSlug: string;
|
|
324
|
+
channel: string;
|
|
325
|
+
platform: string;
|
|
326
|
+
version: string;
|
|
327
|
+
minCompatibleVersion: string | null;
|
|
328
|
+
packageSha256: string;
|
|
329
|
+
packageSizeBytes: number;
|
|
330
|
+
issuedAt: string;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export class KSevenWebSdkError extends Error {
|
|
334
|
+
public readonly statusCode: number;
|
|
335
|
+
public readonly code: string;
|
|
336
|
+
public readonly requestId?: string;
|
|
337
|
+
public readonly details?: Record<string, any>;
|
|
338
|
+
|
|
339
|
+
constructor(
|
|
340
|
+
statusCode: number,
|
|
341
|
+
code: string,
|
|
342
|
+
message: string,
|
|
343
|
+
requestId?: string,
|
|
344
|
+
details?: Record<string, any>
|
|
345
|
+
) {
|
|
346
|
+
super(message);
|
|
347
|
+
this.name = 'KSevenWebSdkError';
|
|
348
|
+
this.statusCode = statusCode;
|
|
349
|
+
this.code = code;
|
|
350
|
+
if (requestId) this.requestId = requestId;
|
|
351
|
+
if (details) this.details = details;
|
|
352
|
+
}
|
|
353
|
+
}
|