licenx 0.0.0-stage → 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/CHANGELOG.md +4 -0
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/THIRD_PARTY_NOTICES.txt +15918 -0
- package/dist/background.d.ts +2 -0
- package/dist/background.js +67 -0
- package/dist/build-info.json +8 -0
- package/dist/client.d.ts +37 -0
- package/dist/client.js +322 -0
- package/dist/identity.d.ts +4 -0
- package/dist/identity.js +35 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/messaging.d.ts +11 -0
- package/dist/messaging.js +17 -0
- package/dist/session.d.ts +6 -0
- package/dist/session.js +10 -0
- package/dist/storage.d.ts +14 -0
- package/dist/storage.js +58 -0
- package/dist/transport.d.ts +23 -0
- package/dist/transport.js +98 -0
- package/dist/types.d.ts +83 -0
- package/dist/types.js +26 -0
- package/dist/wasm/licenx_wasm.d.ts +77 -0
- package/dist/wasm/licenx_wasm.js +672 -0
- package/dist/wasm/licenx_wasm_bg.wasm +0 -0
- package/dist/wasm/licenx_wasm_bg.wasm.d.ts +28 -0
- package/dist/wasm-loader.d.ts +3 -0
- package/dist/wasm-loader.js +14 -0
- package/docs/API_REFERENCE.md +21 -0
- package/docs/BUILD_AND_RELEASE.md +9 -0
- package/docs/COMPATIBILITY.md +7 -0
- package/docs/CONFIGURATION.md +17 -0
- package/docs/GUIDE_BN.md +19 -0
- package/docs/GUIDE_EN.md +19 -0
- package/docs/INTEGRATION_EXAMPLES.md +19 -0
- package/docs/LIFECYCLE.md +7 -0
- package/docs/QUICK_START.md +14 -0
- package/docs/README.md +5 -0
- package/docs/SECURITY.md +7 -0
- package/docs/TROUBLESHOOTING.md +19 -0
- package/examples/chrome/background.js +4 -0
- package/examples/chrome/manifest.json +23 -0
- package/examples/chrome/popup.html +4 -0
- package/examples/chrome/popup.js +12 -0
- package/examples/firefox/background.js +4 -0
- package/examples/firefox/manifest.json +36 -0
- package/examples/firefox/popup.html +4 -0
- package/examples/firefox/popup.js +12 -0
- package/licenx.config.example.json +10 -0
- package/package.json +41 -4
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Client } from "./client.js";
|
|
2
|
+
import { parseMessage } from "./messaging.js";
|
|
3
|
+
import { extensionApi, code } from "./types.js";
|
|
4
|
+
export function registerBackground(config, options = {}, popupPath = "popup.html", api = extensionApi()) {
|
|
5
|
+
let client;
|
|
6
|
+
let loading;
|
|
7
|
+
const alarm = "licenx.signed-heartbeat";
|
|
8
|
+
const boot = () => loading ??= (async () => {
|
|
9
|
+
const c = await Client.create(config, options, api);
|
|
10
|
+
client = c;
|
|
11
|
+
try {
|
|
12
|
+
await c.recover();
|
|
13
|
+
}
|
|
14
|
+
catch { } // status stays fail-closed until fresh proof
|
|
15
|
+
return c;
|
|
16
|
+
})().catch(e => { loading = undefined; throw e; });
|
|
17
|
+
const schedule = async () => { try {
|
|
18
|
+
if (!api.alarms.get || !await api.alarms.get(alarm))
|
|
19
|
+
await api.alarms.create(alarm, { periodInMinutes: Math.max(1, (config.ping_interval_seconds ?? 1200) / 60) });
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
client?.invalidatePermission();
|
|
23
|
+
} };
|
|
24
|
+
const wake = () => { void schedule(); void boot().catch(() => { }); };
|
|
25
|
+
// Register listeners synchronously; MV3 event delivery must not wait for WASM.
|
|
26
|
+
api.runtime.onStartup.addListener(wake);
|
|
27
|
+
api.runtime.onInstalled.addListener(wake);
|
|
28
|
+
api.alarms.onAlarm.addListener(a => { if (a.name === alarm)
|
|
29
|
+
void boot().then(c => c.tick()).catch(() => { }).finally(() => { void schedule(); }); });
|
|
30
|
+
api.permissions.onRemoved.addListener(() => { client?.invalidatePermission(); });
|
|
31
|
+
api.runtime.onMessage.addListener((raw, sender, respond) => {
|
|
32
|
+
let message;
|
|
33
|
+
try {
|
|
34
|
+
message = parseMessage(raw, sender, api.runtime.id, api.runtime.getURL(popupPath));
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
respond({ ok: false, code: code(e, "MESSAGE_DENIED") });
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
void (async () => {
|
|
41
|
+
try {
|
|
42
|
+
const c = await boot();
|
|
43
|
+
switch (message.action) {
|
|
44
|
+
case "login":
|
|
45
|
+
await c.login(message.key);
|
|
46
|
+
break;
|
|
47
|
+
case "ping":
|
|
48
|
+
await c.ping();
|
|
49
|
+
break;
|
|
50
|
+
case "logout":
|
|
51
|
+
respond({ ok: true, outcome: await c.logout(), status: await c.status() });
|
|
52
|
+
return;
|
|
53
|
+
case "reportStats":
|
|
54
|
+
await c.reportStats(message.stats);
|
|
55
|
+
break;
|
|
56
|
+
case "status": break;
|
|
57
|
+
}
|
|
58
|
+
respond({ ok: true, status: await c.status() });
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
respond({ ok: false, code: code(e, "INITIALIZATION_FAILED") });
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
return true;
|
|
65
|
+
});
|
|
66
|
+
wake();
|
|
67
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type Config, type Options, type ExtensionApi, type Status, type Stats, type LogoutOutcome } from "./types.js";
|
|
2
|
+
export declare class Client {
|
|
3
|
+
private bridge;
|
|
4
|
+
private transport;
|
|
5
|
+
private storage;
|
|
6
|
+
readonly deviceIdentity: string;
|
|
7
|
+
private queue;
|
|
8
|
+
private active;
|
|
9
|
+
private pending;
|
|
10
|
+
private permissionEpoch;
|
|
11
|
+
private storageFailed;
|
|
12
|
+
private constructor();
|
|
13
|
+
static create(input: Config, options?: Options, api?: ExtensionApi): Promise<Client>;
|
|
14
|
+
private run;
|
|
15
|
+
private view;
|
|
16
|
+
private persist;
|
|
17
|
+
invalidatePermission(): void;
|
|
18
|
+
private check;
|
|
19
|
+
private normalizedKey;
|
|
20
|
+
private stage;
|
|
21
|
+
private refresh;
|
|
22
|
+
private release;
|
|
23
|
+
private reconcile;
|
|
24
|
+
login(key: string): Promise<void>;
|
|
25
|
+
autoLogin(key: string): Promise<void>;
|
|
26
|
+
private recoverInner;
|
|
27
|
+
recover(): Promise<void>;
|
|
28
|
+
private pingInner;
|
|
29
|
+
ping(): Promise<void>;
|
|
30
|
+
tick(): Promise<void>;
|
|
31
|
+
status(): Promise<Status>;
|
|
32
|
+
isAuthenticated(): Promise<boolean>;
|
|
33
|
+
isValid(): Promise<boolean>;
|
|
34
|
+
hasLoginSession(): Promise<boolean>;
|
|
35
|
+
logout(): Promise<LogoutOutcome>;
|
|
36
|
+
reportStats(stats: Stats): Promise<void>;
|
|
37
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { loadWasm } from "./wasm-loader.js";
|
|
2
|
+
import { Transport, PATHS, normalizedConfig } from "./transport.js";
|
|
3
|
+
import { installationId, scopeId, nonce } from "./identity.js";
|
|
4
|
+
import { RecoveryStorage } from "./storage.js";
|
|
5
|
+
import { Serial, monotonic } from "./session.js";
|
|
6
|
+
import { extensionApi, LicenXError, code } from "./types.js";
|
|
7
|
+
export class Client {
|
|
8
|
+
bridge;
|
|
9
|
+
transport;
|
|
10
|
+
storage;
|
|
11
|
+
deviceIdentity;
|
|
12
|
+
queue = new Serial();
|
|
13
|
+
active = null;
|
|
14
|
+
pending = new Map();
|
|
15
|
+
permissionEpoch = 0;
|
|
16
|
+
storageFailed = false;
|
|
17
|
+
constructor(bridge, transport, storage, deviceIdentity) {
|
|
18
|
+
this.bridge = bridge;
|
|
19
|
+
this.transport = transport;
|
|
20
|
+
this.storage = storage;
|
|
21
|
+
this.deviceIdentity = deviceIdentity;
|
|
22
|
+
}
|
|
23
|
+
static async create(input, options = {}, api = extensionApi()) {
|
|
24
|
+
const config = normalizedConfig(input);
|
|
25
|
+
const transport = new Transport(config, api);
|
|
26
|
+
await transport.requirePermission();
|
|
27
|
+
const scope = await scopeId(transport.origin, config.app_name);
|
|
28
|
+
const id = await installationId(api.storage.local, scope);
|
|
29
|
+
const Bridge = await loadWasm();
|
|
30
|
+
let b;
|
|
31
|
+
try {
|
|
32
|
+
b = new Bridge(JSON.stringify(config), id);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
throw new LicenXError(code(e, "CONFIG_INVALID"));
|
|
36
|
+
}
|
|
37
|
+
const store = await RecoveryStorage.open(api, scope, options.retainSession === true);
|
|
38
|
+
const c = new Client(b, transport, store, id);
|
|
39
|
+
const record = await store.read();
|
|
40
|
+
c.active = record.active;
|
|
41
|
+
for (const k of record.pending)
|
|
42
|
+
c.pending.set(k, undefined);
|
|
43
|
+
return c;
|
|
44
|
+
}
|
|
45
|
+
run(f) { return this.queue.run(async () => { try {
|
|
46
|
+
return await f();
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
throw new LicenXError(code(e));
|
|
50
|
+
} }); }
|
|
51
|
+
view() { return { ...JSON.parse(this.bridge.status(monotonic())), pendingRelease: this.pending.size > 0 }; }
|
|
52
|
+
async persist() {
|
|
53
|
+
const record = { active: this.active, pending: [...this.pending.keys()] };
|
|
54
|
+
try {
|
|
55
|
+
await this.storage.write(record);
|
|
56
|
+
this.storageFailed = false;
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
this.storageFailed = true;
|
|
60
|
+
this.bridge.pause();
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
invalidatePermission() { this.permissionEpoch++; this.bridge.pause(); }
|
|
65
|
+
async check(epoch) {
|
|
66
|
+
if (epoch !== this.permissionEpoch)
|
|
67
|
+
throw new LicenXError("PERMISSION_DENIED");
|
|
68
|
+
try {
|
|
69
|
+
await this.transport.requirePermission();
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
this.bridge.pause();
|
|
73
|
+
throw e;
|
|
74
|
+
}
|
|
75
|
+
if (epoch !== this.permissionEpoch)
|
|
76
|
+
throw new LicenXError("PERMISSION_DENIED");
|
|
77
|
+
}
|
|
78
|
+
normalizedKey(key) {
|
|
79
|
+
if (typeof key !== "string")
|
|
80
|
+
throw new LicenXError("CONFIG_INVALID");
|
|
81
|
+
const k = key.trim().toUpperCase();
|
|
82
|
+
if (!k || k.length > 160)
|
|
83
|
+
throw new LicenXError("CONFIG_INVALID");
|
|
84
|
+
return k;
|
|
85
|
+
}
|
|
86
|
+
async stage(key, epoch) {
|
|
87
|
+
const n = nonce();
|
|
88
|
+
const reply = await this.transport.post(PATHS.validate, this.bridge.validation_body(key, n, false));
|
|
89
|
+
if (reply.status !== 200)
|
|
90
|
+
throw new LicenXError(reply.status === 408 ? "TIMEOUT" : reply.status === 429 ? "RATE_LIMITED" : reply.status >= 500 ? "SERVICE_UNAVAILABLE" : "AUTH_REJECTED");
|
|
91
|
+
this.bridge.stage_login(key, n, reply.body);
|
|
92
|
+
await this.check(epoch);
|
|
93
|
+
}
|
|
94
|
+
async refresh(key) {
|
|
95
|
+
const n = nonce();
|
|
96
|
+
const reply = await this.transport.post(PATHS.ping, this.bridge.ping_body(key, n));
|
|
97
|
+
return this.bridge.refresh_proof(key, n, reply.status, reply.body) ?? undefined;
|
|
98
|
+
}
|
|
99
|
+
async release(key, proof) {
|
|
100
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
101
|
+
const token = proof ?? await this.refresh(key);
|
|
102
|
+
if (!token)
|
|
103
|
+
return false; // independently signed DEVICE_UNBOUND
|
|
104
|
+
const reply = await this.transport.post(PATHS.logout, "{}", token);
|
|
105
|
+
if (this.bridge.release_ok(reply.status, reply.body))
|
|
106
|
+
return true;
|
|
107
|
+
if (reply.status === 403 && attempt === 0) {
|
|
108
|
+
proof = undefined;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
throw new LicenXError("PREVIOUS_SESSION_LOGOUT_FAILED");
|
|
112
|
+
}
|
|
113
|
+
throw new LicenXError("PREVIOUS_SESSION_LOGOUT_FAILED");
|
|
114
|
+
}
|
|
115
|
+
async reconcile() {
|
|
116
|
+
let released = false;
|
|
117
|
+
for (const [key, proof] of this.pending) {
|
|
118
|
+
try {
|
|
119
|
+
released = (await this.release(key, proof)) || released;
|
|
120
|
+
this.pending.delete(key);
|
|
121
|
+
await this.persist();
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
throw new LicenXError("PREVIOUS_SESSION_LOGOUT_FAILED");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return released;
|
|
128
|
+
}
|
|
129
|
+
login(key) {
|
|
130
|
+
return this.run(async () => {
|
|
131
|
+
const k = this.normalizedKey(key);
|
|
132
|
+
const epoch = this.permissionEpoch;
|
|
133
|
+
await this.check(epoch);
|
|
134
|
+
await this.reconcile();
|
|
135
|
+
const previous = this.active;
|
|
136
|
+
// Record uncertain candidate binding before mutation when recovery is opted in.
|
|
137
|
+
if (k !== previous) {
|
|
138
|
+
this.pending.set(k, undefined);
|
|
139
|
+
await this.persist();
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
await this.stage(k, epoch);
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
this.bridge.discard_candidate();
|
|
146
|
+
if (k === previous && code(e) !== "AUTH_REJECTED")
|
|
147
|
+
this.bridge.pause();
|
|
148
|
+
// A normal authenticated HTTP rejection did not create a binding. Ambiguous
|
|
149
|
+
// failures retain pending cleanup and never activate the candidate.
|
|
150
|
+
if (code(e) === "AUTH_REJECTED" && k !== previous) {
|
|
151
|
+
this.pending.delete(k);
|
|
152
|
+
await this.persist();
|
|
153
|
+
}
|
|
154
|
+
throw e;
|
|
155
|
+
}
|
|
156
|
+
if (previous && previous !== k) {
|
|
157
|
+
try {
|
|
158
|
+
await this.release(previous, this.bridge.active_proof());
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
this.bridge.pause();
|
|
162
|
+
this.pending.set(k, this.bridge.candidate_proof());
|
|
163
|
+
this.bridge.discard_candidate();
|
|
164
|
+
try {
|
|
165
|
+
await this.reconcile();
|
|
166
|
+
}
|
|
167
|
+
catch { }
|
|
168
|
+
await this.persist();
|
|
169
|
+
throw new LicenXError("PREVIOUS_SESSION_LOGOUT_FAILED");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Persist desired state before granting local authorization. If storage
|
|
173
|
+
// fails, retain UI/cleanup information in RAM and do not grant.
|
|
174
|
+
this.active = k;
|
|
175
|
+
this.pending.delete(k);
|
|
176
|
+
try {
|
|
177
|
+
await this.persist();
|
|
178
|
+
await this.check(epoch);
|
|
179
|
+
this.bridge.commit_login(monotonic());
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
this.bridge.discard_candidate();
|
|
183
|
+
this.bridge.pause();
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
autoLogin(key) { return this.login(key); }
|
|
189
|
+
async recoverInner() {
|
|
190
|
+
const epoch = this.permissionEpoch;
|
|
191
|
+
await this.check(epoch);
|
|
192
|
+
await this.reconcile();
|
|
193
|
+
if (!this.active)
|
|
194
|
+
return;
|
|
195
|
+
// Never rebind a previously unlinked device just from a saved UI hint.
|
|
196
|
+
// Require signed active heartbeat before obtaining the full login payload.
|
|
197
|
+
try {
|
|
198
|
+
const proof = await this.refresh(this.active);
|
|
199
|
+
if (!proof) {
|
|
200
|
+
this.active = null;
|
|
201
|
+
this.bridge.clear();
|
|
202
|
+
await this.persist();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
await this.stage(this.active, epoch);
|
|
206
|
+
await this.persist();
|
|
207
|
+
await this.check(epoch);
|
|
208
|
+
this.bridge.commit_login(monotonic());
|
|
209
|
+
}
|
|
210
|
+
catch (e) {
|
|
211
|
+
this.bridge.discard_candidate();
|
|
212
|
+
this.bridge.pause();
|
|
213
|
+
if (code(e) === "LICENSE_TERMINATED") {
|
|
214
|
+
this.active = null;
|
|
215
|
+
this.bridge.clear();
|
|
216
|
+
await this.persist();
|
|
217
|
+
}
|
|
218
|
+
throw e;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
recover() { return this.run(() => this.recoverInner()); }
|
|
222
|
+
async pingInner() {
|
|
223
|
+
if (!this.active)
|
|
224
|
+
throw new LicenXError("NOT_AUTHORIZED");
|
|
225
|
+
if (!this.view().hasLoginSession) {
|
|
226
|
+
await this.recoverInner();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const epoch = this.permissionEpoch;
|
|
230
|
+
try {
|
|
231
|
+
await this.check(epoch);
|
|
232
|
+
const n = nonce();
|
|
233
|
+
const r = await this.transport.post(PATHS.ping, this.bridge.ping_body(this.active, n));
|
|
234
|
+
await this.check(epoch);
|
|
235
|
+
this.bridge.heartbeat(this.active, n, r.status, r.body, monotonic());
|
|
236
|
+
}
|
|
237
|
+
catch (e) {
|
|
238
|
+
const c = code(e);
|
|
239
|
+
if (c === "LICENSE_TERMINATED") {
|
|
240
|
+
this.active = null;
|
|
241
|
+
this.bridge.clear();
|
|
242
|
+
await this.persist();
|
|
243
|
+
}
|
|
244
|
+
else if (["TIMEOUT", "RATE_LIMITED", "SERVICE_UNAVAILABLE"].includes(c)) {
|
|
245
|
+
try {
|
|
246
|
+
this.bridge.failed(c, monotonic());
|
|
247
|
+
}
|
|
248
|
+
catch { }
|
|
249
|
+
}
|
|
250
|
+
else
|
|
251
|
+
this.bridge.pause();
|
|
252
|
+
throw e;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
ping() { return this.run(() => this.pingInner()); }
|
|
256
|
+
tick() { return this.run(async () => { if (this.active) {
|
|
257
|
+
await this.pingInner();
|
|
258
|
+
}
|
|
259
|
+
else if (this.pending.size) {
|
|
260
|
+
await this.reconcile();
|
|
261
|
+
} }); }
|
|
262
|
+
status() {
|
|
263
|
+
return this.run(async () => {
|
|
264
|
+
try {
|
|
265
|
+
await this.check(this.permissionEpoch);
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return { ...this.view(), authorized: false, tier: null };
|
|
269
|
+
}
|
|
270
|
+
if (this.active && !this.storageFailed && !this.view().authorized)
|
|
271
|
+
try {
|
|
272
|
+
await this.pingInner();
|
|
273
|
+
}
|
|
274
|
+
catch { }
|
|
275
|
+
const v = this.view();
|
|
276
|
+
return this.storageFailed ? { ...v, authorized: false, tier: null, state: "PausedUnverified" } : v;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
async isAuthenticated() { return (await this.status()).authorized; }
|
|
280
|
+
async isValid() { return this.isAuthenticated(); }
|
|
281
|
+
async hasLoginSession() { return (await this.status()).hasLoginSession; }
|
|
282
|
+
logout() {
|
|
283
|
+
return this.run(async () => {
|
|
284
|
+
if (this.active)
|
|
285
|
+
this.pending.set(this.active, this.bridge.active_proof());
|
|
286
|
+
this.active = null;
|
|
287
|
+
this.bridge.clear();
|
|
288
|
+
try {
|
|
289
|
+
await this.persist();
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
return "RemoteUnconfirmed";
|
|
293
|
+
}
|
|
294
|
+
if (!this.pending.size)
|
|
295
|
+
return "AlreadyLoggedOut";
|
|
296
|
+
try {
|
|
297
|
+
return await this.reconcile() ? "Released" : "AlreadyLoggedOut";
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
return "RemoteUnconfirmed";
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
reportStats(stats) {
|
|
305
|
+
return this.run(async () => {
|
|
306
|
+
const epoch = this.permissionEpoch;
|
|
307
|
+
await this.check(epoch);
|
|
308
|
+
if (!this.active || !this.view().authorized || this.storageFailed)
|
|
309
|
+
throw new LicenXError("NOT_AUTHORIZED");
|
|
310
|
+
const body = this.bridge.stats_body(JSON.stringify(stats));
|
|
311
|
+
const n = nonce();
|
|
312
|
+
const r = await this.transport.post(PATHS.validate, this.bridge.validation_body(this.active, n, true));
|
|
313
|
+
if (r.status !== 200)
|
|
314
|
+
throw new LicenXError("STATS_UNAVAILABLE");
|
|
315
|
+
const cap = this.bridge.stats_capability(this.active, n, r.body);
|
|
316
|
+
await this.check(epoch);
|
|
317
|
+
const reply = await this.transport.post(PATHS.stats, body, cap);
|
|
318
|
+
if (reply.status !== 202)
|
|
319
|
+
throw new LicenXError("STATS_UNAVAILABLE");
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { LicenXError } from "./types.js";
|
|
2
|
+
export async function scopeId(origin, app) {
|
|
3
|
+
const b = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify([origin, app])));
|
|
4
|
+
return Array.from(new Uint8Array(b), x => x.toString(16).padStart(2, "0")).join("");
|
|
5
|
+
}
|
|
6
|
+
export async function installationId(local, scope) {
|
|
7
|
+
const key = "licenx.identity." + scope;
|
|
8
|
+
try {
|
|
9
|
+
if (local.setAccessLevel)
|
|
10
|
+
await local.setAccessLevel({ accessLevel: "TRUSTED_CONTEXTS" });
|
|
11
|
+
const record = await local.get(key);
|
|
12
|
+
const old = record[key];
|
|
13
|
+
if (typeof old === "string" && /^HWID-WB-[0-9a-f]{32}$/.test(old))
|
|
14
|
+
return old;
|
|
15
|
+
// Corrupt identity is not silently replaced (could consume a second slot).
|
|
16
|
+
if (old !== undefined)
|
|
17
|
+
throw new Error("corrupt identity");
|
|
18
|
+
const id = "HWID-WB-" + Array.from(crypto.getRandomValues(new Uint8Array(16)), x => x.toString(16).padStart(2, "0")).join("");
|
|
19
|
+
await local.set({ [key]: id });
|
|
20
|
+
if ((await local.get(key))[key] !== id)
|
|
21
|
+
throw new Error("storage verification");
|
|
22
|
+
return id;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new LicenXError("STORAGE_UNAVAILABLE");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function nonce() {
|
|
29
|
+
try {
|
|
30
|
+
return Array.from(crypto.getRandomValues(new Uint8Array(32)), x => x.toString(16).padStart(2, "0")).join("");
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
throw new LicenXError("RANDOM_UNAVAILABLE");
|
|
34
|
+
}
|
|
35
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Sender, type Stats } from "./types.js";
|
|
2
|
+
export type Message = {
|
|
3
|
+
action: "status" | "ping" | "logout";
|
|
4
|
+
} | {
|
|
5
|
+
action: "login";
|
|
6
|
+
key: string;
|
|
7
|
+
} | {
|
|
8
|
+
action: "reportStats";
|
|
9
|
+
stats: Stats;
|
|
10
|
+
};
|
|
11
|
+
export declare function parseMessage(raw: unknown, sender: Sender, id: string, popup: string): Message;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { LicenXError } from "./types.js";
|
|
2
|
+
export function parseMessage(raw, sender, id, popup) {
|
|
3
|
+
if (sender.id !== id || sender.url !== popup || !raw || typeof raw !== "object" || Array.isArray(raw))
|
|
4
|
+
throw new LicenXError("MESSAGE_DENIED");
|
|
5
|
+
const o = raw;
|
|
6
|
+
const keys = Object.keys(o).sort().join(",");
|
|
7
|
+
if (keys === "action" && (o.action === "status" || o.action === "ping" || o.action === "logout"))
|
|
8
|
+
return { action: o.action };
|
|
9
|
+
if (keys === "action,key" && o.action === "login" && typeof o.key === "string" && o.key.length <= 160 && o.key.trim())
|
|
10
|
+
return { action: "login", key: o.key };
|
|
11
|
+
if (keys === "action,stats" && o.action === "reportStats" && o.stats && typeof o.stats === "object" && !Array.isArray(o.stats)) {
|
|
12
|
+
const st = o.stats;
|
|
13
|
+
if (Object.keys(st).length <= 40 && Object.values(st).every(v => v === null || ["string", "boolean", "number"].includes(typeof v)))
|
|
14
|
+
return { action: "reportStats", stats: st };
|
|
15
|
+
}
|
|
16
|
+
throw new LicenXError("MESSAGE_DENIED");
|
|
17
|
+
}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Single privileged background owner; all lifecycle mutations serialized. */
|
|
2
|
+
export class Serial {
|
|
3
|
+
tail = Promise.resolve();
|
|
4
|
+
run(task) {
|
|
5
|
+
const result = this.tail.then(task, task);
|
|
6
|
+
this.tail = result.then(() => undefined, () => undefined);
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function monotonic() { return performance.now(); }
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ExtensionApi } from "./types.js";
|
|
2
|
+
export interface RecoveryRecord {
|
|
3
|
+
active: string | null;
|
|
4
|
+
pending: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare const emptyRecord: () => RecoveryRecord;
|
|
7
|
+
export declare class RecoveryStorage {
|
|
8
|
+
private area;
|
|
9
|
+
private key;
|
|
10
|
+
private constructor();
|
|
11
|
+
static open(api: ExtensionApi, scope: string, retain: boolean): Promise<RecoveryStorage>;
|
|
12
|
+
read(): Promise<RecoveryRecord>;
|
|
13
|
+
write(v: RecoveryRecord): Promise<void>;
|
|
14
|
+
}
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { LicenXError } from "./types.js";
|
|
2
|
+
export const emptyRecord = () => ({ active: null, pending: [] });
|
|
3
|
+
function validKey(k) { return typeof k === "string" && k.length > 0 && k.length <= 160 && k === k.trim(); }
|
|
4
|
+
export class RecoveryStorage {
|
|
5
|
+
area;
|
|
6
|
+
key;
|
|
7
|
+
constructor(area, key) {
|
|
8
|
+
this.area = area;
|
|
9
|
+
this.key = key;
|
|
10
|
+
}
|
|
11
|
+
static async open(api, scope, retain) {
|
|
12
|
+
const key = "licenx.recovery." + scope;
|
|
13
|
+
const area = api.storage.session;
|
|
14
|
+
// Firefox storage.session is restricted to trusted extension contexts by default.
|
|
15
|
+
// Chrome exposes setAccessLevel; explicitly enforce the restricted level.
|
|
16
|
+
try {
|
|
17
|
+
if (retain && !area)
|
|
18
|
+
throw new Error("storage.session missing");
|
|
19
|
+
if (area?.setAccessLevel)
|
|
20
|
+
await area.setAccessLevel({ accessLevel: "TRUSTED_CONTEXTS" });
|
|
21
|
+
if (!retain && area)
|
|
22
|
+
await area.remove(key);
|
|
23
|
+
return new RecoveryStorage(retain ? area : undefined, key);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new LicenXError("STORAGE_UNAVAILABLE");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async read() {
|
|
30
|
+
if (!this.area)
|
|
31
|
+
return emptyRecord();
|
|
32
|
+
try {
|
|
33
|
+
const v = (await this.area.get(this.key))[this.key];
|
|
34
|
+
if (v === undefined)
|
|
35
|
+
return emptyRecord();
|
|
36
|
+
if (!v || Object.keys(v).sort().join(",") !== "active,pending" || !(v.active === null || validKey(v.active)) ||
|
|
37
|
+
!Array.isArray(v.pending) || v.pending.length > 4 || !v.pending.every(validKey))
|
|
38
|
+
throw new Error("record invalid");
|
|
39
|
+
return { active: v.active, pending: [...new Set(v.pending)] };
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new LicenXError("STORAGE_UNAVAILABLE");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async write(v) {
|
|
46
|
+
if (!this.area)
|
|
47
|
+
return;
|
|
48
|
+
try {
|
|
49
|
+
if (v.active === null && v.pending.length === 0)
|
|
50
|
+
await this.area.remove(this.key);
|
|
51
|
+
else
|
|
52
|
+
await this.area.set({ [this.key]: { active: v.active, pending: [...v.pending] } });
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
throw new LicenXError("STORAGE_UNAVAILABLE");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Config, type ExtensionApi } from "./types.js";
|
|
2
|
+
export declare const PATHS: {
|
|
3
|
+
readonly validate: "/api/v1/license/validate";
|
|
4
|
+
readonly ping: "/api/v1/license/ping";
|
|
5
|
+
readonly logout: "/api/v2/license/logout";
|
|
6
|
+
readonly stats: "/api/v1/device/stats";
|
|
7
|
+
};
|
|
8
|
+
type Path = typeof PATHS[keyof typeof PATHS];
|
|
9
|
+
export interface Reply {
|
|
10
|
+
status: number;
|
|
11
|
+
body: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
export declare function normalizedConfig(input: Config): Config;
|
|
14
|
+
export declare class Transport {
|
|
15
|
+
private config;
|
|
16
|
+
private api;
|
|
17
|
+
readonly origin: string;
|
|
18
|
+
readonly permission: string;
|
|
19
|
+
constructor(config: Config, api: ExtensionApi);
|
|
20
|
+
requirePermission(): Promise<void>;
|
|
21
|
+
post(path: Path, body: string, bearer?: string): Promise<Reply>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|