@mshindi-labs/google 0.0.0-stage → 1.0.2
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 +21 -0
- package/README.md +90 -2
- package/dist/chunk-JVP7HHFU.js +76 -0
- package/dist/chunk-JVP7HHFU.js.map +1 -0
- package/dist/chunk-KJPQ3DMR.cjs +76 -0
- package/dist/chunk-KJPQ3DMR.cjs.map +1 -0
- package/dist/chunk-RWT25QHA.js +190 -0
- package/dist/chunk-RWT25QHA.js.map +1 -0
- package/dist/chunk-SHDP4A3A.cjs +190 -0
- package/dist/chunk-SHDP4A3A.cjs.map +1 -0
- package/dist/chunk-SIDO26T7.js +949 -0
- package/dist/chunk-SIDO26T7.js.map +1 -0
- package/dist/chunk-V22QP2AM.cjs +49 -0
- package/dist/chunk-V22QP2AM.cjs.map +1 -0
- package/dist/chunk-VN5VWVCT.cjs +949 -0
- package/dist/chunk-VN5VWVCT.cjs.map +1 -0
- package/dist/chunk-Z7O6GO3C.js +49 -0
- package/dist/chunk-Z7O6GO3C.js.map +1 -0
- package/dist/core.cjs +289 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +173 -0
- package/dist/core.d.ts +173 -0
- package/dist/core.js +289 -0
- package/dist/core.js.map +1 -0
- package/dist/create-google-CPGcK-3G.d.cts +107 -0
- package/dist/create-google-nP9f9jd9.d.ts +107 -0
- package/dist/events-BsWOMtza.d.cts +160 -0
- package/dist/events-BsWOMtza.d.ts +160 -0
- package/dist/gmail.cjs +489 -0
- package/dist/gmail.cjs.map +1 -0
- package/dist/gmail.d.cts +53 -0
- package/dist/gmail.d.ts +53 -0
- package/dist/gmail.js +489 -0
- package/dist/gmail.js.map +1 -0
- package/dist/key-provider-Cq_14Bh6.d.cts +53 -0
- package/dist/key-provider-Cq_14Bh6.d.ts +53 -0
- package/dist/postgres.cjs +455 -0
- package/dist/postgres.cjs.map +1 -0
- package/dist/postgres.d.cts +22 -0
- package/dist/postgres.d.ts +22 -0
- package/dist/postgres.js +455 -0
- package/dist/postgres.js.map +1 -0
- package/dist/static-key-provider-BhyRhY6-.d.ts +18 -0
- package/dist/static-key-provider-CPNt4YvF.d.cts +18 -0
- package/dist/testing.cjs +829 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +147 -0
- package/dist/testing.d.ts +147 -0
- package/dist/testing.js +829 -0
- package/dist/testing.js.map +1 -0
- package/dist/transport-CVOgr9XP.d.cts +20 -0
- package/dist/transport-CVOgr9XP.d.ts +20 -0
- package/package.json +143 -4
package/dist/testing.js
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StaticKeyProvider
|
|
3
|
+
} from "./chunk-JVP7HHFU.js";
|
|
4
|
+
import {
|
|
5
|
+
AUTH_ENDPOINT,
|
|
6
|
+
InvalidGrantError,
|
|
7
|
+
codeChallenge,
|
|
8
|
+
loadPeer
|
|
9
|
+
} from "./chunk-Z7O6GO3C.js";
|
|
10
|
+
import {
|
|
11
|
+
InvalidConfigError
|
|
12
|
+
} from "./chunk-RWT25QHA.js";
|
|
13
|
+
|
|
14
|
+
// src/testing/local-key-provider.ts
|
|
15
|
+
import { randomBytes } from "crypto";
|
|
16
|
+
var LocalKeyProvider = class extends StaticKeyProvider {
|
|
17
|
+
constructor(opts = {}) {
|
|
18
|
+
if (process.env.NODE_ENV === "production" && !opts.allowInProduction) {
|
|
19
|
+
throw new InvalidConfigError(
|
|
20
|
+
"LocalKeyProvider must not be used in production (pass allowInProduction: true to override)"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
super({ keys: { local: opts.key ?? randomBytes(32) }, current: "local" });
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/testing/store-contract.ts
|
|
28
|
+
var ev = (ct) => ({
|
|
29
|
+
v: 1,
|
|
30
|
+
kid: "static:test",
|
|
31
|
+
wdek: "AA",
|
|
32
|
+
iv: "AA",
|
|
33
|
+
ct,
|
|
34
|
+
tag: "AA"
|
|
35
|
+
});
|
|
36
|
+
var C = (n) => `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`;
|
|
37
|
+
var T0 = /* @__PURE__ */ new Date("2026-01-01T00:00:00.000Z");
|
|
38
|
+
var client = (tenantId, ct = `secret-${tenantId}`) => ({
|
|
39
|
+
tenantId,
|
|
40
|
+
clientId: `${tenantId}.apps.googleusercontent.com`,
|
|
41
|
+
clientSecret: ev(ct),
|
|
42
|
+
secretHint: "\u2026abcd",
|
|
43
|
+
hostedDomain: null,
|
|
44
|
+
createdBy: "admin",
|
|
45
|
+
createdAt: T0,
|
|
46
|
+
updatedBy: "admin",
|
|
47
|
+
updatedAt: T0
|
|
48
|
+
});
|
|
49
|
+
var conn = (id, tenantId, googleSub, over = {}) => ({
|
|
50
|
+
id,
|
|
51
|
+
tenantId,
|
|
52
|
+
googleSub,
|
|
53
|
+
email: `${googleSub}@example.com`,
|
|
54
|
+
displayName: null,
|
|
55
|
+
scopes: ["openid", "email"],
|
|
56
|
+
refreshToken: ev(`rt-${id}`),
|
|
57
|
+
status: "active",
|
|
58
|
+
statusReason: null,
|
|
59
|
+
connectedBy: "admin",
|
|
60
|
+
connectedAt: T0,
|
|
61
|
+
updatedAt: T0,
|
|
62
|
+
lastUsedAt: null,
|
|
63
|
+
...over
|
|
64
|
+
});
|
|
65
|
+
var hashBytes = (n) => new Uint8Array(32).fill(n);
|
|
66
|
+
var hex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
67
|
+
var state = (n, tenantId, expiresAt) => ({
|
|
68
|
+
stateHash: hashBytes(n),
|
|
69
|
+
tenantId,
|
|
70
|
+
actorId: "admin",
|
|
71
|
+
intendedEmail: null,
|
|
72
|
+
connectionId: null,
|
|
73
|
+
scopes: ["openid", "email"],
|
|
74
|
+
codeVerifier: ev(`cv-${n}`),
|
|
75
|
+
expiresAt
|
|
76
|
+
});
|
|
77
|
+
function runStoreContractTests(name, makeStore, api) {
|
|
78
|
+
const { describe, it, expect, beforeEach } = api;
|
|
79
|
+
describe(`CredentialStore contract: ${name}`, () => {
|
|
80
|
+
let store;
|
|
81
|
+
beforeEach(async () => {
|
|
82
|
+
store = await makeStore();
|
|
83
|
+
});
|
|
84
|
+
const seedClients = async () => {
|
|
85
|
+
await store.upsertOAuthClient(client("t1"), { revokeConnections: false });
|
|
86
|
+
await store.upsertOAuthClient(client("t2"), { revokeConnections: false });
|
|
87
|
+
};
|
|
88
|
+
it("getOAuthClient returns null for unknown tenant and a full record after upsert", async () => {
|
|
89
|
+
expect(await store.getOAuthClient("nope")).toBeNull();
|
|
90
|
+
const rec = client("t1");
|
|
91
|
+
await store.upsertOAuthClient(rec, { revokeConnections: false });
|
|
92
|
+
const got = await store.getOAuthClient("t1");
|
|
93
|
+
expect(got).toEqual(rec);
|
|
94
|
+
expect(got.createdAt).toBeInstanceOf(Date);
|
|
95
|
+
expect(got.updatedAt).toBeInstanceOf(Date);
|
|
96
|
+
expect(got.createdAt.getTime()).toBe(T0.getTime());
|
|
97
|
+
});
|
|
98
|
+
it("isolates tenants on getConnection", async () => {
|
|
99
|
+
await seedClients();
|
|
100
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
101
|
+
expect(await store.getConnection("t2", C(1))).toBeNull();
|
|
102
|
+
expect((await store.getConnection("t1", C(1))).id).toBe(C(1));
|
|
103
|
+
});
|
|
104
|
+
it("listConnections excludes other tenants", async () => {
|
|
105
|
+
await seedClients();
|
|
106
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
107
|
+
await store.upsertConnection(conn(C(2), "t2", "sub2"));
|
|
108
|
+
const list = await store.listConnections("t1");
|
|
109
|
+
expect(list.map((c) => c.id)).toEqual([C(1)]);
|
|
110
|
+
});
|
|
111
|
+
it("findConnectionBySub is tenant-scoped", async () => {
|
|
112
|
+
await seedClients();
|
|
113
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
114
|
+
expect((await store.findConnectionBySub("t1", "sub1")).id).toBe(C(1));
|
|
115
|
+
expect(await store.findConnectionBySub("t2", "sub1")).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
it("upsertConnection with same (tenant, sub) and same id updates in place", async () => {
|
|
118
|
+
await seedClients();
|
|
119
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
120
|
+
await store.upsertConnection(
|
|
121
|
+
conn(C(1), "t1", "sub1", { email: "new@example.com" })
|
|
122
|
+
);
|
|
123
|
+
const list = await store.listConnections("t1");
|
|
124
|
+
expect(list).toHaveLength(1);
|
|
125
|
+
expect(list[0].email).toBe("new@example.com");
|
|
126
|
+
});
|
|
127
|
+
it("upsertConnection with a different id but same (tenant, sub) rejects", async () => {
|
|
128
|
+
await seedClients();
|
|
129
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
130
|
+
await expect(
|
|
131
|
+
store.upsertConnection(conn(C(2), "t1", "sub1"))
|
|
132
|
+
).rejects.toThrow();
|
|
133
|
+
expect(await store.listConnections("t1")).toHaveLength(1);
|
|
134
|
+
});
|
|
135
|
+
it("upsertOAuthClient with revokeConnections revokes only that tenant", async () => {
|
|
136
|
+
await seedClients();
|
|
137
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
138
|
+
await store.upsertConnection(conn(C(2), "t1", "sub2"));
|
|
139
|
+
await store.upsertConnection(conn(C(3), "t2", "sub3"));
|
|
140
|
+
await store.upsertOAuthClient(client("t1", "rotated"), {
|
|
141
|
+
revokeConnections: true
|
|
142
|
+
});
|
|
143
|
+
for (const c of await store.listConnections("t1")) {
|
|
144
|
+
expect(c.status).toBe("revoked");
|
|
145
|
+
expect(c.statusReason).toBe("client_changed");
|
|
146
|
+
}
|
|
147
|
+
const other = await store.getConnection("t2", C(3));
|
|
148
|
+
expect(other.status).toBe("active");
|
|
149
|
+
expect(other.statusReason).toBeNull();
|
|
150
|
+
});
|
|
151
|
+
it("upsertOAuthClient without revokeConnections leaves connections alone", async () => {
|
|
152
|
+
await seedClients();
|
|
153
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
154
|
+
await store.upsertOAuthClient(client("t1", "rotated"), {
|
|
155
|
+
revokeConnections: false
|
|
156
|
+
});
|
|
157
|
+
expect((await store.getConnection("t1", C(1))).status).toBe("active");
|
|
158
|
+
});
|
|
159
|
+
it("upsertOAuthClient update path replaces the record and leaves other tenants alone", async () => {
|
|
160
|
+
await seedClients();
|
|
161
|
+
await store.upsertOAuthClient(client("t1", "rotated"), {
|
|
162
|
+
revokeConnections: false
|
|
163
|
+
});
|
|
164
|
+
expect((await store.getOAuthClient("t1")).clientSecret.ct).toBe(
|
|
165
|
+
"rotated"
|
|
166
|
+
);
|
|
167
|
+
expect(await store.getOAuthClient("t2")).toEqual(client("t2"));
|
|
168
|
+
});
|
|
169
|
+
it("getOAuthClient returns each tenant its own record", async () => {
|
|
170
|
+
await seedClients();
|
|
171
|
+
expect((await store.getOAuthClient("t1")).clientSecret.ct).toBe(
|
|
172
|
+
"secret-t1"
|
|
173
|
+
);
|
|
174
|
+
expect((await store.getOAuthClient("t2")).clientSecret.ct).toBe(
|
|
175
|
+
"secret-t2"
|
|
176
|
+
);
|
|
177
|
+
expect((await store.getOAuthClient("t1")).tenantId).toBe("t1");
|
|
178
|
+
});
|
|
179
|
+
it("upsertConnection allows the same googleSub in different tenants", async () => {
|
|
180
|
+
await seedClients();
|
|
181
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
182
|
+
await store.upsertConnection(conn(C(2), "t2", "sub1"));
|
|
183
|
+
expect((await store.getConnection("t1", C(1))).id).toBe(C(1));
|
|
184
|
+
expect((await store.getConnection("t2", C(2))).id).toBe(C(2));
|
|
185
|
+
});
|
|
186
|
+
it("upsertConnection cannot take over another tenant's connection id", async () => {
|
|
187
|
+
await seedClients();
|
|
188
|
+
const original = conn(C(1), "t1", "sub1");
|
|
189
|
+
await store.upsertConnection(original);
|
|
190
|
+
await expect(
|
|
191
|
+
store.upsertConnection(
|
|
192
|
+
conn(C(1), "t2", "subX", { email: "evil@example.com" })
|
|
193
|
+
)
|
|
194
|
+
).rejects.toThrow();
|
|
195
|
+
expect(await store.getConnection("t1", C(1))).toEqual(original);
|
|
196
|
+
expect(await store.getConnection("t2", C(1))).toBeNull();
|
|
197
|
+
});
|
|
198
|
+
it("consumeOAuthState returns the record once, then null", async () => {
|
|
199
|
+
const rec = state(1, "t1", new Date(T0.getTime() + 6e5));
|
|
200
|
+
await store.insertOAuthState(rec);
|
|
201
|
+
const first = await store.consumeOAuthState(hex(rec.stateHash), T0);
|
|
202
|
+
expect(first).not.toBeNull();
|
|
203
|
+
expect(first.stateHash).toBeInstanceOf(Uint8Array);
|
|
204
|
+
expect(Array.from(first.stateHash)).toEqual(Array.from(rec.stateHash));
|
|
205
|
+
expect(first.expiresAt).toBeInstanceOf(Date);
|
|
206
|
+
expect(first.tenantId).toBe("t1");
|
|
207
|
+
expect(await store.consumeOAuthState(hex(rec.stateHash), T0)).toBeNull();
|
|
208
|
+
});
|
|
209
|
+
it("concurrent consumeOAuthState yields exactly one winner", async () => {
|
|
210
|
+
const rec = state(2, "t1", new Date(T0.getTime() + 6e5));
|
|
211
|
+
await store.insertOAuthState(rec);
|
|
212
|
+
const results = await Promise.all([
|
|
213
|
+
store.consumeOAuthState(hex(rec.stateHash), T0),
|
|
214
|
+
store.consumeOAuthState(hex(rec.stateHash), T0),
|
|
215
|
+
store.consumeOAuthState(hex(rec.stateHash), T0)
|
|
216
|
+
]);
|
|
217
|
+
expect(results.filter((r) => r !== null)).toHaveLength(1);
|
|
218
|
+
});
|
|
219
|
+
it("consumeOAuthState returns null when expired", async () => {
|
|
220
|
+
const rec = state(3, "t1", new Date(T0.getTime() + 1e3));
|
|
221
|
+
await store.insertOAuthState(rec);
|
|
222
|
+
const later = new Date(T0.getTime() + 2e3);
|
|
223
|
+
expect(
|
|
224
|
+
await store.consumeOAuthState(hex(rec.stateHash), later)
|
|
225
|
+
).toBeNull();
|
|
226
|
+
});
|
|
227
|
+
it("deleteExpiredOAuthStates returns the count deleted", async () => {
|
|
228
|
+
await store.insertOAuthState(
|
|
229
|
+
state(4, "t1", new Date(T0.getTime() + 1e3))
|
|
230
|
+
);
|
|
231
|
+
await store.insertOAuthState(
|
|
232
|
+
state(5, "t2", new Date(T0.getTime() + 2e3))
|
|
233
|
+
);
|
|
234
|
+
await store.insertOAuthState(
|
|
235
|
+
state(6, "t1", new Date(T0.getTime() + 9e5))
|
|
236
|
+
);
|
|
237
|
+
const n = await store.deleteExpiredOAuthStates(
|
|
238
|
+
new Date(T0.getTime() + 5e3)
|
|
239
|
+
);
|
|
240
|
+
expect(n).toBe(2);
|
|
241
|
+
expect(
|
|
242
|
+
await store.deleteExpiredOAuthStates(new Date(T0.getTime() + 5e3))
|
|
243
|
+
).toBe(0);
|
|
244
|
+
});
|
|
245
|
+
it("iterateEncryptedValues yields every encrypted field across tenants; replaceEncryptedValue persists", async () => {
|
|
246
|
+
await seedClients();
|
|
247
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
248
|
+
await store.upsertConnection(conn(C(2), "t2", "sub2"));
|
|
249
|
+
await store.upsertConnection(
|
|
250
|
+
conn(C(3), "t2", "sub3", { refreshToken: null, status: "revoked" })
|
|
251
|
+
);
|
|
252
|
+
const st = state(7, "t1", new Date(T0.getTime() + 6e5));
|
|
253
|
+
await store.insertOAuthState(st);
|
|
254
|
+
const refs = [];
|
|
255
|
+
for await (const r of store.iterateEncryptedValues(2)) refs.push(r);
|
|
256
|
+
const key = (r) => `${r.tenantId}|${r.record}|${r.recordId}|${r.field}`;
|
|
257
|
+
const expectedCt = {
|
|
258
|
+
"t1|oauth_client|t1|client_secret": "secret-t1",
|
|
259
|
+
"t2|oauth_client|t2|client_secret": "secret-t2",
|
|
260
|
+
[`t1|connection|${C(1)}|refresh_token`]: `rt-${C(1)}`,
|
|
261
|
+
[`t2|connection|${C(2)}|refresh_token`]: `rt-${C(2)}`,
|
|
262
|
+
[`t1|oauth_state|${hex(st.stateHash)}|code_verifier`]: "cv-7"
|
|
263
|
+
};
|
|
264
|
+
for (const r of refs) expect(r.value.ct).toBe(expectedCt[key(r)]);
|
|
265
|
+
expect(refs.map(key).sort()).toEqual(
|
|
266
|
+
[
|
|
267
|
+
"t1|oauth_client|t1|client_secret",
|
|
268
|
+
"t2|oauth_client|t2|client_secret",
|
|
269
|
+
`t1|connection|${C(1)}|refresh_token`,
|
|
270
|
+
`t2|connection|${C(2)}|refresh_token`,
|
|
271
|
+
`t1|oauth_state|${hex(st.stateHash)}|code_verifier`
|
|
272
|
+
].sort()
|
|
273
|
+
);
|
|
274
|
+
for (const r of refs) {
|
|
275
|
+
await store.replaceEncryptedValue(
|
|
276
|
+
r,
|
|
277
|
+
ev(`new-${r.record}-${r.recordId}`)
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
expect((await store.getOAuthClient("t1")).clientSecret.ct).toBe(
|
|
281
|
+
"new-oauth_client-t1"
|
|
282
|
+
);
|
|
283
|
+
expect((await store.getOAuthClient("t2")).clientSecret.ct).toBe(
|
|
284
|
+
"new-oauth_client-t2"
|
|
285
|
+
);
|
|
286
|
+
expect((await store.getConnection("t1", C(1))).refreshToken.ct).toBe(
|
|
287
|
+
`new-connection-${C(1)}`
|
|
288
|
+
);
|
|
289
|
+
expect((await store.getConnection("t2", C(2))).refreshToken.ct).toBe(
|
|
290
|
+
`new-connection-${C(2)}`
|
|
291
|
+
);
|
|
292
|
+
expect((await store.getConnection("t2", C(3))).refreshToken).toBeNull();
|
|
293
|
+
const consumed = await store.consumeOAuthState(hex(st.stateHash), T0);
|
|
294
|
+
expect(consumed.codeVerifier.ct).toBe(
|
|
295
|
+
`new-oauth_state-${hex(st.stateHash)}`
|
|
296
|
+
);
|
|
297
|
+
});
|
|
298
|
+
it("replaceEncryptedValue ignores refs with the wrong tenantId", async () => {
|
|
299
|
+
await seedClients();
|
|
300
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
301
|
+
const st = state(8, "t1", new Date(T0.getTime() + 6e5));
|
|
302
|
+
await store.insertOAuthState(st);
|
|
303
|
+
const base = { value: ev("x") };
|
|
304
|
+
const bad = [
|
|
305
|
+
{
|
|
306
|
+
...base,
|
|
307
|
+
tenantId: "t2",
|
|
308
|
+
record: "connection",
|
|
309
|
+
recordId: C(1),
|
|
310
|
+
field: "refresh_token"
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
...base,
|
|
314
|
+
tenantId: "t2",
|
|
315
|
+
record: "oauth_client",
|
|
316
|
+
recordId: "t1",
|
|
317
|
+
field: "client_secret"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
...base,
|
|
321
|
+
tenantId: "t2",
|
|
322
|
+
record: "oauth_state",
|
|
323
|
+
recordId: hex(st.stateHash),
|
|
324
|
+
field: "code_verifier"
|
|
325
|
+
}
|
|
326
|
+
];
|
|
327
|
+
for (const ref of bad) {
|
|
328
|
+
try {
|
|
329
|
+
expect(await store.replaceEncryptedValue(ref, ev("hijacked"))).toBe(
|
|
330
|
+
false
|
|
331
|
+
);
|
|
332
|
+
} catch {
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
expect((await store.getConnection("t1", C(1))).refreshToken.ct).toBe(
|
|
336
|
+
`rt-${C(1)}`
|
|
337
|
+
);
|
|
338
|
+
expect((await store.getOAuthClient("t1")).clientSecret.ct).toBe(
|
|
339
|
+
"secret-t1"
|
|
340
|
+
);
|
|
341
|
+
const consumed = await store.consumeOAuthState(hex(st.stateHash), T0);
|
|
342
|
+
expect(consumed.codeVerifier.ct).toBe("cv-8");
|
|
343
|
+
});
|
|
344
|
+
it("replaceEncryptedValue during iteration visits every ref exactly once", async () => {
|
|
345
|
+
await seedClients();
|
|
346
|
+
for (let i = 0; i < 5; i++) {
|
|
347
|
+
await store.upsertConnection(conn(C(i), "t1", `sub${i}`));
|
|
348
|
+
}
|
|
349
|
+
await store.insertOAuthState(
|
|
350
|
+
state(9, "t2", new Date(T0.getTime() + 6e5))
|
|
351
|
+
);
|
|
352
|
+
const visited = [];
|
|
353
|
+
for await (const r of store.iterateEncryptedValues(2)) {
|
|
354
|
+
visited.push(`${r.record}|${r.recordId}`);
|
|
355
|
+
await store.replaceEncryptedValue(
|
|
356
|
+
r,
|
|
357
|
+
ev(`new-${r.record}-${r.recordId}`)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
expect(visited).toHaveLength(2 + 5 + 1);
|
|
361
|
+
expect(new Set(visited).size).toBe(visited.length);
|
|
362
|
+
for (let i = 0; i < 5; i++) {
|
|
363
|
+
expect((await store.getConnection("t1", C(i))).refreshToken.ct).toBe(
|
|
364
|
+
`new-connection-${C(i)}`
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
expect((await store.getOAuthClient("t1")).clientSecret.ct).toBe(
|
|
368
|
+
"new-oauth_client-t1"
|
|
369
|
+
);
|
|
370
|
+
expect((await store.getOAuthClient("t2")).clientSecret.ct).toBe(
|
|
371
|
+
"new-oauth_client-t2"
|
|
372
|
+
);
|
|
373
|
+
const consumed = await store.consumeOAuthState(hex(hashBytes(9)), T0);
|
|
374
|
+
expect(consumed.codeVerifier.ct).toBe(
|
|
375
|
+
`new-oauth_state-${hex(hashBytes(9))}`
|
|
376
|
+
);
|
|
377
|
+
});
|
|
378
|
+
it("replaceEncryptedValue is compare-and-swap", async () => {
|
|
379
|
+
await seedClients();
|
|
380
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
381
|
+
const refs = [];
|
|
382
|
+
for await (const r of store.iterateEncryptedValues(10)) refs.push(r);
|
|
383
|
+
const ref = refs.find((r) => r.record === "connection");
|
|
384
|
+
expect(await store.replaceEncryptedValue(ref, ev("concurrent"))).toBe(
|
|
385
|
+
true
|
|
386
|
+
);
|
|
387
|
+
expect(await store.replaceEncryptedValue(ref, ev("stale"))).toBe(false);
|
|
388
|
+
expect((await store.getConnection("t1", C(1))).refreshToken.ct).toBe(
|
|
389
|
+
"concurrent"
|
|
390
|
+
);
|
|
391
|
+
const wrong = { ...ref, tenantId: "t2", value: ev("concurrent") };
|
|
392
|
+
expect(await store.replaceEncryptedValue(wrong, ev("x"))).toBe(false);
|
|
393
|
+
});
|
|
394
|
+
it("tolerates malformed connection ids", async () => {
|
|
395
|
+
await seedClients();
|
|
396
|
+
expect(await store.getConnection("t1", "not-a-uuid")).toBeNull();
|
|
397
|
+
await store.updateConnectionStatus("t1", "not-a-uuid", "error", "x");
|
|
398
|
+
await store.touchConnection("t1", "not-a-uuid", T0);
|
|
399
|
+
});
|
|
400
|
+
it("touchConnection is tenant-scoped", async () => {
|
|
401
|
+
await seedClients();
|
|
402
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
403
|
+
await store.touchConnection(
|
|
404
|
+
"t2",
|
|
405
|
+
C(1),
|
|
406
|
+
/* @__PURE__ */ new Date("2026-02-02T10:00:00.000Z")
|
|
407
|
+
);
|
|
408
|
+
expect((await store.getConnection("t1", C(1))).lastUsedAt).toBeNull();
|
|
409
|
+
});
|
|
410
|
+
it("touchConnection sets lastUsedAt", async () => {
|
|
411
|
+
await seedClients();
|
|
412
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
413
|
+
const at = /* @__PURE__ */ new Date("2026-02-02T10:00:00.000Z");
|
|
414
|
+
await store.touchConnection("t1", C(1), at);
|
|
415
|
+
const got = await store.getConnection("t1", C(1));
|
|
416
|
+
expect(got.lastUsedAt).toBeInstanceOf(Date);
|
|
417
|
+
expect(got.lastUsedAt.getTime()).toBe(at.getTime());
|
|
418
|
+
});
|
|
419
|
+
it("setConnectionDisplayName updates displayName and updatedAt only", async () => {
|
|
420
|
+
await seedClients();
|
|
421
|
+
const original = conn(C(1), "t1", "sub1");
|
|
422
|
+
await store.upsertConnection(original);
|
|
423
|
+
const at = /* @__PURE__ */ new Date("2026-03-03T03:00:00.000Z");
|
|
424
|
+
expect(await store.setConnectionDisplayName("t1", C(1), "HR", at)).toBe(
|
|
425
|
+
true
|
|
426
|
+
);
|
|
427
|
+
const got = await store.getConnection("t1", C(1));
|
|
428
|
+
expect(got).toEqual({ ...original, displayName: "HR", updatedAt: at });
|
|
429
|
+
expect(got.updatedAt).toBeInstanceOf(Date);
|
|
430
|
+
expect(await store.setConnectionDisplayName("t1", C(1), null, at)).toBe(
|
|
431
|
+
true
|
|
432
|
+
);
|
|
433
|
+
expect((await store.getConnection("t1", C(1))).displayName).toBeNull();
|
|
434
|
+
});
|
|
435
|
+
it("setConnectionDisplayName never changes status or refresh token on a revoked row", async () => {
|
|
436
|
+
await seedClients();
|
|
437
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
438
|
+
await store.upsertOAuthClient(client("t1", "rotated"), {
|
|
439
|
+
revokeConnections: true
|
|
440
|
+
});
|
|
441
|
+
const before = await store.getConnection("t1", C(1));
|
|
442
|
+
expect(before.status).toBe("revoked");
|
|
443
|
+
const at = /* @__PURE__ */ new Date("2026-03-03T03:00:00.000Z");
|
|
444
|
+
expect(await store.setConnectionDisplayName("t1", C(1), "HR", at)).toBe(
|
|
445
|
+
true
|
|
446
|
+
);
|
|
447
|
+
const got = await store.getConnection("t1", C(1));
|
|
448
|
+
expect(got.status).toBe("revoked");
|
|
449
|
+
expect(got.statusReason).toBe("client_changed");
|
|
450
|
+
expect(got.refreshToken).toEqual(before.refreshToken);
|
|
451
|
+
expect(got.displayName).toBe("HR");
|
|
452
|
+
});
|
|
453
|
+
it("setConnectionDisplayName with the wrong tenant or unknown id \u2192 false, unchanged", async () => {
|
|
454
|
+
await seedClients();
|
|
455
|
+
const original = conn(C(1), "t1", "sub1");
|
|
456
|
+
await store.upsertConnection(original);
|
|
457
|
+
const at = /* @__PURE__ */ new Date("2026-03-03T03:00:00.000Z");
|
|
458
|
+
expect(await store.setConnectionDisplayName("t2", C(1), "X", at)).toBe(
|
|
459
|
+
false
|
|
460
|
+
);
|
|
461
|
+
expect(await store.setConnectionDisplayName("t1", C(9), "X", at)).toBe(
|
|
462
|
+
false
|
|
463
|
+
);
|
|
464
|
+
expect(
|
|
465
|
+
await store.setConnectionDisplayName("t1", "not-a-uuid", "X", at)
|
|
466
|
+
).toBe(false);
|
|
467
|
+
expect(await store.getConnection("t1", C(1))).toEqual(original);
|
|
468
|
+
expect(await store.getConnection("t2", C(1))).toBeNull();
|
|
469
|
+
});
|
|
470
|
+
it("disconnectConnection nulls the token and marks revoked/disconnected", async () => {
|
|
471
|
+
await seedClients();
|
|
472
|
+
const original = conn(C(1), "t1", "sub1", { displayName: "HR" });
|
|
473
|
+
await store.upsertConnection(original);
|
|
474
|
+
const at = /* @__PURE__ */ new Date("2026-03-03T03:00:00.000Z");
|
|
475
|
+
expect(await store.disconnectConnection("t1", C(1), at)).toBe(true);
|
|
476
|
+
const got = await store.getConnection("t1", C(1));
|
|
477
|
+
expect(got).toEqual({
|
|
478
|
+
...original,
|
|
479
|
+
refreshToken: null,
|
|
480
|
+
status: "revoked",
|
|
481
|
+
statusReason: "disconnected",
|
|
482
|
+
updatedAt: at
|
|
483
|
+
});
|
|
484
|
+
const refs = [];
|
|
485
|
+
for await (const r of store.iterateEncryptedValues(10)) refs.push(r);
|
|
486
|
+
expect(refs.filter((r) => r.record === "connection")).toHaveLength(0);
|
|
487
|
+
});
|
|
488
|
+
it("disconnectConnection with the wrong tenant or unknown id \u2192 false, unchanged", async () => {
|
|
489
|
+
await seedClients();
|
|
490
|
+
const original = conn(C(1), "t1", "sub1");
|
|
491
|
+
await store.upsertConnection(original);
|
|
492
|
+
const at = /* @__PURE__ */ new Date("2026-03-03T03:00:00.000Z");
|
|
493
|
+
expect(await store.disconnectConnection("t2", C(1), at)).toBe(false);
|
|
494
|
+
expect(await store.disconnectConnection("t1", C(9), at)).toBe(false);
|
|
495
|
+
expect(await store.disconnectConnection("t1", "not-a-uuid", at)).toBe(
|
|
496
|
+
false
|
|
497
|
+
);
|
|
498
|
+
expect(await store.getConnection("t1", C(1))).toEqual(original);
|
|
499
|
+
});
|
|
500
|
+
it("updateConnectionStatus sets status and reason (tenant-scoped)", async () => {
|
|
501
|
+
await seedClients();
|
|
502
|
+
await store.upsertConnection(conn(C(1), "t1", "sub1"));
|
|
503
|
+
await store.updateConnectionStatus("t2", C(1), "error", "wrong tenant");
|
|
504
|
+
expect((await store.getConnection("t1", C(1))).status).toBe("active");
|
|
505
|
+
await store.updateConnectionStatus("t1", C(1), "error", "invalid_grant");
|
|
506
|
+
const got = await store.getConnection("t1", C(1));
|
|
507
|
+
expect(got.status).toBe("error");
|
|
508
|
+
expect(got.statusReason).toBe("invalid_grant");
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// src/testing/in-memory-store.ts
|
|
514
|
+
var clone = (v) => structuredClone(v);
|
|
515
|
+
var toHex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
516
|
+
var toBytes = (b) => new Uint8Array(b);
|
|
517
|
+
var InMemoryStore = class {
|
|
518
|
+
clients = /* @__PURE__ */ new Map();
|
|
519
|
+
connections = /* @__PURE__ */ new Map();
|
|
520
|
+
// by connection id
|
|
521
|
+
states = /* @__PURE__ */ new Map();
|
|
522
|
+
// by lowercase hex hash
|
|
523
|
+
async getOAuthClient(tenantId) {
|
|
524
|
+
const r = this.clients.get(tenantId);
|
|
525
|
+
return r ? clone(r) : null;
|
|
526
|
+
}
|
|
527
|
+
async upsertOAuthClient(rec, opts) {
|
|
528
|
+
this.clients.set(rec.tenantId, clone(rec));
|
|
529
|
+
if (opts.revokeConnections) {
|
|
530
|
+
for (const c of this.connections.values()) {
|
|
531
|
+
if (c.tenantId === rec.tenantId) {
|
|
532
|
+
c.status = "revoked";
|
|
533
|
+
c.statusReason = "client_changed";
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
async getConnection(tenantId, connectionId) {
|
|
539
|
+
const c = this.connections.get(connectionId);
|
|
540
|
+
return c && c.tenantId === tenantId ? clone(c) : null;
|
|
541
|
+
}
|
|
542
|
+
async findConnectionBySub(tenantId, googleSub) {
|
|
543
|
+
for (const c of this.connections.values()) {
|
|
544
|
+
if (c.tenantId === tenantId && c.googleSub === googleSub) return clone(c);
|
|
545
|
+
}
|
|
546
|
+
return null;
|
|
547
|
+
}
|
|
548
|
+
async listConnections(tenantId) {
|
|
549
|
+
return [...this.connections.values()].filter((c) => c.tenantId === tenantId).map(clone);
|
|
550
|
+
}
|
|
551
|
+
async upsertConnection(rec) {
|
|
552
|
+
for (const c of this.connections.values()) {
|
|
553
|
+
if (c.id !== rec.id && c.tenantId === rec.tenantId && c.googleSub === rec.googleSub) {
|
|
554
|
+
throw new Error(
|
|
555
|
+
"unique constraint violation: (tenantId, googleSub) already exists"
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
const existing = this.connections.get(rec.id);
|
|
560
|
+
if (existing && existing.tenantId !== rec.tenantId) {
|
|
561
|
+
throw new Error("connection id belongs to a different tenant");
|
|
562
|
+
}
|
|
563
|
+
this.connections.set(rec.id, clone(rec));
|
|
564
|
+
}
|
|
565
|
+
async updateConnectionStatus(tenantId, connectionId, status, reason) {
|
|
566
|
+
const c = this.connections.get(connectionId);
|
|
567
|
+
if (c && c.tenantId === tenantId) {
|
|
568
|
+
c.status = status;
|
|
569
|
+
c.statusReason = reason;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
async touchConnection(tenantId, connectionId, at) {
|
|
573
|
+
const c = this.connections.get(connectionId);
|
|
574
|
+
if (c && c.tenantId === tenantId) c.lastUsedAt = new Date(at.getTime());
|
|
575
|
+
}
|
|
576
|
+
async setConnectionDisplayName(tenantId, connectionId, displayName, at) {
|
|
577
|
+
const c = this.connections.get(connectionId);
|
|
578
|
+
if (!c || c.tenantId !== tenantId) return false;
|
|
579
|
+
c.displayName = displayName;
|
|
580
|
+
c.updatedAt = new Date(at.getTime());
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
async disconnectConnection(tenantId, connectionId, at) {
|
|
584
|
+
const c = this.connections.get(connectionId);
|
|
585
|
+
if (!c || c.tenantId !== tenantId) return false;
|
|
586
|
+
c.refreshToken = null;
|
|
587
|
+
c.status = "revoked";
|
|
588
|
+
c.statusReason = "disconnected";
|
|
589
|
+
c.updatedAt = new Date(at.getTime());
|
|
590
|
+
return true;
|
|
591
|
+
}
|
|
592
|
+
async insertOAuthState(rec) {
|
|
593
|
+
const copy = clone(rec);
|
|
594
|
+
copy.stateHash = toBytes(copy.stateHash);
|
|
595
|
+
this.states.set(toHex(copy.stateHash), copy);
|
|
596
|
+
}
|
|
597
|
+
async consumeOAuthState(stateHash, now) {
|
|
598
|
+
const key = stateHash.toLowerCase();
|
|
599
|
+
const s = this.states.get(key);
|
|
600
|
+
if (!s || s.expiresAt.getTime() <= now.getTime()) return null;
|
|
601
|
+
this.states.delete(key);
|
|
602
|
+
const out = clone(s);
|
|
603
|
+
out.stateHash = toBytes(out.stateHash);
|
|
604
|
+
return out;
|
|
605
|
+
}
|
|
606
|
+
async deleteExpiredOAuthStates(now) {
|
|
607
|
+
let n = 0;
|
|
608
|
+
for (const [k, s] of this.states) {
|
|
609
|
+
if (s.expiresAt.getTime() <= now.getTime()) {
|
|
610
|
+
this.states.delete(k);
|
|
611
|
+
n++;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return n;
|
|
615
|
+
}
|
|
616
|
+
async *iterateEncryptedValues(_batchSize) {
|
|
617
|
+
const refs = [];
|
|
618
|
+
for (const c of this.clients.values()) {
|
|
619
|
+
refs.push({
|
|
620
|
+
tenantId: c.tenantId,
|
|
621
|
+
record: "oauth_client",
|
|
622
|
+
recordId: c.tenantId,
|
|
623
|
+
field: "client_secret",
|
|
624
|
+
value: clone(c.clientSecret)
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
for (const c of this.connections.values()) {
|
|
628
|
+
if (c.refreshToken) {
|
|
629
|
+
refs.push({
|
|
630
|
+
tenantId: c.tenantId,
|
|
631
|
+
record: "connection",
|
|
632
|
+
recordId: c.id,
|
|
633
|
+
field: "refresh_token",
|
|
634
|
+
value: clone(c.refreshToken)
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
for (const [hex2, s] of this.states) {
|
|
639
|
+
refs.push({
|
|
640
|
+
tenantId: s.tenantId,
|
|
641
|
+
record: "oauth_state",
|
|
642
|
+
recordId: hex2,
|
|
643
|
+
field: "code_verifier",
|
|
644
|
+
value: clone(s.codeVerifier)
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
for (const r of refs) yield r;
|
|
648
|
+
}
|
|
649
|
+
async replaceEncryptedValue(ref, value) {
|
|
650
|
+
const v = clone(value);
|
|
651
|
+
const same = (a) => a != null && JSON.stringify(a) === JSON.stringify(ref.value);
|
|
652
|
+
if (ref.record === "oauth_client" && ref.field === "client_secret") {
|
|
653
|
+
const c = this.clients.get(ref.recordId);
|
|
654
|
+
if (c && c.tenantId === ref.tenantId && same(c.clientSecret)) {
|
|
655
|
+
c.clientSecret = v;
|
|
656
|
+
return true;
|
|
657
|
+
}
|
|
658
|
+
return false;
|
|
659
|
+
} else if (ref.record === "connection" && ref.field === "refresh_token") {
|
|
660
|
+
const c = this.connections.get(ref.recordId);
|
|
661
|
+
if (c && c.tenantId === ref.tenantId && same(c.refreshToken)) {
|
|
662
|
+
c.refreshToken = v;
|
|
663
|
+
return true;
|
|
664
|
+
}
|
|
665
|
+
return false;
|
|
666
|
+
} else if (ref.record === "oauth_state" && ref.field === "code_verifier") {
|
|
667
|
+
const s = this.states.get(ref.recordId.toLowerCase());
|
|
668
|
+
if (s && s.tenantId === ref.tenantId && same(s.codeVerifier)) {
|
|
669
|
+
s.codeVerifier = v;
|
|
670
|
+
return true;
|
|
671
|
+
}
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
throw new Error(`unknown encrypted field ${ref.record}/${ref.field}`);
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// src/testing/fake-google-oauth.ts
|
|
679
|
+
var USERINFO_EMAIL = "https://www.googleapis.com/auth/userinfo.email";
|
|
680
|
+
var ACCESS_TTL_MS = 36e5;
|
|
681
|
+
var FakeGoogleOAuth = class {
|
|
682
|
+
revoked = [];
|
|
683
|
+
calls = [];
|
|
684
|
+
now;
|
|
685
|
+
n = 0;
|
|
686
|
+
revokeFails = false;
|
|
687
|
+
codes = /* @__PURE__ */ new Map();
|
|
688
|
+
idTokens = /* @__PURE__ */ new Map();
|
|
689
|
+
refreshTokens = /* @__PURE__ */ new Map();
|
|
690
|
+
// token -> clientId
|
|
691
|
+
failing = /* @__PURE__ */ new Set();
|
|
692
|
+
constructor(opts = {}) {
|
|
693
|
+
this.now = opts.now ?? (() => /* @__PURE__ */ new Date());
|
|
694
|
+
}
|
|
695
|
+
consent(authUrl, account) {
|
|
696
|
+
const parsed = new URL(authUrl);
|
|
697
|
+
if (parsed.origin + parsed.pathname !== AUTH_ENDPOINT)
|
|
698
|
+
throw new Error(`Auth URL must target the endpoint ${AUTH_ENDPOINT}`);
|
|
699
|
+
const q = parsed.searchParams;
|
|
700
|
+
for (const [k, v] of [
|
|
701
|
+
["response_type", "code"],
|
|
702
|
+
["code_challenge_method", "S256"],
|
|
703
|
+
["access_type", "offline"]
|
|
704
|
+
]) {
|
|
705
|
+
if (q.get(k) !== v)
|
|
706
|
+
throw new Error(`Auth URL must include ${k}=${v} (got ${q.get(k)})`);
|
|
707
|
+
}
|
|
708
|
+
const requested = (q.get("scope") ?? "").split(" ").filter(Boolean);
|
|
709
|
+
const scopes = account.grantScopes ?? requested.map((s) => s === "email" ? USERINFO_EMAIL : s);
|
|
710
|
+
const code = `fake-code-${++this.n}`;
|
|
711
|
+
this.codes.set(code, {
|
|
712
|
+
clientId: q.get("client_id") ?? "",
|
|
713
|
+
redirectUri: q.get("redirect_uri") ?? "",
|
|
714
|
+
challenge: q.get("code_challenge") ?? "",
|
|
715
|
+
scopes,
|
|
716
|
+
account,
|
|
717
|
+
used: false
|
|
718
|
+
});
|
|
719
|
+
return { code, state: q.get("state") ?? "" };
|
|
720
|
+
}
|
|
721
|
+
async exchangeCode(p) {
|
|
722
|
+
this.calls.push({ method: "exchangeCode" });
|
|
723
|
+
const c = this.codes.get(p.code);
|
|
724
|
+
if (!c) throw new Error("invalid_grant: unknown code");
|
|
725
|
+
if (c.used) throw new Error("invalid_grant: code already used");
|
|
726
|
+
c.used = true;
|
|
727
|
+
if (c.clientId !== p.clientId) throw new Error("invalid_client");
|
|
728
|
+
if (c.redirectUri !== p.redirectUri)
|
|
729
|
+
throw new Error("redirect_uri_mismatch");
|
|
730
|
+
if (codeChallenge(p.codeVerifier) !== c.challenge)
|
|
731
|
+
throw new Error("invalid_grant: PKCE verifier mismatch");
|
|
732
|
+
const id = ++this.n;
|
|
733
|
+
const idToken = `fake-id.${id}`;
|
|
734
|
+
this.idTokens.set(idToken, { clientId: c.clientId, account: c.account });
|
|
735
|
+
let refreshToken = null;
|
|
736
|
+
if (!c.account.omitRefreshToken) {
|
|
737
|
+
refreshToken = `1//fake-refresh-${id}`;
|
|
738
|
+
this.refreshTokens.set(refreshToken, c.clientId);
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
refreshToken,
|
|
742
|
+
accessToken: `ya29.fake-access-${id}`,
|
|
743
|
+
expiresAt: new Date(this.now().getTime() + ACCESS_TTL_MS),
|
|
744
|
+
scopes: [...c.scopes],
|
|
745
|
+
idToken
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
async verifyIdToken(p) {
|
|
749
|
+
this.calls.push({ method: "verifyIdToken" });
|
|
750
|
+
const t = this.idTokens.get(p.idToken);
|
|
751
|
+
if (!t || t.clientId !== p.clientId) throw new Error("invalid id token");
|
|
752
|
+
return {
|
|
753
|
+
sub: t.account.sub,
|
|
754
|
+
email: t.account.email,
|
|
755
|
+
emailVerified: t.account.emailVerified ?? true,
|
|
756
|
+
hd: t.account.hd ?? null
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
async refreshAccessToken(p) {
|
|
760
|
+
this.calls.push({ method: "refreshAccessToken" });
|
|
761
|
+
const owner = this.refreshTokens.get(p.refreshToken);
|
|
762
|
+
if (owner === void 0 || owner !== p.clientId || this.failing.has(p.refreshToken) || this.revoked.includes(p.refreshToken)) {
|
|
763
|
+
throw new InvalidGrantError();
|
|
764
|
+
}
|
|
765
|
+
return {
|
|
766
|
+
accessToken: `ya29.fake-access-${++this.n}`,
|
|
767
|
+
expiresAt: new Date(this.now().getTime() + ACCESS_TTL_MS)
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
async revokeToken(token) {
|
|
771
|
+
this.calls.push({ method: "revokeToken" });
|
|
772
|
+
if (this.revokeFails) throw new Error("revoke failed");
|
|
773
|
+
this.revoked.push(token);
|
|
774
|
+
}
|
|
775
|
+
failRefresh(refreshToken, _error) {
|
|
776
|
+
this.failing.add(refreshToken);
|
|
777
|
+
}
|
|
778
|
+
failRevoke(on) {
|
|
779
|
+
this.revokeFails = on;
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
// src/testing/capture-transport.ts
|
|
784
|
+
var addresses = (a) => (a === void 0 ? [] : Array.isArray(a) ? a : [a]).flatMap((o) => o.value).flatMap((v) => v.address ? [v.address] : []);
|
|
785
|
+
var CaptureTransport = class {
|
|
786
|
+
messages = [];
|
|
787
|
+
n = 0;
|
|
788
|
+
async send(p) {
|
|
789
|
+
const mod = await loadPeer(
|
|
790
|
+
"mailparser"
|
|
791
|
+
);
|
|
792
|
+
const { simpleParser } = mod.simpleParser ? mod : mod.default;
|
|
793
|
+
const parsed = await simpleParser(p.raw);
|
|
794
|
+
const id = `capture-${++this.n}`;
|
|
795
|
+
this.messages.push({
|
|
796
|
+
raw: p.raw,
|
|
797
|
+
threadId: p.threadId,
|
|
798
|
+
parsed: {
|
|
799
|
+
from: addresses(parsed.from)[0] ?? "",
|
|
800
|
+
to: addresses(parsed.to),
|
|
801
|
+
cc: addresses(parsed.cc),
|
|
802
|
+
bcc: addresses(parsed.bcc),
|
|
803
|
+
subject: parsed.subject ?? "",
|
|
804
|
+
html: parsed.html === false || parsed.html === void 0 ? null : parsed.html,
|
|
805
|
+
text: parsed.text ?? null,
|
|
806
|
+
attachments: parsed.attachments.map((a) => ({
|
|
807
|
+
filename: a.filename ?? "",
|
|
808
|
+
contentType: a.contentType,
|
|
809
|
+
size: a.size
|
|
810
|
+
}))
|
|
811
|
+
}
|
|
812
|
+
});
|
|
813
|
+
return { messageId: id, threadId: p.threadId ?? id };
|
|
814
|
+
}
|
|
815
|
+
clear() {
|
|
816
|
+
this.messages.length = 0;
|
|
817
|
+
}
|
|
818
|
+
find(pred) {
|
|
819
|
+
return this.messages.find(pred);
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
export {
|
|
823
|
+
CaptureTransport,
|
|
824
|
+
FakeGoogleOAuth,
|
|
825
|
+
InMemoryStore,
|
|
826
|
+
LocalKeyProvider,
|
|
827
|
+
runStoreContractTests
|
|
828
|
+
};
|
|
829
|
+
//# sourceMappingURL=testing.js.map
|