@frak-labs/core-sdk 0.0.19-beta.f259d7fc → 0.1.0-beta.263acd1e
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/cdn/bundle.iife.js +14 -0
- package/dist/actions-CEEObPYc.js +1 -0
- package/dist/actions-DbQhWYx8.cjs +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -1400
- package/dist/actions.d.ts +3 -1400
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -13
- package/dist/bundle.d.cts +6 -1927
- package/dist/bundle.d.ts +6 -1927
- package/dist/bundle.js +1 -13
- package/dist/index-7OZ39x1U.d.ts +195 -0
- package/dist/index-C6FxkWPC.d.cts +511 -0
- package/dist/index-UFX7xCg3.d.ts +351 -0
- package/dist/index-d8xS4ryI.d.ts +511 -0
- package/dist/index-p4FqSp8z.d.cts +351 -0
- package/dist/index-zDq-VlKx.d.cts +195 -0
- package/dist/index.cjs +1 -13
- package/dist/index.d.cts +4 -1269
- package/dist/index.d.ts +4 -1269
- package/dist/index.js +1 -13
- package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
- package/dist/interaction-KX1h9a7V.d.ts +45 -0
- package/dist/interactions-DnfM3oe0.js +1 -0
- package/dist/interactions-EIXhNLf6.cjs +1 -0
- package/dist/interactions.cjs +1 -1
- package/dist/interactions.d.cts +2 -182
- package/dist/interactions.d.ts +2 -182
- package/dist/interactions.js +1 -1
- package/dist/openSso-D--Airj6.d.cts +1018 -0
- package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
- package/dist/productTypes-BUkXJKZ7.cjs +1 -0
- package/dist/productTypes-CGb1MmBF.js +1 -0
- package/dist/src-B_xO0AR6.cjs +13 -0
- package/dist/src-D2d52OZa.js +13 -0
- package/dist/trackEvent-CHnYa85W.js +1 -0
- package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
- package/package.json +24 -19
- package/src/actions/displayEmbeddedWallet.test.ts +194 -0
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.test.ts +387 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.test.ts +133 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.test.ts +407 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.test.ts +223 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.test.ts +219 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.test.ts +287 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.test.ts +372 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.ts +94 -0
- package/src/bundle.ts +3 -0
- package/src/clients/DebugInfo.ts +182 -0
- package/src/clients/createIFrameFrakClient.ts +287 -0
- package/src/clients/index.ts +3 -0
- package/src/clients/setupClient.test.ts +343 -0
- package/src/clients/setupClient.ts +73 -0
- package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
- package/src/clients/transports/iframeLifecycleManager.ts +90 -0
- package/src/constants/interactionTypes.ts +44 -0
- package/src/constants/locales.ts +14 -0
- package/src/constants/productTypes.ts +33 -0
- package/src/index.ts +101 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.test.ts +215 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.test.ts +291 -0
- package/src/interactions/purchaseEncoder.ts +99 -0
- package/src/interactions/referralEncoder.test.ts +170 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.test.ts +107 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.test.ts +56 -0
- package/src/interactions/webshopEncoder.ts +30 -0
- package/src/types/client.ts +14 -0
- package/src/types/compression.ts +22 -0
- package/src/types/config.ts +111 -0
- package/src/types/context.ts +13 -0
- package/src/types/index.ts +71 -0
- package/src/types/lifecycle/client.ts +46 -0
- package/src/types/lifecycle/iframe.ts +35 -0
- package/src/types/lifecycle/index.ts +2 -0
- package/src/types/rpc/displayModal.ts +84 -0
- package/src/types/rpc/embedded/index.ts +68 -0
- package/src/types/rpc/embedded/loggedIn.ts +55 -0
- package/src/types/rpc/embedded/loggedOut.ts +28 -0
- package/src/types/rpc/interaction.ts +43 -0
- package/src/types/rpc/modal/final.ts +46 -0
- package/src/types/rpc/modal/generic.ts +46 -0
- package/src/types/rpc/modal/index.ts +20 -0
- package/src/types/rpc/modal/login.ts +32 -0
- package/src/types/rpc/modal/openSession.ts +25 -0
- package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
- package/src/types/rpc/modal/transaction.ts +33 -0
- package/src/types/rpc/productInformation.ts +59 -0
- package/src/types/rpc/sso.ts +80 -0
- package/src/types/rpc/walletStatus.ts +35 -0
- package/src/types/rpc.ts +158 -0
- package/src/types/transport.ts +34 -0
- package/src/utils/FrakContext.test.ts +407 -0
- package/src/utils/FrakContext.ts +158 -0
- package/src/utils/compression/b64.test.ts +181 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.test.ts +123 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.test.ts +145 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.test.ts +80 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.test.ts +23 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.test.ts +113 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.test.ts +44 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.test.ts +51 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.test.ts +64 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.test.ts +450 -0
- package/src/utils/iframeHelper.ts +143 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.test.ts +361 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.test.ts +162 -0
- package/src/utils/trackEvent.ts +26 -0
- package/cdn/bundle.js +0 -19
- package/cdn/bundle.js.LICENSE.txt +0 -10
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for openSso action
|
|
3
|
+
* Tests SSO flows in both redirect and popup modes
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { vi } from "vitest";
|
|
7
|
+
|
|
8
|
+
// Mock utilities before imports
|
|
9
|
+
vi.mock("../utils/sso", () => ({
|
|
10
|
+
generateSsoUrl: vi.fn((walletUrl, _args, productId, name, _css) => {
|
|
11
|
+
return `${walletUrl}/sso?name=${name}&productId=${productId}`;
|
|
12
|
+
}),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
vi.mock("../utils/computeProductId", () => ({
|
|
16
|
+
computeProductId: vi.fn(
|
|
17
|
+
() =>
|
|
18
|
+
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
|
|
19
|
+
),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
afterEach,
|
|
24
|
+
beforeEach,
|
|
25
|
+
describe,
|
|
26
|
+
expect,
|
|
27
|
+
it,
|
|
28
|
+
} from "../../tests/vitest-fixtures";
|
|
29
|
+
import type {
|
|
30
|
+
FrakClient,
|
|
31
|
+
OpenSsoParamsType,
|
|
32
|
+
OpenSsoReturnType,
|
|
33
|
+
} from "../types";
|
|
34
|
+
import { openSso, ssoPopupFeatures, ssoPopupName } from "./openSso";
|
|
35
|
+
|
|
36
|
+
describe("openSso", () => {
|
|
37
|
+
describe("constants", () => {
|
|
38
|
+
it("should have correct popup features", () => {
|
|
39
|
+
expect(ssoPopupFeatures).toBe(
|
|
40
|
+
"menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800"
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should have correct popup name", () => {
|
|
45
|
+
expect(ssoPopupName).toBe("frak-sso");
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("redirect mode", () => {
|
|
50
|
+
it("should use redirect mode when openInSameWindow is true", async () => {
|
|
51
|
+
const mockResponse: OpenSsoReturnType = {
|
|
52
|
+
wallet: "0x1234567890123456789012345678901234567890",
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const mockClient = {
|
|
56
|
+
config: {
|
|
57
|
+
metadata: { name: "Test App" },
|
|
58
|
+
},
|
|
59
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
60
|
+
} as unknown as FrakClient;
|
|
61
|
+
|
|
62
|
+
const params: OpenSsoParamsType = {
|
|
63
|
+
openInSameWindow: true,
|
|
64
|
+
metadata: {
|
|
65
|
+
logoUrl: "https://example.com/logo.png",
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const result = await openSso(mockClient, params);
|
|
70
|
+
|
|
71
|
+
expect(mockClient.request).toHaveBeenCalledWith({
|
|
72
|
+
method: "frak_openSso",
|
|
73
|
+
params: [params, "Test App", undefined],
|
|
74
|
+
});
|
|
75
|
+
expect(result).toEqual(mockResponse);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should use redirect mode when redirectUrl is provided", async () => {
|
|
79
|
+
const mockResponse: OpenSsoReturnType = {
|
|
80
|
+
wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd",
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const mockClient = {
|
|
84
|
+
config: {
|
|
85
|
+
metadata: { name: "Test App" },
|
|
86
|
+
},
|
|
87
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
88
|
+
} as unknown as FrakClient;
|
|
89
|
+
|
|
90
|
+
const params: OpenSsoParamsType = {
|
|
91
|
+
redirectUrl: "https://example.com/callback",
|
|
92
|
+
metadata: {
|
|
93
|
+
logoUrl: "https://example.com/logo.png",
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const result = await openSso(mockClient, params);
|
|
98
|
+
|
|
99
|
+
expect(mockClient.request).toHaveBeenCalledWith({
|
|
100
|
+
method: "frak_openSso",
|
|
101
|
+
params: [params, "Test App", undefined],
|
|
102
|
+
});
|
|
103
|
+
expect(result).toEqual(mockResponse);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should pass custom CSS in redirect mode", async () => {
|
|
107
|
+
const mockResponse: OpenSsoReturnType = {};
|
|
108
|
+
|
|
109
|
+
const mockClient = {
|
|
110
|
+
config: {
|
|
111
|
+
metadata: { name: "Test App" },
|
|
112
|
+
customizations: {
|
|
113
|
+
css: ":root { --primary: blue; }",
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
117
|
+
} as unknown as FrakClient;
|
|
118
|
+
|
|
119
|
+
const params: OpenSsoParamsType = {
|
|
120
|
+
openInSameWindow: true,
|
|
121
|
+
metadata: {
|
|
122
|
+
logoUrl: "https://example.com/logo.png",
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
await openSso(mockClient, params);
|
|
127
|
+
|
|
128
|
+
expect(mockClient.request).toHaveBeenCalledWith({
|
|
129
|
+
method: "frak_openSso",
|
|
130
|
+
params: [params, "Test App", ":root { --primary: blue; }"],
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("popup mode", () => {
|
|
136
|
+
let windowOpenSpy: any;
|
|
137
|
+
let mockPopup: {
|
|
138
|
+
focus: ReturnType<typeof vi.fn>;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
beforeEach(() => {
|
|
142
|
+
mockPopup = {
|
|
143
|
+
focus: vi.fn(),
|
|
144
|
+
};
|
|
145
|
+
windowOpenSpy = vi
|
|
146
|
+
.spyOn(window, "open")
|
|
147
|
+
.mockReturnValue(mockPopup as unknown as Window);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
afterEach(() => {
|
|
151
|
+
windowOpenSpy.mockRestore();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("should open popup with generated URL", async () => {
|
|
155
|
+
const mockResponse: OpenSsoReturnType = {
|
|
156
|
+
wallet: "0x1234567890123456789012345678901234567890",
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const mockClient = {
|
|
160
|
+
config: {
|
|
161
|
+
metadata: { name: "Test App" },
|
|
162
|
+
walletUrl: "https://wallet.frak.id",
|
|
163
|
+
},
|
|
164
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
165
|
+
} as unknown as FrakClient;
|
|
166
|
+
|
|
167
|
+
const params: OpenSsoParamsType = {
|
|
168
|
+
metadata: {
|
|
169
|
+
logoUrl: "https://example.com/logo.png",
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
await openSso(mockClient, params);
|
|
174
|
+
|
|
175
|
+
expect(window.open).toHaveBeenCalledWith(
|
|
176
|
+
expect.stringContaining("https://wallet.frak.id/sso"),
|
|
177
|
+
"frak-sso",
|
|
178
|
+
"menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800"
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should use custom ssoPopupUrl when provided", async () => {
|
|
183
|
+
const mockResponse: OpenSsoReturnType = {};
|
|
184
|
+
|
|
185
|
+
const mockClient = {
|
|
186
|
+
config: {
|
|
187
|
+
metadata: { name: "Test App" },
|
|
188
|
+
},
|
|
189
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
190
|
+
} as unknown as FrakClient;
|
|
191
|
+
|
|
192
|
+
const params: OpenSsoParamsType = {
|
|
193
|
+
ssoPopupUrl: "https://custom-wallet.com/sso?custom=param",
|
|
194
|
+
metadata: {
|
|
195
|
+
logoUrl: "https://example.com/logo.png",
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
await openSso(mockClient, params);
|
|
200
|
+
|
|
201
|
+
expect(window.open).toHaveBeenCalledWith(
|
|
202
|
+
"https://custom-wallet.com/sso?custom=param",
|
|
203
|
+
"frak-sso",
|
|
204
|
+
"menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800"
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("should focus popup after opening", async () => {
|
|
209
|
+
const mockResponse: OpenSsoReturnType = {};
|
|
210
|
+
|
|
211
|
+
const mockClient = {
|
|
212
|
+
config: {
|
|
213
|
+
metadata: { name: "Test App" },
|
|
214
|
+
},
|
|
215
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
216
|
+
} as unknown as FrakClient;
|
|
217
|
+
|
|
218
|
+
const params: OpenSsoParamsType = {
|
|
219
|
+
metadata: {
|
|
220
|
+
logoUrl: "https://example.com/logo.png",
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
await openSso(mockClient, params);
|
|
225
|
+
|
|
226
|
+
expect(mockPopup.focus).toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("should wait for SSO completion via client.request", async () => {
|
|
230
|
+
const mockResponse: OpenSsoReturnType = {
|
|
231
|
+
wallet: "0x1234567890123456789012345678901234567890",
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const mockClient = {
|
|
235
|
+
config: {
|
|
236
|
+
metadata: { name: "Test App" },
|
|
237
|
+
},
|
|
238
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
239
|
+
} as unknown as FrakClient;
|
|
240
|
+
|
|
241
|
+
const params: OpenSsoParamsType = {
|
|
242
|
+
metadata: {
|
|
243
|
+
logoUrl: "https://example.com/logo.png",
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const result = await openSso(mockClient, params);
|
|
248
|
+
|
|
249
|
+
expect(mockClient.request).toHaveBeenCalledWith({
|
|
250
|
+
method: "frak_openSso",
|
|
251
|
+
params: [params, "Test App", undefined],
|
|
252
|
+
});
|
|
253
|
+
expect(result).toEqual(mockResponse);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("should return empty object when result is null", async () => {
|
|
257
|
+
const mockClient = {
|
|
258
|
+
config: {
|
|
259
|
+
metadata: { name: "Test App" },
|
|
260
|
+
},
|
|
261
|
+
request: vi.fn().mockResolvedValue(null),
|
|
262
|
+
} as unknown as FrakClient;
|
|
263
|
+
|
|
264
|
+
const params: OpenSsoParamsType = {
|
|
265
|
+
metadata: {
|
|
266
|
+
logoUrl: "https://example.com/logo.png",
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const result = await openSso(mockClient, params);
|
|
271
|
+
|
|
272
|
+
expect(result).toEqual({});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("should use default wallet URL when not configured", async () => {
|
|
276
|
+
const mockResponse: OpenSsoReturnType = {};
|
|
277
|
+
|
|
278
|
+
const mockClient = {
|
|
279
|
+
config: {
|
|
280
|
+
metadata: { name: "Test App" },
|
|
281
|
+
},
|
|
282
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
283
|
+
} as unknown as FrakClient;
|
|
284
|
+
|
|
285
|
+
const params: OpenSsoParamsType = {
|
|
286
|
+
metadata: {
|
|
287
|
+
logoUrl: "https://example.com/logo.png",
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
await openSso(mockClient, params);
|
|
292
|
+
|
|
293
|
+
expect(window.open).toHaveBeenCalledWith(
|
|
294
|
+
expect.stringContaining("https://wallet.frak.id/sso"),
|
|
295
|
+
expect.any(String),
|
|
296
|
+
expect.any(String)
|
|
297
|
+
);
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe("popup blocker", () => {
|
|
302
|
+
it("should throw error when popup is blocked", async () => {
|
|
303
|
+
vi.spyOn(window, "open").mockReturnValue(null);
|
|
304
|
+
|
|
305
|
+
const mockClient = {
|
|
306
|
+
config: {
|
|
307
|
+
metadata: { name: "Test App" },
|
|
308
|
+
},
|
|
309
|
+
request: vi.fn(),
|
|
310
|
+
} as unknown as FrakClient;
|
|
311
|
+
|
|
312
|
+
const params: OpenSsoParamsType = {
|
|
313
|
+
metadata: {
|
|
314
|
+
logoUrl: "https://example.com/logo.png",
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
await expect(openSso(mockClient, params)).rejects.toThrow(
|
|
319
|
+
"Popup was blocked. Please allow popups for this site."
|
|
320
|
+
);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("should not call client.request when popup is blocked", async () => {
|
|
324
|
+
vi.spyOn(window, "open").mockReturnValue(null);
|
|
325
|
+
|
|
326
|
+
const mockClient = {
|
|
327
|
+
config: {
|
|
328
|
+
metadata: { name: "Test App" },
|
|
329
|
+
},
|
|
330
|
+
request: vi.fn(),
|
|
331
|
+
} as unknown as FrakClient;
|
|
332
|
+
|
|
333
|
+
const params: OpenSsoParamsType = {
|
|
334
|
+
metadata: {
|
|
335
|
+
logoUrl: "https://example.com/logo.png",
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
try {
|
|
340
|
+
await openSso(mockClient, params);
|
|
341
|
+
} catch {
|
|
342
|
+
// Expected error
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
expect(mockClient.request).not.toHaveBeenCalled();
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe("mode detection", () => {
|
|
350
|
+
it("should prefer openInSameWindow over redirectUrl", async () => {
|
|
351
|
+
const mockResponse: OpenSsoReturnType = {};
|
|
352
|
+
|
|
353
|
+
const mockClient = {
|
|
354
|
+
config: {
|
|
355
|
+
metadata: { name: "Test App" },
|
|
356
|
+
},
|
|
357
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
358
|
+
} as unknown as FrakClient;
|
|
359
|
+
|
|
360
|
+
const params: OpenSsoParamsType = {
|
|
361
|
+
openInSameWindow: false,
|
|
362
|
+
redirectUrl: "https://example.com/callback",
|
|
363
|
+
metadata: {
|
|
364
|
+
logoUrl: "https://example.com/logo.png",
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const windowOpenSpy = vi.spyOn(window, "open").mockReturnValue({
|
|
369
|
+
focus: vi.fn(),
|
|
370
|
+
} as unknown as Window);
|
|
371
|
+
|
|
372
|
+
await openSso(mockClient, params);
|
|
373
|
+
|
|
374
|
+
// Should use popup mode because openInSameWindow=false
|
|
375
|
+
expect(window.open).toHaveBeenCalled();
|
|
376
|
+
|
|
377
|
+
windowOpenSpy.mockRestore();
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("should use popup mode when neither flag is set", async () => {
|
|
381
|
+
const mockResponse: OpenSsoReturnType = {};
|
|
382
|
+
|
|
383
|
+
const mockClient = {
|
|
384
|
+
config: {
|
|
385
|
+
metadata: { name: "Test App" },
|
|
386
|
+
},
|
|
387
|
+
request: vi.fn().mockResolvedValue(mockResponse),
|
|
388
|
+
} as unknown as FrakClient;
|
|
389
|
+
|
|
390
|
+
const params: OpenSsoParamsType = {
|
|
391
|
+
metadata: {
|
|
392
|
+
logoUrl: "https://example.com/logo.png",
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
const windowOpenSpy = vi.spyOn(window, "open").mockReturnValue({
|
|
397
|
+
focus: vi.fn(),
|
|
398
|
+
} as unknown as Window);
|
|
399
|
+
|
|
400
|
+
await openSso(mockClient, params);
|
|
401
|
+
|
|
402
|
+
expect(window.open).toHaveBeenCalled();
|
|
403
|
+
|
|
404
|
+
windowOpenSpy.mockRestore();
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FrakClient,
|
|
3
|
+
OpenSsoParamsType,
|
|
4
|
+
OpenSsoReturnType,
|
|
5
|
+
} from "../types";
|
|
6
|
+
import { computeProductId } from "../utils/computeProductId";
|
|
7
|
+
import { generateSsoUrl } from "../utils/sso";
|
|
8
|
+
|
|
9
|
+
// SSO popup configuration
|
|
10
|
+
export const ssoPopupFeatures =
|
|
11
|
+
"menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800";
|
|
12
|
+
export const ssoPopupName = "frak-sso";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Function used to open the SSO
|
|
16
|
+
* @param client - The current Frak Client
|
|
17
|
+
* @param args - The SSO parameters
|
|
18
|
+
*
|
|
19
|
+
* @description Two SSO flow modes:
|
|
20
|
+
*
|
|
21
|
+
* **Redirect Mode** (openInSameWindow: true):
|
|
22
|
+
* - Wallet generates URL and triggers redirect
|
|
23
|
+
* - Used when redirectUrl is provided
|
|
24
|
+
*
|
|
25
|
+
* **Popup Mode** (openInSameWindow: false/omitted):
|
|
26
|
+
* - SDK generates URL client-side (or uses provided ssoPopupUrl)
|
|
27
|
+
* - Opens popup synchronously (prevents popup blockers)
|
|
28
|
+
* - Waits for SSO completion via postMessage
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* First we build the sso metadata
|
|
32
|
+
* ```ts
|
|
33
|
+
* // Build the metadata
|
|
34
|
+
* const metadata: SsoMetadata = {
|
|
35
|
+
* logoUrl: "https://my-app.com/logo.png",
|
|
36
|
+
* homepageLink: "https://my-app.com",
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Then, either use it with direct exit (and so user is directly redirected to your website), or a custom redirect URL
|
|
41
|
+
* :::code-group
|
|
42
|
+
* ```ts [Popup (default)]
|
|
43
|
+
* // Opens in popup, SDK generates URL automatically
|
|
44
|
+
* await openSso(frakConfig, {
|
|
45
|
+
* directExit: true,
|
|
46
|
+
* metadata,
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
* ```ts [Redirect]
|
|
50
|
+
* // Opens in same window with redirect
|
|
51
|
+
* await openSso(frakConfig, {
|
|
52
|
+
* redirectUrl: "https://my-app.com/frak-sso",
|
|
53
|
+
* metadata,
|
|
54
|
+
* openInSameWindow: true,
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
* ```ts [Custom popup URL]
|
|
58
|
+
* // Advanced: provide custom SSO URL
|
|
59
|
+
* const { ssoUrl } = await prepareSso(frakConfig, { metadata });
|
|
60
|
+
* await openSso(frakConfig, {
|
|
61
|
+
* metadata,
|
|
62
|
+
* ssoPopupUrl: `${ssoUrl}&custom=param`,
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
* :::
|
|
66
|
+
*/
|
|
67
|
+
export async function openSso(
|
|
68
|
+
client: FrakClient,
|
|
69
|
+
args: OpenSsoParamsType
|
|
70
|
+
): Promise<OpenSsoReturnType> {
|
|
71
|
+
const { metadata, customizations, walletUrl } = client.config;
|
|
72
|
+
|
|
73
|
+
// Check if redirect mode (default to true if redirectUrl present)
|
|
74
|
+
const isRedirectMode = args.openInSameWindow ?? !!args.redirectUrl;
|
|
75
|
+
|
|
76
|
+
if (isRedirectMode) {
|
|
77
|
+
// Redirect flow: Wallet generates URL and triggers redirect via lifecycle event
|
|
78
|
+
// This must happen on wallet side because only the iframe can trigger the redirect
|
|
79
|
+
return await client.request({
|
|
80
|
+
method: "frak_openSso",
|
|
81
|
+
params: [args, metadata.name, customizations?.css],
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Popup flow: Generate URL on SDK side and open synchronously
|
|
86
|
+
// This ensures window.open() is called in same tick as user gesture (no popup blocker)
|
|
87
|
+
|
|
88
|
+
// Step 1: Generate or use provided SSO URL
|
|
89
|
+
const ssoUrl =
|
|
90
|
+
args.ssoPopupUrl ??
|
|
91
|
+
generateSsoUrl(
|
|
92
|
+
walletUrl ?? "https://wallet.frak.id",
|
|
93
|
+
args,
|
|
94
|
+
computeProductId(),
|
|
95
|
+
metadata.name,
|
|
96
|
+
customizations?.css
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// Step 2: Open popup synchronously (critical for popup blocker prevention)
|
|
100
|
+
const popup = window.open(ssoUrl, ssoPopupName, ssoPopupFeatures);
|
|
101
|
+
if (!popup) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
"Popup was blocked. Please allow popups for this site."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
popup.focus();
|
|
107
|
+
|
|
108
|
+
// Step 3: Wait for SSO completion via RPC
|
|
109
|
+
// The wallet iframe will resolve this when SSO page sends sso_complete message
|
|
110
|
+
const result = await client.request({
|
|
111
|
+
method: "frak_openSso",
|
|
112
|
+
params: [args, metadata.name, customizations?.css],
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return result ?? {};
|
|
116
|
+
}
|