@open-workspace/create 0.4.1 → 0.5.0
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/README.md +9 -5
- package/dist/backup-schema.ts +110 -0
- package/dist/workspace.ts +162 -0
- package/import/backup-snapshot.ts +254 -0
- package/import/backup-source.ts +270 -0
- package/import/cli.ts +134 -90
- package/import/concurrency.ts +34 -37
- package/import/csv.ts +1 -1
- package/import/directory/command.ts +293 -72
- package/import/directory/google.ts +158 -120
- package/import/directory/invitation-file.ts +108 -0
- package/import/directory/plan.ts +314 -38
- package/import/directory/read.ts +55 -0
- package/import/gmail.ts +9 -12
- package/import/headers.ts +1 -1
- package/import/idp/apps.ts +244 -0
- package/import/idp/command.ts +161 -0
- package/import/idp/csv.ts +70 -0
- package/import/idp/entra.ts +297 -0
- package/import/idp/keycloak.ts +250 -0
- package/import/idp/model.ts +108 -0
- package/import/idp/okta.ts +159 -0
- package/import/idp/pages.ts +65 -0
- package/import/imap-command.ts +42 -18
- package/import/imap-source.ts +245 -220
- package/import/invariant.ts +1 -1
- package/import/loopback.ts +136 -0
- package/import/mbox-source.ts +106 -55
- package/import/mbox.ts +1 -1
- package/import/oauth.ts +234 -138
- package/import/preview.ts +48 -0
- package/import/uploader.ts +169 -115
- package/import/workspace.ts +78 -15
- package/index.ts +3 -0
- package/module-templates.ts +9 -12
- package/module.ts +1 -5
- package/package.json +13 -2
- package/setup/alchemy.ts +2 -2
- package/setup/args.ts +16 -17
- package/setup/cloudflare-api.ts +75 -19
- package/setup/domains.ts +18 -6
- package/setup/flow.ts +217 -58
- package/setup/migration-guide.ts +255 -0
- package/setup/mx-recovery.ts +115 -0
- package/setup/ui.ts +4 -1
- package/templates.ts +26 -18
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { forEachConcurrent } from "../concurrency.ts";
|
|
4
|
+
import { readDirectoryPage } from "../directory/read.ts";
|
|
5
|
+
import type { Fetch } from "../oauth.ts";
|
|
6
|
+
import { browserOrigins, emptyExport, person, team } from "./model.ts";
|
|
7
|
+
import { readPages } from "./pages.ts";
|
|
8
|
+
|
|
9
|
+
const graph = "https://graph.microsoft.com/v1.0/";
|
|
10
|
+
const guid = z.guid();
|
|
11
|
+
|
|
12
|
+
const userSchema = z.object({
|
|
13
|
+
id: z.string(),
|
|
14
|
+
displayName: z.string(),
|
|
15
|
+
mail: z.string().nullable(),
|
|
16
|
+
userPrincipalName: z.string(),
|
|
17
|
+
userType: z.string().nullable(),
|
|
18
|
+
accountEnabled: z.boolean(),
|
|
19
|
+
proxyAddresses: z.array(z.string()),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const groupSchema = z.object({
|
|
23
|
+
id: z.string(),
|
|
24
|
+
displayName: z.string(),
|
|
25
|
+
mail: z.string().nullable(),
|
|
26
|
+
groupTypes: z.array(z.string()),
|
|
27
|
+
proxyAddresses: z.array(z.string()),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const platform = z.object({
|
|
31
|
+
redirectUris: z.array(z.string()),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function aliases(addresses: string[], primary: string) {
|
|
35
|
+
return addresses.flatMap((value) => {
|
|
36
|
+
const address = value.slice(5);
|
|
37
|
+
|
|
38
|
+
return /^smtp:/iu.test(value) &&
|
|
39
|
+
address.toLowerCase() !== primary.toLowerCase()
|
|
40
|
+
? [address]
|
|
41
|
+
: [];
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Delegated device login for the administrator's public client. Uses no client secret. */
|
|
46
|
+
export async function graphSignIn(
|
|
47
|
+
tenant: string,
|
|
48
|
+
client: string,
|
|
49
|
+
fetchImpl: Fetch,
|
|
50
|
+
log: (line: string) => void
|
|
51
|
+
) {
|
|
52
|
+
const base = `https://login.microsoftonline.com/${guid.parse(tenant)}/oauth2/v2.0/`;
|
|
53
|
+
const clientId = guid.parse(client);
|
|
54
|
+
|
|
55
|
+
const post = (endpoint: string, fields: Record<string, string>) =>
|
|
56
|
+
fetchImpl(base + endpoint, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
body: new URLSearchParams({ client_id: clientId, ...fields }),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const challenge = await post("devicecode", {
|
|
62
|
+
scope:
|
|
63
|
+
"https://graph.microsoft.com/User.Read.All https://graph.microsoft.com/GroupMember.Read.All https://graph.microsoft.com/Member.Read.Hidden https://graph.microsoft.com/Application.Read.All",
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (!challenge.ok) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Microsoft device sign-in failed (HTTP ${challenge.status}). Enable public client flows on the Entra app.`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const code = z
|
|
73
|
+
.object({
|
|
74
|
+
message: z.string(),
|
|
75
|
+
device_code: z.string(),
|
|
76
|
+
expires_in: z.number(),
|
|
77
|
+
interval: z.number().optional(),
|
|
78
|
+
})
|
|
79
|
+
.parse(await challenge.json());
|
|
80
|
+
|
|
81
|
+
log(code.message);
|
|
82
|
+
let delay = (code.interval ?? 5) * 1000;
|
|
83
|
+
const until = Date.now() + code.expires_in * 1000;
|
|
84
|
+
|
|
85
|
+
/* oxlint-disable no-await-in-loop -- Microsoft specifies the polling interval and returns authorization_pending until sign-in */
|
|
86
|
+
while (Date.now() + delay < until) {
|
|
87
|
+
await Bun.sleep(delay);
|
|
88
|
+
|
|
89
|
+
const response = await post("token", {
|
|
90
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
91
|
+
device_code: code.device_code,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const token = z
|
|
95
|
+
.object({
|
|
96
|
+
access_token: z.string().optional(),
|
|
97
|
+
error: z.string().optional(),
|
|
98
|
+
error_description: z.string().optional(),
|
|
99
|
+
})
|
|
100
|
+
.parse(await response.json());
|
|
101
|
+
|
|
102
|
+
if (response.ok && token.access_token) return token.access_token;
|
|
103
|
+
|
|
104
|
+
if (token.error === "slow_down") delay += 5000;
|
|
105
|
+
else if (token.error !== "authorization_pending") {
|
|
106
|
+
throw new Error(token.error_description ?? "Microsoft sign-in failed");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* oxlint-enable no-await-in-loop */
|
|
111
|
+
throw new Error("Microsoft sign-in expired. Run the command again.");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function readEntra(
|
|
115
|
+
tenant: string,
|
|
116
|
+
token: string,
|
|
117
|
+
fetchImpl: Fetch
|
|
118
|
+
) {
|
|
119
|
+
const list = <T>(path: string, schema: z.ZodType<T>) =>
|
|
120
|
+
readPages(new URL(path, graph), `Bearer ${token}`, schema, fetchImpl, true);
|
|
121
|
+
|
|
122
|
+
const [users, groups, apps, principals] = await Promise.all([
|
|
123
|
+
list(
|
|
124
|
+
"users?$select=id,displayName,mail,userPrincipalName,userType,accountEnabled,proxyAddresses",
|
|
125
|
+
userSchema
|
|
126
|
+
),
|
|
127
|
+
list(
|
|
128
|
+
"groups?$select=id,displayName,mail,groupTypes,proxyAddresses",
|
|
129
|
+
groupSchema
|
|
130
|
+
),
|
|
131
|
+
list(
|
|
132
|
+
"applications?$select=appId,displayName,web,spa,publicClient",
|
|
133
|
+
z.object({
|
|
134
|
+
appId: z.string().nullish(),
|
|
135
|
+
displayName: z.string().nullable(),
|
|
136
|
+
web: platform.nullish(),
|
|
137
|
+
spa: platform.nullish(),
|
|
138
|
+
publicClient: platform.nullish(),
|
|
139
|
+
})
|
|
140
|
+
),
|
|
141
|
+
list(
|
|
142
|
+
"servicePrincipals?$select=appId,displayName,preferredSingleSignOnMode,servicePrincipalNames,replyUrls",
|
|
143
|
+
z.object({
|
|
144
|
+
appId: z.string().nullish(),
|
|
145
|
+
displayName: z.string().nullable(),
|
|
146
|
+
preferredSingleSignOnMode: z.string().nullable(),
|
|
147
|
+
servicePrincipalNames: z.array(z.string()),
|
|
148
|
+
replyUrls: z.array(z.string()),
|
|
149
|
+
})
|
|
150
|
+
),
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
const result = emptyExport();
|
|
154
|
+
const ids = new Map<string, string>();
|
|
155
|
+
|
|
156
|
+
for (const u of users) {
|
|
157
|
+
if (u.userType === "Guest") continue;
|
|
158
|
+
const email = u.mail || u.userPrincipalName;
|
|
159
|
+
result.directory.users.push(
|
|
160
|
+
person(
|
|
161
|
+
email,
|
|
162
|
+
u.displayName,
|
|
163
|
+
!u.accountEnabled,
|
|
164
|
+
aliases(u.proxyAddresses, email)
|
|
165
|
+
)
|
|
166
|
+
);
|
|
167
|
+
ids.set(u.id, email);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const entries = groups.map((g) => {
|
|
171
|
+
const group = team(
|
|
172
|
+
`entra:${tenant}:${g.id}`,
|
|
173
|
+
g.displayName,
|
|
174
|
+
g.mail ?? "",
|
|
175
|
+
aliases(g.proxyAddresses, g.mail ?? "")
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
ids.set(g.id, group.key ?? group.email);
|
|
179
|
+
result.directory.groups.push(group);
|
|
180
|
+
|
|
181
|
+
return { id: g.id, unified: g.groupTypes.includes("Unified"), group };
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await forEachConcurrent(entries, 4, async ({ id, unified, group }) => {
|
|
185
|
+
const path = `groups/${encodeURIComponent(id)}`;
|
|
186
|
+
|
|
187
|
+
const members = await list(
|
|
188
|
+
`${path}/members?$select=id`,
|
|
189
|
+
z.object({ id: z.string() })
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
group.members = members.flatMap((member) => ids.get(member.id) ?? []);
|
|
193
|
+
|
|
194
|
+
if (!group.email && !group.aliases.length) return;
|
|
195
|
+
|
|
196
|
+
if (!unified) {
|
|
197
|
+
result.skipped.push(
|
|
198
|
+
`Exchange posting settings for ${group.email || group.name}: review the imported workspace-only posting policy`
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Graph exposes this Exchange property only on an individual Microsoft 365 group.
|
|
205
|
+
try {
|
|
206
|
+
const response = await readDirectoryPage(
|
|
207
|
+
fetchImpl,
|
|
208
|
+
new URL(`${path}?$select=allowExternalSenders`, graph).href,
|
|
209
|
+
{ headers: { authorization: `Bearer ${token}` }, redirect: "error" }
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
213
|
+
|
|
214
|
+
const settings = z
|
|
215
|
+
.object({ allowExternalSenders: z.boolean() })
|
|
216
|
+
.safeParse(await response.json());
|
|
217
|
+
|
|
218
|
+
if (!settings.success) {
|
|
219
|
+
throw new Error("allowExternalSenders missing or invalid");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
group.posting = settings.data.allowExternalSenders
|
|
223
|
+
? "anyone"
|
|
224
|
+
: "workspace";
|
|
225
|
+
} catch (error) {
|
|
226
|
+
group.mailboxReadError = `could not read Microsoft 365 posting settings: ${error instanceof Error ? error.message : String(error)}`;
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const samlPrincipals = principals.filter(
|
|
231
|
+
(p) => p.preferredSingleSignOnMode === "saml"
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const samlIds = new Set(samlPrincipals.flatMap((p) => p.appId ?? []));
|
|
235
|
+
|
|
236
|
+
for (const app of apps) {
|
|
237
|
+
if (app.appId && samlIds.has(app.appId)) continue;
|
|
238
|
+
const name = app.displayName || app.appId;
|
|
239
|
+
|
|
240
|
+
if (!name) continue;
|
|
241
|
+
const webUris = app.web?.redirectUris ?? [];
|
|
242
|
+
|
|
243
|
+
const spaUris = app.spa?.redirectUris ?? [];
|
|
244
|
+
const publicUris = [...spaUris, ...(app.publicClient?.redirectUris ?? [])];
|
|
245
|
+
|
|
246
|
+
if (webUris.length && publicUris.length) {
|
|
247
|
+
result.skipped.push(`${name}: mixed public and confidential clients`);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const redirectUris = webUris.length ? webUris : publicUris;
|
|
252
|
+
|
|
253
|
+
if (redirectUris.length) {
|
|
254
|
+
result.apps.push({
|
|
255
|
+
kind: "oidc",
|
|
256
|
+
sourceId: app.appId ?? name,
|
|
257
|
+
name,
|
|
258
|
+
publicClient: !webUris.length,
|
|
259
|
+
allowedOrigins: browserOrigins(spaUris),
|
|
260
|
+
redirectUris,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (const app of samlPrincipals) {
|
|
266
|
+
const name = app.displayName || app.appId;
|
|
267
|
+
|
|
268
|
+
if (!name) continue;
|
|
269
|
+
|
|
270
|
+
const names = app.servicePrincipalNames.filter(
|
|
271
|
+
(value) => value.toLowerCase() !== app.appId?.toLowerCase()
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
const [entityId] = names;
|
|
275
|
+
const [acsUrl] = app.replyUrls;
|
|
276
|
+
|
|
277
|
+
if (
|
|
278
|
+
names.length !== 1 ||
|
|
279
|
+
app.replyUrls.length !== 1 ||
|
|
280
|
+
!entityId ||
|
|
281
|
+
!acsUrl
|
|
282
|
+
) {
|
|
283
|
+
result.skipped.push(
|
|
284
|
+
`${name}: ambiguous SAML identifiers or reply URLs; import the app's SP metadata`
|
|
285
|
+
);
|
|
286
|
+
} else {
|
|
287
|
+
result.apps.push({
|
|
288
|
+
kind: "saml",
|
|
289
|
+
name,
|
|
290
|
+
entityId,
|
|
291
|
+
acsUrl,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
browserOrigin,
|
|
7
|
+
browserOrigins,
|
|
8
|
+
emptyExport,
|
|
9
|
+
person,
|
|
10
|
+
team,
|
|
11
|
+
} from "./model.ts";
|
|
12
|
+
|
|
13
|
+
const text = z.string().nullish();
|
|
14
|
+
|
|
15
|
+
const groupNode = z.object({
|
|
16
|
+
id: z.string(),
|
|
17
|
+
name: z.string(),
|
|
18
|
+
path: text,
|
|
19
|
+
subGroups: z.array(z.unknown()).default([]),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const client = z.object({
|
|
23
|
+
clientId: z.string(),
|
|
24
|
+
name: text,
|
|
25
|
+
protocol: text,
|
|
26
|
+
publicClient: z.boolean().optional(),
|
|
27
|
+
bearerOnly: z.boolean().optional(),
|
|
28
|
+
standardFlowEnabled: z.boolean().optional(),
|
|
29
|
+
frontchannelLogout: z.boolean().optional(),
|
|
30
|
+
redirectUris: z.array(z.string()).default([]),
|
|
31
|
+
webOrigins: z.array(z.string()).default([]),
|
|
32
|
+
rootUrl: text,
|
|
33
|
+
adminUrl: text,
|
|
34
|
+
attributes: z.record(z.string(), z.string()).default({}),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const realmSchema = z.object({
|
|
38
|
+
realm: z.string(),
|
|
39
|
+
groups: z.array(groupNode).default([]),
|
|
40
|
+
users: z
|
|
41
|
+
.array(
|
|
42
|
+
z.object({
|
|
43
|
+
email: text,
|
|
44
|
+
username: z.string(),
|
|
45
|
+
firstName: text,
|
|
46
|
+
lastName: text,
|
|
47
|
+
enabled: z.boolean(),
|
|
48
|
+
groups: z.array(z.string()).default([]),
|
|
49
|
+
})
|
|
50
|
+
)
|
|
51
|
+
.default([]),
|
|
52
|
+
clients: z.array(client).default([]),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
function explicitWebOrigin(value: string) {
|
|
56
|
+
// Keycloak also accepts patterns and "+" (derive origins from redirects).
|
|
57
|
+
if (!/^https?:\/\/[^/\\?#@*\s$]+\/?$/iu.test(value)) return null;
|
|
58
|
+
|
|
59
|
+
return browserOrigin(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function exactClientUrl(value: string, root: string | null | undefined) {
|
|
63
|
+
if (!value || value.includes("*") || value.includes("${")) return null;
|
|
64
|
+
|
|
65
|
+
if (URL.canParse(value)) return value;
|
|
66
|
+
|
|
67
|
+
if (!root || root.includes("*") || root.includes("${")) return null;
|
|
68
|
+
|
|
69
|
+
// Keycloak appends a leading-slash endpoint to the whole client root, including its path.
|
|
70
|
+
if (value.startsWith("/")) {
|
|
71
|
+
const url = root + value;
|
|
72
|
+
|
|
73
|
+
return URL.canParse(url) ? url : null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return URL.parse(value, root)?.href ?? null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function logoutRedirects(setting: string | undefined, redirects: string[]) {
|
|
80
|
+
const entries = setting?.split(/\s*##\s*/u).filter(Boolean) ?? [];
|
|
81
|
+
|
|
82
|
+
// An unset value or "+" inherits login redirects. A leading "-" disables returns.
|
|
83
|
+
if (!entries.length) return redirects;
|
|
84
|
+
|
|
85
|
+
if (entries[0] === "-") return [];
|
|
86
|
+
|
|
87
|
+
return entries.flatMap((entry) => (entry === "+" ? redirects : [entry]));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function nameIdWarning(format: string | undefined) {
|
|
91
|
+
if (format === "email") return undefined;
|
|
92
|
+
|
|
93
|
+
if (format === "persistent") {
|
|
94
|
+
return "OpenWorkspace issues new persistent NameIDs. Check how this app links existing accounts before enabling sign-in.";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const source = format
|
|
98
|
+
? `Keycloak's configured NameID format is "${format}".`
|
|
99
|
+
: "Keycloak defaults to the username when the app requests no NameID format.";
|
|
100
|
+
|
|
101
|
+
return `${source} OpenWorkspace will send the user's email. Check how this app links existing accounts before enabling sign-in.`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function readKeycloak(filename: string) {
|
|
105
|
+
const realm = realmSchema.parse(JSON.parse(readFileSync(filename, "utf-8")));
|
|
106
|
+
const result = emptyExport();
|
|
107
|
+
const groups = new Map<string, ReturnType<typeof team>>();
|
|
108
|
+
|
|
109
|
+
function addGroup(g: z.infer<typeof groupNode>, parentPath: string) {
|
|
110
|
+
const key = g.path || `${parentPath}/${g.name}`;
|
|
111
|
+
const group = team(`keycloak:${realm.realm}:${g.id}`, g.name);
|
|
112
|
+
groups.set(key, group);
|
|
113
|
+
result.directory.groups.push(group);
|
|
114
|
+
groups.get(parentPath)?.members.push(group.key ?? group.email);
|
|
115
|
+
|
|
116
|
+
for (const child of g.subGroups) addGroup(groupNode.parse(child), key);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const g of realm.groups) addGroup(g, "");
|
|
120
|
+
|
|
121
|
+
for (const u of realm.users) {
|
|
122
|
+
if (!u.email) {
|
|
123
|
+
result.skipped.push(`${u.username}: no email`);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
result.directory.users.push(
|
|
128
|
+
person(
|
|
129
|
+
u.email,
|
|
130
|
+
[u.firstName, u.lastName].filter(Boolean).join(" "),
|
|
131
|
+
!u.enabled
|
|
132
|
+
)
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
for (const path of u.groups) {
|
|
136
|
+
const group = groups.get(path);
|
|
137
|
+
|
|
138
|
+
if (group) group.members.push(u.email);
|
|
139
|
+
else result.skipped.push(`${u.username}: missing group ${path}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const builtins = new Set([
|
|
144
|
+
"account",
|
|
145
|
+
"account-console",
|
|
146
|
+
"admin-cli",
|
|
147
|
+
"broker",
|
|
148
|
+
"realm-management",
|
|
149
|
+
"security-admin-console",
|
|
150
|
+
]);
|
|
151
|
+
|
|
152
|
+
for (const app of realm.clients) {
|
|
153
|
+
if (builtins.has(app.clientId)) continue;
|
|
154
|
+
const name = app.name || app.clientId;
|
|
155
|
+
|
|
156
|
+
if (
|
|
157
|
+
app.protocol === "openid-connect" &&
|
|
158
|
+
!app.bearerOnly &&
|
|
159
|
+
app.standardFlowEnabled !== false &&
|
|
160
|
+
app.redirectUris.length
|
|
161
|
+
) {
|
|
162
|
+
const resolveUrl = (uri: string) => exactClientUrl(uri, app.rootUrl);
|
|
163
|
+
const urls = app.redirectUris.map(resolveUrl);
|
|
164
|
+
|
|
165
|
+
const logout = logoutRedirects(
|
|
166
|
+
app.attributes["post.logout.redirect.uris"],
|
|
167
|
+
app.redirectUris
|
|
168
|
+
).map(resolveUrl);
|
|
169
|
+
|
|
170
|
+
const backchannel =
|
|
171
|
+
!app.frontchannelLogout && app.attributes["backchannel.logout.url"];
|
|
172
|
+
|
|
173
|
+
const backchannelLogoutUri = backchannel
|
|
174
|
+
? resolveUrl(backchannel)
|
|
175
|
+
: undefined;
|
|
176
|
+
|
|
177
|
+
if (
|
|
178
|
+
!urls.every((uri) => uri !== null) ||
|
|
179
|
+
!logout.every((uri) => uri !== null)
|
|
180
|
+
) {
|
|
181
|
+
result.skipped.push(
|
|
182
|
+
`${name}: configure exact redirect URLs before importing`
|
|
183
|
+
);
|
|
184
|
+
} else if (backchannelLogoutUri === null) {
|
|
185
|
+
result.skipped.push(
|
|
186
|
+
`${name}: configure an exact back-channel logout URL before importing`
|
|
187
|
+
);
|
|
188
|
+
} else {
|
|
189
|
+
const allowedOrigins = new Set<string>();
|
|
190
|
+
|
|
191
|
+
for (const value of app.webOrigins) {
|
|
192
|
+
if (value === "+") {
|
|
193
|
+
for (const origin of browserOrigins(urls)) {
|
|
194
|
+
allowedOrigins.add(origin);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const origin = explicitWebOrigin(value);
|
|
201
|
+
|
|
202
|
+
if (origin) {
|
|
203
|
+
allowedOrigins.add(origin);
|
|
204
|
+
} else {
|
|
205
|
+
result.skipped.push(
|
|
206
|
+
`${name}: review unsupported web origin ${value}`
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
result.apps.push({
|
|
212
|
+
allowedOrigins: [...allowedOrigins],
|
|
213
|
+
kind: "oidc",
|
|
214
|
+
sourceId: app.clientId,
|
|
215
|
+
name,
|
|
216
|
+
redirectUris: urls,
|
|
217
|
+
logoutUris: [...new Set(logout)],
|
|
218
|
+
backchannelLogoutUri,
|
|
219
|
+
publicClient: app.publicClient === true,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
} else if (
|
|
223
|
+
app.protocol === "saml" &&
|
|
224
|
+
(app.attributes.saml_assertion_consumer_url_post || app.adminUrl)
|
|
225
|
+
) {
|
|
226
|
+
// Keycloak exports the certificate body. The workspace validates the PEM,
|
|
227
|
+
// including rejecting a missing certificate when signatures are required.
|
|
228
|
+
const certificate = app.attributes["saml.signing.certificate"]?.trim();
|
|
229
|
+
const format = app.attributes.saml_name_id_format;
|
|
230
|
+
|
|
231
|
+
result.apps.push({
|
|
232
|
+
kind: "saml",
|
|
233
|
+
name,
|
|
234
|
+
entityId: app.clientId,
|
|
235
|
+
nameIdFormat: format === "persistent" ? "persistent" : "email",
|
|
236
|
+
nameIdWarning: nameIdWarning(format),
|
|
237
|
+
requireSignedAuthnRequests:
|
|
238
|
+
app.attributes["saml.client.signature"] === "true",
|
|
239
|
+
spSigningCertificate:
|
|
240
|
+
certificate && !certificate.startsWith("-----BEGIN CERTIFICATE-----")
|
|
241
|
+
? `-----BEGIN CERTIFICATE-----\n${certificate}\n-----END CERTIFICATE-----`
|
|
242
|
+
: certificate || null,
|
|
243
|
+
acsUrl:
|
|
244
|
+
app.attributes.saml_assertion_consumer_url_post || app.adminUrl || "",
|
|
245
|
+
});
|
|
246
|
+
} else result.skipped.push(`${name}: no supported sign-in endpoints`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Directory,
|
|
3
|
+
DirectoryGroup,
|
|
4
|
+
DirectoryUser,
|
|
5
|
+
} from "../directory/plan.ts";
|
|
6
|
+
|
|
7
|
+
export type ImportedApp = {
|
|
8
|
+
name: string;
|
|
9
|
+
} & (
|
|
10
|
+
| {
|
|
11
|
+
kind: "oidc";
|
|
12
|
+
sourceId: string;
|
|
13
|
+
redirectUris: string[];
|
|
14
|
+
publicClient: boolean;
|
|
15
|
+
logoutUris?: string[];
|
|
16
|
+
backchannelLogoutUri?: string;
|
|
17
|
+
allowedOrigins?: string[];
|
|
18
|
+
}
|
|
19
|
+
| {
|
|
20
|
+
kind: "saml";
|
|
21
|
+
entityId: string;
|
|
22
|
+
acsUrl: string;
|
|
23
|
+
nameIdFormat?: "email" | "persistent";
|
|
24
|
+
nameIdWarning?: string;
|
|
25
|
+
requireSignedAuthnRequests?: boolean;
|
|
26
|
+
spSigningCertificate?: string | null;
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
/** The workspace permits HTTPS browser origins and local HTTP development servers. */
|
|
31
|
+
export function browserOrigin(value: string) {
|
|
32
|
+
const url = URL.parse(value);
|
|
33
|
+
|
|
34
|
+
if (!url || url.username || url.password || url.hostname.includes("*")) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (url.protocol === "https:") return url.origin;
|
|
39
|
+
|
|
40
|
+
return url.protocol === "http:" &&
|
|
41
|
+
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
|
|
42
|
+
? url.origin
|
|
43
|
+
: null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Browser apps exchange the authorization code from an origin of their redirect URLs. */
|
|
47
|
+
export function browserOrigins(redirectUris: string[]) {
|
|
48
|
+
return [...new Set(redirectUris.flatMap((uri) => browserOrigin(uri) ?? []))];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type IdentityExport = {
|
|
52
|
+
directory: Directory;
|
|
53
|
+
apps: ImportedApp[];
|
|
54
|
+
skipped: string[];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export function emptyExport() {
|
|
58
|
+
const result: IdentityExport = {
|
|
59
|
+
directory: { users: [], groups: [] },
|
|
60
|
+
apps: [],
|
|
61
|
+
skipped: [],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function person(
|
|
68
|
+
email: string,
|
|
69
|
+
name: string,
|
|
70
|
+
suspended = false,
|
|
71
|
+
aliases: string[] = []
|
|
72
|
+
) {
|
|
73
|
+
return {
|
|
74
|
+
email,
|
|
75
|
+
name,
|
|
76
|
+
suspended,
|
|
77
|
+
aliases,
|
|
78
|
+
isAdmin: false,
|
|
79
|
+
} satisfies DirectoryUser;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A group without mail uses its source key for nested membership. */
|
|
83
|
+
export function team(
|
|
84
|
+
sourceId: string,
|
|
85
|
+
name: string,
|
|
86
|
+
email = "",
|
|
87
|
+
aliases: string[] = []
|
|
88
|
+
) {
|
|
89
|
+
const hash = new Bun.CryptoHasher("sha256")
|
|
90
|
+
.update(sourceId)
|
|
91
|
+
.digest("hex")
|
|
92
|
+
.slice(0, 20);
|
|
93
|
+
|
|
94
|
+
const group: DirectoryGroup = {
|
|
95
|
+
sourceId,
|
|
96
|
+
key: email || `idp-${hash}`,
|
|
97
|
+
email,
|
|
98
|
+
name,
|
|
99
|
+
aliases,
|
|
100
|
+
members: [],
|
|
101
|
+
description: null,
|
|
102
|
+
sharedInbox: false,
|
|
103
|
+
membersCanSend: null,
|
|
104
|
+
posting: null,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
return group;
|
|
108
|
+
}
|