-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.tsx
More file actions
347 lines (327 loc) · 9.53 KB
/
dev.tsx
File metadata and controls
347 lines (327 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { type Factor } from "@slashid/slashid";
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom/client";
import { GDPRConsentDialog } from "./components/gdpr-consent-dialog";
import "./dev.css";
import { FactorConfiguration, Handle } from "./domain/types";
import { defaultOrganization } from "./middleware/default-organization";
import { Slot } from "./components/slot";
import { ConfigurationProvider } from "./context/config-context";
import { LoggedOut } from "./components/logged-out";
import { Form } from "./components/form";
import { LoggedIn } from "./components/logged-in";
import { OrganizationSwitcher } from "./components/organization-switcher";
import { SlashIDLoaded } from "./components/slashid-loaded";
import { useSlashID } from "./hooks/use-slash-id";
import { SlashIDProvider } from "./context/slash-id-context";
import { DynamicFlow } from "./components/dynamic-flow";
import { useOrganizations } from "./hooks/use-organizations";
const rootOid = "b6f94b67-d20f-7fc3-51df-bf6e3b82683e";
const initialFactors: Factor[] = [
{ method: "email_link" },
{ method: "otp_via_sms" },
{
method: "oidc",
options: {
provider: "google",
client_id: import.meta.env.VITE_GOOGLE_SSO_CLIENT_ID ?? "test_oidc",
},
},
];
const withWan: FactorConfiguration[] = [
{ method: "webauthn", options: { attachment: "platform" } },
{ method: "email_link" },
{ method: "otp_via_sms" },
{
method: "oidc",
options: {
provider: "google",
client_id: import.meta.env.VITE_GOOGLE_SSO_CLIENT_ID ?? "test_oidc_1",
},
},
{
method: "oidc",
label: "Google SSO - label test",
options: {
provider: "google",
client_id: "test_oidc_2",
},
},
];
function Config() {
const { currentOrganization } = useOrganizations();
const [factors, setFactors] = useState<Factor[]>(initialFactors);
useEffect(() => {
const checkPlatformAuthenticator = async () => {
try {
const hasPlatformAuthenticator =
await window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
if (hasPlatformAuthenticator) {
setFactors(withWan);
}
} catch (e) {
console.log(e);
}
};
checkPlatformAuthenticator();
}, []);
return (
<ConfigurationProvider factors={factors} storeLastHandle={true}>
{/* <div className="formWrapper">
<MultiFactorAuth
steps={[
{ factors: [{ method: "email_link" }] },
{
factors: [{ method: "otp_via_sms" }],
text: {
"initial.title": "TEST TITLE MFA",
},
},
]}
/>
</div> */}
<LoggedOut>
<div className="formWrapper">
<Form
middleware={[
defaultOrganization(({ organizations }) => {
const preferred = organizations.find(
(org) => org.org_name === "MyOrg/abc2"
);
if (preferred) return preferred.id;
return rootOid;
}),
]}
/>
</div>
</LoggedOut>
<LoggedIn>
<>
Current org: {currentOrganization?.org_name}
<OrganizationSwitcher
filter={(org) =>
org.org_name.includes("abc") || org.org_name === "MyOrg"
}
/>
</>
</LoggedIn>
<GDPRConsentDialog
forceConsent
necessaryCookiesRequired
defaultRejectAllLevels={["none", "necessary"]}
className="gdprConsentDialogClass"
triggerClassName="gdprConsentDialogTriggerClass"
onSuccess={(consents) => console.log("onSuccess - consents:", consents)}
onError={(error) => console.error(error)}
/>
</ConfigurationProvider>
);
}
const getFactor = (handle?: Handle) => {
if (!handle || handle.type !== "email_address") {
throw new Error("Only use email for demo!");
}
const email = handle?.value;
const domain = email.split("@")[1];
if (domain === "slashid.dev") {
const oidcFactor: Factor = {
method: "oidc",
options: {
provider: "google",
client_id: import.meta.env.VITE_GOOGLE_SSO_CLIENT_ID ?? "test_oidc",
},
};
return oidcFactor;
} else {
const emailLinkFactor: Factor = { method: "email_link" };
return emailLinkFactor;
}
};
const ConfiguredDynamicFlow = () => {
return (
<ConfigurationProvider text={{ "initial.oidc": "Continue with" }}>
<DynamicFlow className="formWrapper" getFactor={getFactor} />
</ConfigurationProvider>
);
};
const BasicForm = () => {
return (
<ConfigurationProvider
factors={[
{ method: "webauthn" },
{ method: "email_link" },
{ method: "otp_via_email" },
{ method: "otp_via_sms" },
{ method: "password" },
{
method: "oidc",
options: {
provider: "okta",
client_id: import.meta.env.VITE_GOOGLE_SSO_CLIENT_ID ?? "test_oidc",
},
},
{
method: "oidc",
options: {
provider: "apple",
client_id: "test_apple_id",
},
},
{
method: "saml",
options: {
provider_credentials_id: "test_saml",
},
label: "SAML test",
logo: "https://www.oasis-open.org/committees/download.php/29723/draft-saml-logo-03.png",
},
]}
>
<SlashIDLoaded>
<>
<LoggedIn>Logged in!</LoggedIn>
<LoggedOut>
<Form
onError={(error, context) =>
console.log("onError", { error, context })
}
/>
</LoggedOut>
</>
</SlashIDLoaded>
</ConfigurationProvider>
);
};
const ComposedForm = () => {
return (
<ConfigurationProvider
factors={[
{ method: "webauthn" },
{ method: "email_link" },
{ method: "otp_via_email" },
{ method: "otp_via_sms" },
{
method: "oidc",
options: {
provider: "google",
client_id: import.meta.env.VITE_GOOGLE_SSO_CLIENT_ID ?? "test_oidc",
},
},
{
method: "saml",
options: {
provider_credentials_id: "test_saml",
},
},
]}
>
<SlashIDLoaded>
<>
<LoggedIn>Logged in!</LoggedIn>
<LoggedOut>
<Form>
<Slot name="initial">
<Form.Initial.Logo />
<Form.Initial.Header />
<h1>What a mess!</h1>
<p>Some text on top of the form</p>
<Form.Initial.Controls>
<h2>Controls should be here</h2>
<Form.Initial.Controls.Input />
<p>Text below controls, on top of the submit button</p>
<Form.Initial.Controls.Submit />
<p>Text right below the submit button</p>
</Form.Initial.Controls>
<Form.Initial.SSO />
<p>Some text below the form</p>
</Slot>
<Slot name="error">
<Form.Error>
{({ context, retry, cancel }) => (
<div>
<h1>{context.error.message}</h1>
<button onClick={retry}>Retry</button>
<button onClick={cancel}>Cancel</button>
</div>
)}
</Form.Error>
</Slot>
<Slot name="footer">
<p>
Some footer text with a{" "}
<a
target="_blank"
rel="noreferrer"
href="https://www.google.com"
>
link
</a>
</p>
</Slot>
</Form>
</LoggedOut>
</>
</SlashIDLoaded>
</ConfigurationProvider>
);
};
const vars = {
"--sid-input-border-radius": "32px",
"--sid-input-label-color": "yellow",
"--sid-button-border-radius": "32px",
"--sid-input-border-color": "red",
"--sid-color-primary": "red",
} as React.CSSProperties;
/* const checkatradeVars = {
"--sid-color-primary": "#0058A2",
"--sid-button-border-radius": "4px",
"--sid-input-border-radius": "4px",
"--sid-input-border-color": "#EDEDED",
"--sid-input-label-color": "#62687A",
"--sid-form-logo-width": "150px",
"--sid-form-border-radius": "0px",
} as React.CSSProperties; */
const LogOut = () => {
const { logOut } = useSlashID();
return (
<LoggedIn>
<button onClick={() => logOut()}>Log out</button>
</LoggedIn>
);
};
const container = document.getElementById("root") as HTMLElement;
const root = ReactDOM.createRoot(container);
root.render(
<React.StrictMode>
<SlashIDProvider
oid={import.meta.env.VITE_ORG_ID}
themeProps={{ theme: "dark" }}
tokenStorage="memory"
analyticsEnabled
>
<LogOut />
<div className="layout">
<div>
<div style={vars}>
<h2>Basic form</h2>
<BasicForm />
</div>
<div>
<h2>Composed form</h2>
<ComposedForm />
</div>
</div>
<div>
<div>
<h2>Switch to default org</h2>
<Config />
</div>
<div>
<h2>Dynamic flow - factor based on handle</h2>
<ConfiguredDynamicFlow />
</div>
</div>
</div>
</SlashIDProvider>
</React.StrictMode>
);