@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/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { KSevenWebLicenseSdk, KSevenWebSdkError } from './index.js';
|
|
3
|
+
function parseArgs(argv) {
|
|
4
|
+
const [command = 'help', ...rest] = argv;
|
|
5
|
+
const flags = {};
|
|
6
|
+
for (let i = 0; i < rest.length; i++) {
|
|
7
|
+
const token = rest[i];
|
|
8
|
+
if (!token.startsWith('--'))
|
|
9
|
+
continue;
|
|
10
|
+
const key = token.slice(2);
|
|
11
|
+
const next = rest[i + 1];
|
|
12
|
+
if (!next || next.startsWith('--')) {
|
|
13
|
+
flags[key] = true;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
flags[key] = next;
|
|
17
|
+
i++;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return { command, flags };
|
|
21
|
+
}
|
|
22
|
+
function getStringFlag(flags, key, envFallback) {
|
|
23
|
+
const val = flags[key];
|
|
24
|
+
if (typeof val === 'string' && val.trim())
|
|
25
|
+
return val.trim();
|
|
26
|
+
if (envFallback && process.env[envFallback]?.trim())
|
|
27
|
+
return process.env[envFallback].trim();
|
|
28
|
+
return '';
|
|
29
|
+
}
|
|
30
|
+
async function main() {
|
|
31
|
+
const { command, flags } = parseArgs(process.argv.slice(2));
|
|
32
|
+
if (command === 'help' || command === '--help' || command === '-h') {
|
|
33
|
+
console.log(JSON.stringify({
|
|
34
|
+
usage: 'k7web <install|check-version|releases|update|rollback|validate|validate-offline|transfer|entitlements|uninstall> [opções]',
|
|
35
|
+
commands: {
|
|
36
|
+
install: '--api <url> --key <licenseKey> --domain <host> --app-version <semver> [--platform nodejs|php|wordpress|universal] [--env production|staging|development] [--state-file .kseven-web-license.dat] [--state-secret <chave>]',
|
|
37
|
+
'check-version': '--api <url> [--channel stable|beta] [--release-kind PRODUCT|UPDATE|PLUGIN] [--app-version <semver>] [--state-file ...]',
|
|
38
|
+
releases: '--api <url> [--channel stable|beta] [--release-kind PRODUCT|UPDATE|PLUGIN] [--state-file ...]',
|
|
39
|
+
update: '--api <url> [--channel stable|beta] [--release-kind PRODUCT|UPDATE|PLUGIN] [--dest-dir ./updates] [--state-file ...]',
|
|
40
|
+
rollback: '--api <url> --release-id <id> [--dest-dir ./updates] [--state-file ...]',
|
|
41
|
+
validate: '--api <url> [--app-version <semver>] [--allow-offline-grace] [--state-file ...]',
|
|
42
|
+
'validate-offline': '--state-file ... [--state-secret <chave>]',
|
|
43
|
+
transfer: '--api <url> --to-domain <host> [--reason "..."] [--immediate-activate] [--state-file ...]',
|
|
44
|
+
entitlements: '--api <url> [--state-file ...]',
|
|
45
|
+
uninstall: '--api <url> [--reason "..."] [--state-file ...]'
|
|
46
|
+
}
|
|
47
|
+
}, null, 2));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const apiBaseUrl = getStringFlag(flags, 'api', 'K7_WEB_API_URL') ||
|
|
51
|
+
(command === 'validate-offline' ? 'https://offline.local' : '');
|
|
52
|
+
const stateFilePath = getStringFlag(flags, 'state-file', 'K7_WEB_STATE_FILE') || undefined;
|
|
53
|
+
const stateSecret = getStringFlag(flags, 'state-secret', 'K7_WEB_STATE_SECRET') || undefined;
|
|
54
|
+
const allowInsecureLocalHttp = Boolean(flags['allow-local-http'] || process.env.K7_ALLOW_LOCAL_HTTP === '1');
|
|
55
|
+
const useEncryptedTransport = flags['plain-transport'] ? false : true;
|
|
56
|
+
const requireSealedTransport = Boolean(flags['require-sealed'] || process.env.K7_WEB_REQUIRE_SEALED === '1');
|
|
57
|
+
const pinnedRootPublicKeyPem = getStringFlag(flags, 'root-key-pem', 'K7_WEB_ROOT_PUBLIC_KEY_PEM').replace(/\\n/g, '\n') || undefined;
|
|
58
|
+
const platform = (getStringFlag(flags, 'platform') || 'nodejs');
|
|
59
|
+
const sdk = new KSevenWebLicenseSdk({
|
|
60
|
+
apiBaseUrl,
|
|
61
|
+
stateFilePath,
|
|
62
|
+
stateSecret,
|
|
63
|
+
platform,
|
|
64
|
+
allowInsecureLocalHttp,
|
|
65
|
+
useEncryptedTransport,
|
|
66
|
+
requireSealedTransport,
|
|
67
|
+
pinnedRootPublicKeyPem
|
|
68
|
+
});
|
|
69
|
+
if (command === 'install') {
|
|
70
|
+
const res = await sdk.install({
|
|
71
|
+
licenseKey: getStringFlag(flags, 'key', 'K7_WEB_LICENSE_KEY'),
|
|
72
|
+
domain: getStringFlag(flags, 'domain', 'K7_WEB_DOMAIN'),
|
|
73
|
+
appVersion: getStringFlag(flags, 'app-version') || '1.0.0',
|
|
74
|
+
platform,
|
|
75
|
+
environment: (getStringFlag(flags, 'env') || 'production'),
|
|
76
|
+
instanceFingerprint: getStringFlag(flags, 'fingerprint') || undefined,
|
|
77
|
+
generateClientKeyPair: Boolean(flags['client-keypair']),
|
|
78
|
+
requireSealedTransport
|
|
79
|
+
});
|
|
80
|
+
console.log(JSON.stringify(res, null, 2));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (command === 'check-version') {
|
|
84
|
+
const channel = (getStringFlag(flags, 'channel') || 'stable');
|
|
85
|
+
const currentVersion = getStringFlag(flags, 'app-version') || undefined;
|
|
86
|
+
const releaseKind = (getStringFlag(flags, 'release-kind') || undefined);
|
|
87
|
+
const res = await sdk.checkVersion({ channel, currentVersion, releaseKind });
|
|
88
|
+
console.log(JSON.stringify(res, null, 2));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (command === 'releases') {
|
|
92
|
+
const channel = (getStringFlag(flags, 'channel') || 'stable');
|
|
93
|
+
const currentVersion = getStringFlag(flags, 'app-version') || undefined;
|
|
94
|
+
const releaseKind = (getStringFlag(flags, 'release-kind') || undefined);
|
|
95
|
+
const res = await sdk.listCompatibleReleases({ channel, currentVersion, releaseKind });
|
|
96
|
+
console.log(JSON.stringify(res, null, 2));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (command === 'update') {
|
|
100
|
+
const channel = (getStringFlag(flags, 'channel') || 'stable');
|
|
101
|
+
const destDir = getStringFlag(flags, 'dest-dir') || undefined;
|
|
102
|
+
const releaseKind = (getStringFlag(flags, 'release-kind') || undefined);
|
|
103
|
+
const res = await sdk.update({ channel, destDir, releaseKind });
|
|
104
|
+
console.log(JSON.stringify(res, null, 2));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (command === 'rollback') {
|
|
108
|
+
const releaseId = Number(getStringFlag(flags, 'release-id'));
|
|
109
|
+
const destDir = getStringFlag(flags, 'dest-dir') || undefined;
|
|
110
|
+
const channel = (getStringFlag(flags, 'channel') || 'stable');
|
|
111
|
+
const res = await sdk.rollback(releaseId, { channel, destDir });
|
|
112
|
+
console.log(JSON.stringify(res, null, 2));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (command === 'validate') {
|
|
116
|
+
const appVersion = getStringFlag(flags, 'app-version') || undefined;
|
|
117
|
+
const allowOfflineGrace = Boolean(flags['allow-offline-grace']);
|
|
118
|
+
const res = await sdk.validate(appVersion, { allowOfflineGrace });
|
|
119
|
+
console.log(JSON.stringify(res, null, 2));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (command === 'validate-offline') {
|
|
123
|
+
const res = await sdk.validateOffline();
|
|
124
|
+
console.log(JSON.stringify(res, null, 2));
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (command === 'transfer') {
|
|
128
|
+
const toDomain = getStringFlag(flags, 'to-domain');
|
|
129
|
+
const reason = getStringFlag(flags, 'reason') || undefined;
|
|
130
|
+
const immediateActivate = Boolean(flags['immediate-activate']);
|
|
131
|
+
const res = await sdk.transferInstallation({ toDomain, reason, immediateActivate });
|
|
132
|
+
console.log(JSON.stringify(res, null, 2));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (command === 'entitlements') {
|
|
136
|
+
const res = await sdk.entitlements();
|
|
137
|
+
console.log(JSON.stringify(res, null, 2));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (command === 'uninstall') {
|
|
141
|
+
const reason = getStringFlag(flags, 'reason') || 'CLI_UNINSTALL';
|
|
142
|
+
const res = await sdk.uninstall(reason);
|
|
143
|
+
console.log(JSON.stringify(res, null, 2));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
throw new KSevenWebSdkError(400, 'UNKNOWN_COMMAND', `Comando desconhecido: ${command}`);
|
|
147
|
+
}
|
|
148
|
+
main().catch((err) => {
|
|
149
|
+
console.error(JSON.stringify({
|
|
150
|
+
error: true,
|
|
151
|
+
code: err?.code || 'CLI_ERROR',
|
|
152
|
+
statusCode: err?.statusCode || 1,
|
|
153
|
+
message: err?.message || 'Erro desconhecido'
|
|
154
|
+
}, null, 2));
|
|
155
|
+
process.exit(1);
|
|
156
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { VerifiedManifestPayload, WebEcdhHandshakeSignature, WebKeyTransitionProof, WebPublicKeyBundle } from '../types.js';
|
|
3
|
+
export interface ActiveWireSession {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
aesKey: Buffer;
|
|
6
|
+
expiresAtMs: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class KSevenCodec {
|
|
9
|
+
/**
|
|
10
|
+
* Deriva chave local determinística para este host/processo caso `stateSecret` não seja fornecido explicitamente.
|
|
11
|
+
*/
|
|
12
|
+
static resolveDefaultStateSecret(customSecret?: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Serialização JSON determinística (chaves ordenadas recursivamente) idêntica à do backend.
|
|
15
|
+
*/
|
|
16
|
+
static canonicalizeJson(value: any): string;
|
|
17
|
+
static sha256CanonicalJson(value: any): string;
|
|
18
|
+
static extractEd25519RawPublicKeyHex(publicKeyPem: string): string;
|
|
19
|
+
static generateClientEd25519KeyPair(): {
|
|
20
|
+
clientPublicKeyPem: string;
|
|
21
|
+
clientPrivateKeyPem: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Cifra e codifica estado local no formato `k7st.v1.<saltB64Url>.<ivB64Url>.<tagB64Url>.<cipherB64Url>`
|
|
25
|
+
* usando AES-256-GCM + HKDF-SHA256.
|
|
26
|
+
*/
|
|
27
|
+
static encodeEncryptedState(stateObj: unknown, secretInput?: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Decifra estado local `k7st.v1...` (ou lê JSON legado caso exista arquivo anterior à migração).
|
|
30
|
+
*/
|
|
31
|
+
static decodeEncryptedState<T = any>(rawContent: string, secretInput?: string): T;
|
|
32
|
+
/**
|
|
33
|
+
* Inicia par de chaves efêmero ECDH P-256 para negociação de sessão cifrada de transporte.
|
|
34
|
+
*/
|
|
35
|
+
static createClientEphemeralKeyPair(): {
|
|
36
|
+
clientPublicKeySpkiBase64: string;
|
|
37
|
+
privateKey: crypto.KeyObject;
|
|
38
|
+
};
|
|
39
|
+
static deriveWireSessionFromServerResponse(privateKey: crypto.KeyObject, sessionId: string, serverPublicKeySpkiBase64: string, expiresInSeconds?: number): ActiveWireSession;
|
|
40
|
+
static sealWirePayload(payload: unknown, aesKey: Buffer, aad: string): string;
|
|
41
|
+
static unsealWirePayload(sealedEnvelope: string, aesKey: Buffer, aad: string): any;
|
|
42
|
+
static verifyKeyTransitionProof(proof: WebKeyTransitionProof | null | undefined, signerPublicKeyPem: string, expectedToKeyId?: string, expectedToPublicKeyRawHex?: string, nowMs?: number): boolean;
|
|
43
|
+
static resolveTrustedKeysFromBundle(bundle: Partial<WebPublicKeyBundle>, pinnedRootPublicKeyPem?: string, explicitPinnedKeys?: Record<string, string>, nowMs?: number): Record<string, string>;
|
|
44
|
+
static verifyEcdhHandshakeSignature(params: {
|
|
45
|
+
sessionId: string;
|
|
46
|
+
serverPublicKeySpkiBase64: string;
|
|
47
|
+
fingerprintSha256: string;
|
|
48
|
+
clientPublicKeySpkiBase64: string;
|
|
49
|
+
expiresInSeconds: number;
|
|
50
|
+
expectedNonce: string;
|
|
51
|
+
handshakeSignature: WebEcdhHandshakeSignature | undefined;
|
|
52
|
+
trustedKeys: Record<string, string>;
|
|
53
|
+
maxAgeSeconds?: number;
|
|
54
|
+
}): void;
|
|
55
|
+
static verifyRuntimeResponseSignature(params: {
|
|
56
|
+
responseBody: Record<string, any>;
|
|
57
|
+
trustedKeys: Record<string, string>;
|
|
58
|
+
expectedMethod: string;
|
|
59
|
+
expectedPath: string;
|
|
60
|
+
expectedHttpStatus: number;
|
|
61
|
+
expectedNonce?: string;
|
|
62
|
+
maxAgeSeconds?: number;
|
|
63
|
+
}): {
|
|
64
|
+
keyId: string;
|
|
65
|
+
};
|
|
66
|
+
static signClientRequest(params: {
|
|
67
|
+
clientPrivateKeyPem: string;
|
|
68
|
+
method: string;
|
|
69
|
+
path: string;
|
|
70
|
+
timestamp: string;
|
|
71
|
+
nonce: string;
|
|
72
|
+
installationId: string;
|
|
73
|
+
body?: Record<string, any>;
|
|
74
|
+
}): string;
|
|
75
|
+
static verifyEd25519Manifest(params: {
|
|
76
|
+
manifestJson: string;
|
|
77
|
+
manifestSignature: string;
|
|
78
|
+
signingKeyId: string;
|
|
79
|
+
publicKeyPem: string;
|
|
80
|
+
}): VerifiedManifestPayload;
|
|
81
|
+
/**
|
|
82
|
+
* Valida offline o último `signatureEnvelope` Ed25519 salvo em cache criptografado,
|
|
83
|
+
* garantindo integridade do payload, status ativo (não REVOKED/SUSPENDED),
|
|
84
|
+
* correspondência de instalação/domínio e janela de carência `graceUntil`.
|
|
85
|
+
*/
|
|
86
|
+
static verifyCachedOfflineEntitlement(params: {
|
|
87
|
+
cachedResponse: Record<string, any> | null | undefined;
|
|
88
|
+
trustedKeys: Record<string, string>;
|
|
89
|
+
expectedInstallationId: string;
|
|
90
|
+
expectedDomain: string;
|
|
91
|
+
nowMs?: number;
|
|
92
|
+
}): {
|
|
93
|
+
keyId: string;
|
|
94
|
+
graceUntil: string;
|
|
95
|
+
entitlement: Record<string, any>;
|
|
96
|
+
issuedAt: string;
|
|
97
|
+
};
|
|
98
|
+
}
|