cliodot 1.2.2 → 1.2.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.
- package/LICENSE +201 -0
- package/README.md +81 -0
- package/dist/AuthAppClient.d.ts +1 -0
- package/dist/AuthAppClient.d.ts.map +1 -1
- package/dist/AuthAppClient.js +199 -1
- package/dist/CommercialAppClient.d.ts +178 -0
- package/dist/CommercialAppClient.d.ts.map +1 -0
- package/dist/CommercialAppClient.js +576 -0
- package/dist/ConnectorBuilder.js +104 -1
- package/dist/Events.d.ts +2 -0
- package/dist/Events.d.ts.map +1 -1
- package/dist/Events.js +325 -1
- package/dist/Flosync.js +296 -1
- package/dist/FlosyncClient.js +429 -1
- package/dist/FunctionBuilder.js +65 -1
- package/dist/IdentityAppClient.d.ts +23 -0
- package/dist/IdentityAppClient.d.ts.map +1 -0
- package/dist/IdentityAppClient.js +197 -0
- package/dist/MemoryAppClient.d.ts +28 -0
- package/dist/MemoryAppClient.d.ts.map +1 -0
- package/dist/MemoryAppClient.js +149 -0
- package/dist/MemoryClient.d.ts +8 -0
- package/dist/MemoryClient.d.ts.map +1 -0
- package/dist/MemoryClient.js +20 -0
- package/dist/OAuthAppClient.d.ts +1 -0
- package/dist/OAuthAppClient.d.ts.map +1 -1
- package/dist/OAuthAppClient.js +269 -1
- package/dist/ProviderServicesClient.d.ts +24 -0
- package/dist/ProviderServicesClient.d.ts.map +1 -0
- package/dist/ProviderServicesClient.js +158 -0
- package/dist/Surface.d.ts +2 -0
- package/dist/Surface.d.ts.map +1 -1
- package/dist/Surface.js +255 -1
- package/dist/ValidatorBuilder.js +24 -1
- package/dist/Webhooks.d.ts +20 -0
- package/dist/Webhooks.d.ts.map +1 -0
- package/dist/Webhooks.js +61 -0
- package/dist/WorkflowBuilder.js +279 -1
- package/dist/__tests__/CommercialAppClient.test.d.ts +2 -0
- package/dist/__tests__/CommercialAppClient.test.d.ts.map +1 -0
- package/dist/__tests__/CommercialAppClient.test.js +217 -0
- package/dist/__tests__/Events.test.js +191 -1
- package/dist/__tests__/IdentityAppClient.test.d.ts +2 -0
- package/dist/__tests__/IdentityAppClient.test.d.ts.map +1 -0
- package/dist/__tests__/IdentityAppClient.test.js +232 -0
- package/dist/__tests__/OAuthAppClient.test.js +300 -1
- package/dist/__tests__/ProviderServicesClient.test.d.ts +2 -0
- package/dist/__tests__/ProviderServicesClient.test.d.ts.map +1 -0
- package/dist/__tests__/ProviderServicesClient.test.js +131 -0
- package/dist/__tests__/Surface.test.js +150 -1
- package/dist/__tests__/errors.test.js +54 -1
- package/dist/__tests__/function.test.js +58 -1
- package/dist/__tests__/helpers.d.ts +5 -0
- package/dist/__tests__/helpers.d.ts.map +1 -0
- package/dist/__tests__/helpers.js +10 -0
- package/dist/__tests__/localhost-lookup.test.d.ts +2 -0
- package/dist/__tests__/localhost-lookup.test.d.ts.map +1 -0
- package/dist/__tests__/localhost-lookup.test.js +26 -0
- package/dist/__tests__/parse-api-error.test.js +26 -1
- package/dist/__tests__/runner.test.js +156 -1
- package/dist/connectors/builtin.js +354 -1
- package/dist/connectors/registry.js +300 -1
- package/dist/decorators/build.js +105 -1
- package/dist/decorators/connector.js +56 -1
- package/dist/decorators/index.js +34 -1
- package/dist/decorators/metadata.js +338 -1
- package/dist/decorators/steps.js +220 -1
- package/dist/decorators/workflow.js +52 -1
- package/dist/errors.js +76 -1
- package/dist/http/__tests__/cliodot-request.environment.test.d.ts +2 -0
- package/dist/http/__tests__/cliodot-request.environment.test.d.ts.map +1 -0
- package/dist/http/__tests__/cliodot-request.environment.test.js +37 -0
- package/dist/http/__tests__/cliodot-result.test.d.ts +2 -0
- package/dist/http/__tests__/cliodot-result.test.d.ts.map +1 -0
- package/dist/http/__tests__/cliodot-result.test.js +57 -0
- package/dist/http/__tests__/events-instance-id.test.d.ts +2 -0
- package/dist/http/__tests__/events-instance-id.test.d.ts.map +1 -0
- package/dist/http/__tests__/events-instance-id.test.js +45 -0
- package/dist/http/cliodot-request.d.ts +35 -0
- package/dist/http/cliodot-request.d.ts.map +1 -0
- package/dist/http/cliodot-request.js +105 -0
- package/dist/http/cliodot-result.d.ts +40 -0
- package/dist/http/cliodot-result.d.ts.map +1 -0
- package/dist/http/cliodot-result.js +139 -0
- package/dist/http/create-client.d.ts +3 -0
- package/dist/http/create-client.d.ts.map +1 -0
- package/dist/http/create-client.js +14 -0
- package/dist/http/events-instance-id.d.ts +11 -0
- package/dist/http/events-instance-id.d.ts.map +1 -0
- package/dist/http/events-instance-id.js +86 -0
- package/dist/http/localhost-lookup.d.ts +12 -0
- package/dist/http/localhost-lookup.d.ts.map +1 -0
- package/dist/http/localhost-lookup.js +67 -0
- package/dist/http/parse-api-error.js +56 -1
- package/dist/http/sanitize-execution-headers.js +79 -1
- package/dist/identity-offline-verify.d.ts +6 -0
- package/dist/identity-offline-verify.d.ts.map +1 -0
- package/dist/identity-offline-verify.js +174 -0
- package/dist/identity-token-cache.d.ts +6 -0
- package/dist/identity-token-cache.d.ts.map +1 -0
- package/dist/identity-token-cache.js +51 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +108 -1
- package/dist/runner/ProcessorEngine.js +1311 -1
- package/dist/runner/connector.executor.js +1786 -1
- package/dist/runner/utilities/math.executor.js +153 -1
- package/dist/runner/utilities/random.executor.js +151 -1
- package/dist/runner/utilities/string.executor.js +825 -1
- package/dist/template.js +127 -1
- package/dist/transformers/StepsToNodesTransformer.js +214 -1
- package/dist/transformers/WorkflowTransformer.js +189 -1
- package/dist/types/auth-app.api.d.ts +2 -0
- package/dist/types/auth-app.api.d.ts.map +1 -1
- package/dist/types/auth-app.api.js +2 -1
- package/dist/types/builtin.js +2 -1
- package/dist/types/client.api.js +2 -1
- package/dist/types/commercial-app.api.d.ts +963 -0
- package/dist/types/commercial-app.api.d.ts.map +1 -0
- package/dist/types/commercial-app.api.js +2 -0
- package/dist/types/connector.js +2 -1
- package/dist/types/context.js +2 -1
- package/dist/types/event-app.api.d.ts +8 -0
- package/dist/types/event-app.api.d.ts.map +1 -1
- package/dist/types/event-app.api.js +2 -1
- package/dist/types/function.js +2 -1
- package/dist/types/identity-app.api.d.ts +99 -0
- package/dist/types/identity-app.api.d.ts.map +1 -0
- package/dist/types/identity-app.api.js +2 -0
- package/dist/types/index.js +23 -1
- package/dist/types/memory-app.api.d.ts +140 -0
- package/dist/types/memory-app.api.d.ts.map +1 -0
- package/dist/types/memory-app.api.js +2 -0
- package/dist/types/oauth-app.api.d.ts +2 -0
- package/dist/types/oauth-app.api.d.ts.map +1 -1
- package/dist/types/oauth-app.api.js +105 -1
- package/dist/types/provider-services.api.d.ts +98 -0
- package/dist/types/provider-services.api.d.ts.map +1 -0
- package/dist/types/provider-services.api.js +2 -0
- package/dist/types/validator.js +2 -1
- package/dist/types/webhook-app.api.d.ts +2 -0
- package/dist/types/webhook-app.api.d.ts.map +1 -0
- package/dist/types/webhook-app.api.js +2 -0
- package/dist/types/workflow.js +33 -1
- package/dist/validators/validators.js +478 -1
- package/dist/variable.js +37 -1
- package/package.json +3 -2
|
@@ -1 +1,191 @@
|
|
|
1
|
-
|
|
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
|
+
const axios_1 = __importDefault(require("axios"));
|
|
7
|
+
const Events_1 = require("../Events");
|
|
8
|
+
const errors_1 = require("../errors");
|
|
9
|
+
jest.mock("axios");
|
|
10
|
+
const mockedAxios = axios_1.default;
|
|
11
|
+
describe("Events", () => {
|
|
12
|
+
const mockAxiosInstance = jest.fn();
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
jest.clearAllMocks();
|
|
15
|
+
mockedAxios.create.mockReturnValue(mockAxiosInstance);
|
|
16
|
+
});
|
|
17
|
+
function createClient(overrides) {
|
|
18
|
+
return new Events_1.Events({
|
|
19
|
+
baseUrl: "https://api.example.com",
|
|
20
|
+
appId: "evt_app_test",
|
|
21
|
+
apiKey: "eak_test_key",
|
|
22
|
+
instanceId: "test-instance",
|
|
23
|
+
...overrides,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
it("requires baseUrl and appId", () => {
|
|
27
|
+
expect(() => new Events_1.Events({ baseUrl: "", appId: "evt_app_test" })).toThrow(errors_1.CliodotApiError);
|
|
28
|
+
expect(() => new Events_1.Events({ baseUrl: "https://api.example.com", appId: "" })).toThrow(errors_1.CliodotApiError);
|
|
29
|
+
});
|
|
30
|
+
it("publishes with bearer api key and app id header", async () => {
|
|
31
|
+
mockAxiosInstance.mockResolvedValue({
|
|
32
|
+
data: { ok: true, event_id: "evt_1", accepted: true },
|
|
33
|
+
});
|
|
34
|
+
const client = createClient();
|
|
35
|
+
const result = await client.publish("order.created", { id: "ord_1" }, {
|
|
36
|
+
ordering_key: "ord_1",
|
|
37
|
+
});
|
|
38
|
+
expect(result.event_id).toBe("evt_1");
|
|
39
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
40
|
+
method: "POST",
|
|
41
|
+
url: "/v1/publish",
|
|
42
|
+
headers: {
|
|
43
|
+
Authorization: "Bearer eak_test_key",
|
|
44
|
+
"x-cliodot-app-id": "evt_app_test",
|
|
45
|
+
"x-cliodot-instance-id": "test-instance",
|
|
46
|
+
},
|
|
47
|
+
data: {
|
|
48
|
+
event: "order.created",
|
|
49
|
+
payload: { id: "ord_1" },
|
|
50
|
+
ordering_key: "ord_1",
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
it("uses app secret headers when api key is absent", async () => {
|
|
55
|
+
mockAxiosInstance.mockResolvedValue({
|
|
56
|
+
data: { ok: true, subscription_id: "evt_sub_1", events: ["order.created"] },
|
|
57
|
+
});
|
|
58
|
+
const client = createClient({
|
|
59
|
+
apiKey: undefined,
|
|
60
|
+
appSecret: "secret_1",
|
|
61
|
+
});
|
|
62
|
+
await client.subscribe({ events: ["order.created"], name: "checkout" });
|
|
63
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
64
|
+
method: "POST",
|
|
65
|
+
url: "/v1/subscribe",
|
|
66
|
+
headers: {
|
|
67
|
+
"x-cliodot-app-id": "evt_app_test",
|
|
68
|
+
"x-cliodot-app-secret": "secret_1",
|
|
69
|
+
"x-cliodot-instance-id": "test-instance",
|
|
70
|
+
},
|
|
71
|
+
data: {
|
|
72
|
+
events: ["order.created"],
|
|
73
|
+
name: "checkout",
|
|
74
|
+
instance_id: "test-instance",
|
|
75
|
+
},
|
|
76
|
+
}));
|
|
77
|
+
});
|
|
78
|
+
it("unsubscribes by id", async () => {
|
|
79
|
+
mockAxiosInstance.mockResolvedValue({
|
|
80
|
+
data: { ok: true, deleted: true },
|
|
81
|
+
});
|
|
82
|
+
const client = createClient();
|
|
83
|
+
const result = await client.unsubscribe("evt_sub_1");
|
|
84
|
+
expect(result.deleted).toBe(true);
|
|
85
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
86
|
+
method: "DELETE",
|
|
87
|
+
url: "/v1/subscribe/evt_sub_1",
|
|
88
|
+
}));
|
|
89
|
+
});
|
|
90
|
+
it("builds browser listenUrl with query auth", () => {
|
|
91
|
+
const client = createClient();
|
|
92
|
+
const url = client.listenUrl({ events: ["order.created", "payment.completed"] });
|
|
93
|
+
expect(url).toBe("https://api.example.com/event/v1/listen?app_id=evt_app_test&access_token=eak_test_key&instance_id=test-instance&events=order.created%2Cpayment.completed");
|
|
94
|
+
});
|
|
95
|
+
it("accepts appApiKey alias", async () => {
|
|
96
|
+
mockAxiosInstance.mockResolvedValue({
|
|
97
|
+
data: { ok: true, event_id: "evt_2", accepted: true },
|
|
98
|
+
});
|
|
99
|
+
const client = createClient({
|
|
100
|
+
apiKey: undefined,
|
|
101
|
+
appApiKey: "eak_alias",
|
|
102
|
+
});
|
|
103
|
+
await client.publish("ping", {});
|
|
104
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
105
|
+
headers: expect.objectContaining({
|
|
106
|
+
Authorization: "Bearer eak_alias",
|
|
107
|
+
}),
|
|
108
|
+
}));
|
|
109
|
+
});
|
|
110
|
+
it("auto-reconnects listen after stream ends until close", async () => {
|
|
111
|
+
jest.useFakeTimers();
|
|
112
|
+
const { EventEmitter } = require("events");
|
|
113
|
+
const stream1 = new EventEmitter();
|
|
114
|
+
const stream2 = new EventEmitter();
|
|
115
|
+
stream1.destroy = jest.fn();
|
|
116
|
+
stream2.destroy = jest.fn();
|
|
117
|
+
mockAxiosInstance
|
|
118
|
+
.mockResolvedValueOnce({ data: stream1 })
|
|
119
|
+
.mockResolvedValueOnce({ data: stream2 });
|
|
120
|
+
const client = createClient({ debug: false });
|
|
121
|
+
const messages = [];
|
|
122
|
+
const handle = client.listen({ events: ["order.created"] }, (m) => {
|
|
123
|
+
messages.push(m);
|
|
124
|
+
});
|
|
125
|
+
await Promise.resolve();
|
|
126
|
+
await Promise.resolve();
|
|
127
|
+
stream1.emit("data", Buffer.from('data: {"type":"connected","listener_id":"sse_1","events":["order.created"]}\n\n'));
|
|
128
|
+
stream1.emit("end");
|
|
129
|
+
await Promise.resolve();
|
|
130
|
+
expect(messages).toHaveLength(1);
|
|
131
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
132
|
+
await Promise.resolve();
|
|
133
|
+
await Promise.resolve();
|
|
134
|
+
stream2.emit("data", Buffer.from('data: {"type":"connected","listener_id":"sse_2","events":["order.created"]}\n\n'));
|
|
135
|
+
expect(messages).toHaveLength(2);
|
|
136
|
+
expect(mockAxiosInstance).toHaveBeenCalledTimes(2);
|
|
137
|
+
handle.close();
|
|
138
|
+
jest.useRealTimers();
|
|
139
|
+
});
|
|
140
|
+
it("does not reconnect when reconnect is false", async () => {
|
|
141
|
+
jest.useFakeTimers();
|
|
142
|
+
const { EventEmitter } = require("events");
|
|
143
|
+
const stream1 = new EventEmitter();
|
|
144
|
+
stream1.destroy = jest.fn();
|
|
145
|
+
mockAxiosInstance.mockResolvedValueOnce({ data: stream1 });
|
|
146
|
+
const client = createClient();
|
|
147
|
+
client.listen({ events: ["order.created"], reconnect: false }, () => { });
|
|
148
|
+
await Promise.resolve();
|
|
149
|
+
await Promise.resolve();
|
|
150
|
+
stream1.emit("end");
|
|
151
|
+
await Promise.resolve();
|
|
152
|
+
await jest.advanceTimersByTimeAsync(5000);
|
|
153
|
+
expect(mockAxiosInstance).toHaveBeenCalledTimes(1);
|
|
154
|
+
jest.useRealTimers();
|
|
155
|
+
});
|
|
156
|
+
it("passes environment on publish", async () => {
|
|
157
|
+
mockAxiosInstance.mockResolvedValue({
|
|
158
|
+
data: { ok: true, event_id: "evt_env", accepted: true },
|
|
159
|
+
});
|
|
160
|
+
const client = createClient();
|
|
161
|
+
await client.publish("order.created", { id: "1" }, { environment: "dev" });
|
|
162
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
163
|
+
data: expect.objectContaining({
|
|
164
|
+
event: "order.created",
|
|
165
|
+
environment: "dev",
|
|
166
|
+
}),
|
|
167
|
+
}));
|
|
168
|
+
});
|
|
169
|
+
it("keeps instance id stable for this client and distinct across computers", async () => {
|
|
170
|
+
mockAxiosInstance.mockResolvedValue({
|
|
171
|
+
data: {
|
|
172
|
+
ok: true,
|
|
173
|
+
subscription_id: "evt_sub_1",
|
|
174
|
+
events: ["order.created"],
|
|
175
|
+
instance_id: "test-instance",
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
const sameBox = createClient();
|
|
179
|
+
await sameBox.subscribe({ events: ["order.created"] });
|
|
180
|
+
await sameBox.subscribe({ events: ["order.created"] });
|
|
181
|
+
const otherBox = createClient({ instanceId: "other-computer" });
|
|
182
|
+
await otherBox.subscribe({ events: ["order.created"] });
|
|
183
|
+
const calls = mockAxiosInstance.mock.calls.filter((c) => c[0]?.url === "/v1/subscribe");
|
|
184
|
+
expect(calls).toHaveLength(3);
|
|
185
|
+
expect(calls[0][0].data.instance_id).toBe("test-instance");
|
|
186
|
+
expect(calls[1][0].data.instance_id).toBe("test-instance");
|
|
187
|
+
expect(calls[2][0].data.instance_id).toBe("other-computer");
|
|
188
|
+
expect(sameBox.instanceId).toBe("test-instance");
|
|
189
|
+
expect(otherBox.instanceId).not.toBe(sameBox.instanceId);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IdentityAppClient.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/IdentityAppClient.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,232 @@
|
|
|
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
|
+
const axios_1 = __importDefault(require("axios"));
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
9
|
+
const IdentityAppClient_1 = require("../IdentityAppClient");
|
|
10
|
+
const errors_1 = require("../errors");
|
|
11
|
+
const identity_offline_verify_1 = require("../identity-offline-verify");
|
|
12
|
+
const identity_token_cache_1 = require("../identity-token-cache");
|
|
13
|
+
jest.mock("axios");
|
|
14
|
+
const mockedAxios = axios_1.default;
|
|
15
|
+
describe("IdentityAppClient", () => {
|
|
16
|
+
const mockAxiosInstance = jest.fn();
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
jest.clearAllMocks();
|
|
19
|
+
mockedAxios.create.mockReturnValue(mockAxiosInstance);
|
|
20
|
+
});
|
|
21
|
+
function createClient(overrides) {
|
|
22
|
+
return new IdentityAppClient_1.IdentityAppClient({
|
|
23
|
+
baseUrl: "https://api.example.com",
|
|
24
|
+
appId: "identity_payroll",
|
|
25
|
+
appApiKey: "iak_test_key",
|
|
26
|
+
...overrides,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
it("requires baseUrl and appId", () => {
|
|
30
|
+
expect(() => new IdentityAppClient_1.IdentityAppClient({ baseUrl: "", appId: "identity_payroll" })).toThrow(errors_1.CliodotApiError);
|
|
31
|
+
expect(() => new IdentityAppClient_1.IdentityAppClient({
|
|
32
|
+
baseUrl: "https://api.example.com",
|
|
33
|
+
appId: "",
|
|
34
|
+
})).toThrow(errors_1.CliodotApiError);
|
|
35
|
+
});
|
|
36
|
+
it("authenticates with bearer api key", async () => {
|
|
37
|
+
mockAxiosInstance.mockResolvedValue({
|
|
38
|
+
data: {
|
|
39
|
+
ok: true,
|
|
40
|
+
access_token: "jwt",
|
|
41
|
+
token_type: "Bearer",
|
|
42
|
+
expires_in: 3600,
|
|
43
|
+
caller_app_id: "identity_payroll",
|
|
44
|
+
target_app_id: "identity_hrms",
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
const client = createClient();
|
|
48
|
+
const result = await client.authenticate({
|
|
49
|
+
target_app_id: "identity_hrms",
|
|
50
|
+
});
|
|
51
|
+
expect(result.access_token).toBe("jwt");
|
|
52
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
53
|
+
method: "POST",
|
|
54
|
+
url: "/v1/authenticate",
|
|
55
|
+
headers: {
|
|
56
|
+
"x-cliodot-app-id": "identity_payroll",
|
|
57
|
+
Authorization: "Bearer iak_test_key",
|
|
58
|
+
},
|
|
59
|
+
data: { target_app_id: "identity_hrms" },
|
|
60
|
+
}));
|
|
61
|
+
});
|
|
62
|
+
it("authenticate and verify pass scopes", async () => {
|
|
63
|
+
mockAxiosInstance.mockResolvedValue({
|
|
64
|
+
data: {
|
|
65
|
+
ok: true,
|
|
66
|
+
access_token: "jwt",
|
|
67
|
+
token_type: "Bearer",
|
|
68
|
+
expires_in: 3600,
|
|
69
|
+
caller_app_id: "identity_payroll",
|
|
70
|
+
target_app_id: "identity_hrms",
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const client = createClient();
|
|
74
|
+
await client.authenticate({
|
|
75
|
+
target_app_id: "identity_hrms",
|
|
76
|
+
scopes: ["employees.read"],
|
|
77
|
+
});
|
|
78
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
79
|
+
data: {
|
|
80
|
+
target_app_id: "identity_hrms",
|
|
81
|
+
scopes: ["employees.read"],
|
|
82
|
+
},
|
|
83
|
+
}));
|
|
84
|
+
mockAxiosInstance.mockResolvedValue({
|
|
85
|
+
data: {
|
|
86
|
+
ok: true,
|
|
87
|
+
valid: true,
|
|
88
|
+
jti: "j1",
|
|
89
|
+
caller_app_id: "identity_payroll",
|
|
90
|
+
target_app_id: "identity_hrms",
|
|
91
|
+
expires_at: new Date().toISOString(),
|
|
92
|
+
claims: { scope: "employees.read" },
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
await client.verify({
|
|
96
|
+
access_token: "jwt",
|
|
97
|
+
required_scopes: ["employees.read"],
|
|
98
|
+
});
|
|
99
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
100
|
+
data: {
|
|
101
|
+
access_token: "jwt",
|
|
102
|
+
required_scopes: ["employees.read"],
|
|
103
|
+
},
|
|
104
|
+
}));
|
|
105
|
+
});
|
|
106
|
+
it("uses app secret headers when no api key", async () => {
|
|
107
|
+
mockAxiosInstance.mockResolvedValue({
|
|
108
|
+
data: {
|
|
109
|
+
ok: true,
|
|
110
|
+
app: {
|
|
111
|
+
_id: "identity_payroll",
|
|
112
|
+
name: "Payroll",
|
|
113
|
+
slug: "identity_payroll",
|
|
114
|
+
tenant_id: "t1",
|
|
115
|
+
status: "active",
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
const client = createClient({
|
|
120
|
+
appApiKey: undefined,
|
|
121
|
+
appSecret: "secret_hex",
|
|
122
|
+
});
|
|
123
|
+
await client.me();
|
|
124
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
125
|
+
method: "GET",
|
|
126
|
+
url: "/v1/me",
|
|
127
|
+
headers: {
|
|
128
|
+
"x-cliodot-app-id": "identity_payroll",
|
|
129
|
+
"x-cliodot-app-secret": "secret_hex",
|
|
130
|
+
},
|
|
131
|
+
}));
|
|
132
|
+
});
|
|
133
|
+
it("maps API error codes", async () => {
|
|
134
|
+
mockAxiosInstance.mockResolvedValue({
|
|
135
|
+
data: {
|
|
136
|
+
ok: false,
|
|
137
|
+
error: "Target app does not trust the caller",
|
|
138
|
+
code: "IDENTITY_TRUST_DENIED",
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
const client = createClient();
|
|
142
|
+
await expect(client.authenticate({ target_app_id: "identity_hrms" })).rejects.toMatchObject({
|
|
143
|
+
code: "IDENTITY_TRUST_DENIED",
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
it("fetches JWKS without auth headers", async () => {
|
|
147
|
+
mockAxiosInstance.mockResolvedValue({
|
|
148
|
+
data: { keys: [{ kid: "k1", kty: "RSA" }] },
|
|
149
|
+
});
|
|
150
|
+
const client = createClient();
|
|
151
|
+
const jwks = await client.getJwks("identity_hrms");
|
|
152
|
+
expect(jwks.keys[0].kid).toBe("k1");
|
|
153
|
+
expect(mockAxiosInstance).toHaveBeenCalledWith(expect.objectContaining({
|
|
154
|
+
method: "GET",
|
|
155
|
+
url: "/v1/apps/identity_hrms/jwks.json",
|
|
156
|
+
}));
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
describe("verifyIdentityTokenOffline", () => {
|
|
160
|
+
beforeEach(() => {
|
|
161
|
+
(0, identity_offline_verify_1.clearIdentityJwksCache)();
|
|
162
|
+
});
|
|
163
|
+
it("verifies HS256 tokens", async () => {
|
|
164
|
+
const secret = "ijwt_test_secret_value_1234567890";
|
|
165
|
+
const token = jsonwebtoken_1.default.sign({ sub: "identity_payroll", jti: "j1", exp: Math.floor(Date.now() / 1000) + 60 }, secret, { algorithm: "HS256", noTimestamp: true });
|
|
166
|
+
const claims = await (0, identity_offline_verify_1.verifyIdentityTokenOffline)(token, {
|
|
167
|
+
algorithm: "HS256",
|
|
168
|
+
secret,
|
|
169
|
+
});
|
|
170
|
+
expect(claims.jti).toBe("j1");
|
|
171
|
+
});
|
|
172
|
+
it("verifies RS256 via JWKS kid", async () => {
|
|
173
|
+
const { publicKey, privateKey } = crypto_1.default.generateKeyPairSync("rsa", {
|
|
174
|
+
modulusLength: 2048,
|
|
175
|
+
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
176
|
+
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
177
|
+
});
|
|
178
|
+
const jwk = crypto_1.default
|
|
179
|
+
.createPublicKey(publicKey)
|
|
180
|
+
.export({ format: "jwk" });
|
|
181
|
+
const token = jsonwebtoken_1.default.sign({ sub: "identity_payroll", jti: "j2", exp: Math.floor(Date.now() / 1000) + 60 }, privateKey, { algorithm: "RS256", keyid: "kid_1", noTimestamp: true });
|
|
182
|
+
const fetchImpl = jest.fn().mockResolvedValue({
|
|
183
|
+
ok: true,
|
|
184
|
+
json: async () => ({
|
|
185
|
+
keys: [
|
|
186
|
+
{
|
|
187
|
+
kty: jwk.kty || "RSA",
|
|
188
|
+
kid: "kid_1",
|
|
189
|
+
n: jwk.n,
|
|
190
|
+
e: jwk.e,
|
|
191
|
+
alg: "RS256",
|
|
192
|
+
use: "sig",
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
}),
|
|
196
|
+
});
|
|
197
|
+
const claims = await (0, identity_offline_verify_1.verifyIdentityTokenOffline)(token, {
|
|
198
|
+
algorithm: "RS256",
|
|
199
|
+
jwksUrl: "https://api.example.com/identity/v1/apps/identity_hrms/jwks.json",
|
|
200
|
+
fetch: fetchImpl,
|
|
201
|
+
});
|
|
202
|
+
expect(claims.jti).toBe("j2");
|
|
203
|
+
expect(fetchImpl).toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
it("tokenHasScopes checks JWT scope claim", () => {
|
|
206
|
+
const token = jsonwebtoken_1.default.sign({ scope: "employees.read employees.write", jti: "j" }, "secret", { algorithm: "HS256", noTimestamp: true });
|
|
207
|
+
expect((0, identity_offline_verify_1.tokenHasScopes)(token, ["employees.read"])).toBe(true);
|
|
208
|
+
expect((0, identity_offline_verify_1.tokenHasScopes)(token, ["employees.admin"])).toBe(false);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
describe("createIdentityTokenCache", () => {
|
|
212
|
+
it("authenticates then reuses until near expiry", async () => {
|
|
213
|
+
const authenticate = jest.fn().mockResolvedValue({
|
|
214
|
+
ok: true,
|
|
215
|
+
access_token: "at_1",
|
|
216
|
+
token_type: "Bearer",
|
|
217
|
+
expires_in: 3600,
|
|
218
|
+
refresh_token: "irt_1",
|
|
219
|
+
caller_app_id: "identity_payroll",
|
|
220
|
+
target_app_id: "identity_hrms",
|
|
221
|
+
});
|
|
222
|
+
const refresh = jest.fn();
|
|
223
|
+
const cache = (0, identity_token_cache_1.createIdentityTokenCache)({
|
|
224
|
+
client: { authenticate, refresh },
|
|
225
|
+
});
|
|
226
|
+
const first = await cache.getAccessToken("identity_hrms");
|
|
227
|
+
const second = await cache.getAccessToken("identity_hrms");
|
|
228
|
+
expect(first).toBe("at_1");
|
|
229
|
+
expect(second).toBe("at_1");
|
|
230
|
+
expect(authenticate).toHaveBeenCalledTimes(1);
|
|
231
|
+
});
|
|
232
|
+
});
|