omnikey-cli 1.5.2 → 1.5.4

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.
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchJwtToken = fetchJwtToken;
7
+ exports.clearCachedToken = clearCachedToken;
8
+ const axios_1 = __importDefault(require("axios"));
9
+ const config_1 = require("./config");
10
+ let cachedToken = null;
11
+ let cachedExpiresAtMs = null;
12
+ // Renew JWTs a few minutes before expiry to avoid mid-WebSocket failures.
13
+ const RENEW_BEFORE_MS = 60 * 1000;
14
+ function decodeJwtExpiry(token) {
15
+ const parts = token.split(".");
16
+ if (parts.length < 2)
17
+ return null;
18
+ try {
19
+ const payload = JSON.parse(Buffer.from(parts[1].replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8"));
20
+ return typeof payload.exp === "number" ? payload.exp * 1000 : null;
21
+ }
22
+ catch {
23
+ return null;
24
+ }
25
+ }
26
+ async function fetchJwtToken(logger, force = false) {
27
+ const now = Date.now();
28
+ if (!force &&
29
+ cachedToken &&
30
+ cachedExpiresAtMs &&
31
+ cachedExpiresAtMs - RENEW_BEFORE_MS > now) {
32
+ return cachedToken;
33
+ }
34
+ const url = `${(0, config_1.omnikeyBaseUrl)()}/api/subscription/activate`;
35
+ logger.info("Requesting JWT from omnikey-ai", { url });
36
+ const resp = await axios_1.default.post(url, {}, { timeout: 10000 });
37
+ if (!resp.data?.token) {
38
+ throw new Error("activate endpoint returned no token");
39
+ }
40
+ cachedToken = resp.data.token;
41
+ cachedExpiresAtMs = decodeJwtExpiry(cachedToken);
42
+ logger.info("Received JWT from omnikey-ai", {
43
+ subscriptionStatus: resp.data.subscriptionStatus,
44
+ expiresAt: cachedExpiresAtMs
45
+ ? new Date(cachedExpiresAtMs).toISOString()
46
+ : null,
47
+ });
48
+ return cachedToken;
49
+ }
50
+ function clearCachedToken() {
51
+ cachedToken = null;
52
+ cachedExpiresAtMs = null;
53
+ }