@openpeepshq/common 0.1.46 → 0.2.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/dist/lib/adminSections.js +1 -1
- package/dist/lib/capabilitiesHelpers.js +30 -40
- package/dist/lib/csvHelpers.js +1 -1
- package/dist/lib/eventIcs.js +26 -32
- package/dist/lib/eventRecurrence.d.ts +17 -0
- package/dist/lib/eventRecurrence.js +75 -18
- package/dist/lib/eventTime.js +9 -11
- package/dist/lib/groupHelpers.js +13 -34
- package/dist/lib/groupTemplateHelpers.js +6 -10
- package/dist/lib/jamHelpers.js +6 -9
- package/dist/lib/markdownHelpers.js +2 -3
- package/dist/lib/postHelpers.js +70 -97
- package/dist/lib/profileHelpers.js +17 -24
- package/dist/lib/pushHelpers.js +3 -4
- package/dist/lib/scopeHelpers.js +6 -9
- package/dist/lib/ssoLoginUrl.js +4 -6
- package/dist/lib/ssoProviders.js +35 -39
- package/dist/lib/tokenHelpers.js +5 -5
- package/dist/lib/utils.js +6 -7
- package/dist/types/admin-db.js +1 -1
- package/dist/types/api.d.ts +1 -0
- package/dist/types/api.js +1 -1
- package/dist/types/config.d.ts +3 -0
- package/dist/types/config.js +3 -0
- package/dist/types/models.js +1 -1
- package/dist/types/plugins.js +2 -2
- package/dist/zod.d.ts +1 -0
- package/dist/zod.js +1 -0
- package/package.json +36 -6
- package/src/index.ts +4 -0
- package/src/lib/adminSections.ts +84 -0
- package/src/lib/attachmentHelpers.ts +14 -0
- package/src/lib/capabilitiesHelpers.ts +532 -0
- package/src/lib/colors.ts +20 -0
- package/src/lib/csvHelpers.ts +31 -0
- package/src/lib/eventIcs.ts +265 -0
- package/src/lib/eventRecurrence.ts +495 -0
- package/src/lib/eventTime.ts +206 -0
- package/src/lib/federationHosts.ts +22 -0
- package/src/lib/feedCursor.ts +27 -0
- package/src/lib/feedFormat.ts +24 -0
- package/src/lib/groupHelpers.ts +421 -0
- package/src/lib/groupTemplateHelpers.ts +83 -0
- package/src/lib/index.ts +29 -0
- package/src/lib/jamHelpers.ts +164 -0
- package/src/lib/legacyThemeRoot.ts +14 -0
- package/src/lib/markdownHelpers.ts +122 -0
- package/src/lib/mediaDimensions.ts +21 -0
- package/src/lib/postHelpers.ts +783 -0
- package/src/lib/profileHelpers.ts +220 -0
- package/src/lib/pushHelpers.ts +34 -0
- package/src/lib/reactions.ts +52 -0
- package/src/lib/reportHelpers.ts +7 -0
- package/src/lib/scopeHelpers.ts +176 -0
- package/src/lib/ssoLoginUrl.ts +93 -0
- package/src/lib/ssoProviders.ts +472 -0
- package/src/lib/text.ts +26 -0
- package/src/lib/timeZone.ts +52 -0
- package/src/lib/tokenHelpers.ts +119 -0
- package/src/lib/utils.ts +322 -0
- package/src/types/admin-db.ts +74 -0
- package/src/types/admin-email.ts +55 -0
- package/src/types/admin-jobs.ts +17 -0
- package/src/types/admin-performance.ts +20 -0
- package/src/types/analytics.ts +318 -0
- package/src/types/api.ts +871 -0
- package/src/types/apple-app-site-association.ts +54 -0
- package/src/types/auth.ts +22 -0
- package/src/types/capabilities.ts +280 -0
- package/src/types/config.ts +546 -0
- package/src/types/db.ts +138 -0
- package/src/types/export.ts +10 -0
- package/src/types/index.ts +20 -0
- package/src/types/internal.ts +338 -0
- package/src/types/models.ts +939 -0
- package/src/types/notifications.ts +92 -0
- package/src/types/plugins.ts +150 -0
- package/src/types/roleDefaults.ts +132 -0
- package/src/types/stats.ts +195 -0
- package/src/types/stripe.ts +45 -0
- package/src/types/utils.ts +100 -0
- package/src/zod.ts +1 -0
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import { passwordPlaceHolder, type CoreConfig } from '../types';
|
|
2
|
+
|
|
3
|
+
export const SSO_OIDC_KINDS = [
|
|
4
|
+
'oidc',
|
|
5
|
+
'github',
|
|
6
|
+
'gitlab',
|
|
7
|
+
'gitlab-selfhosted',
|
|
8
|
+
'google',
|
|
9
|
+
'entra',
|
|
10
|
+
'auth0',
|
|
11
|
+
'keycloak',
|
|
12
|
+
'authentik',
|
|
13
|
+
'okta',
|
|
14
|
+
'discord',
|
|
15
|
+
'pocketid',
|
|
16
|
+
] as const;
|
|
17
|
+
|
|
18
|
+
export type SsoOidcKind = (typeof SSO_OIDC_KINDS)[number];
|
|
19
|
+
export type SsoProviderKind = SsoOidcKind | 'generic';
|
|
20
|
+
|
|
21
|
+
export const SSO_ADD_KINDS = [
|
|
22
|
+
'gitlab',
|
|
23
|
+
'gitlab-selfhosted',
|
|
24
|
+
'github',
|
|
25
|
+
'google',
|
|
26
|
+
'entra',
|
|
27
|
+
'discord',
|
|
28
|
+
'auth0',
|
|
29
|
+
'okta',
|
|
30
|
+
'keycloak',
|
|
31
|
+
'authentik',
|
|
32
|
+
'pocketid',
|
|
33
|
+
'oidc',
|
|
34
|
+
'generic',
|
|
35
|
+
] as const satisfies readonly SsoProviderKind[];
|
|
36
|
+
|
|
37
|
+
export type OidcProvider = CoreConfig['sso']['oidc'][number];
|
|
38
|
+
export type GenericProvider = CoreConfig['sso']['generic'][number];
|
|
39
|
+
|
|
40
|
+
export type OidcClaimMapping = NonNullable<OidcProvider['claimMapping']>;
|
|
41
|
+
|
|
42
|
+
export type OidcEndpoints = {
|
|
43
|
+
authorizationUrl: string;
|
|
44
|
+
tokenUrl: string;
|
|
45
|
+
userinfoUrl: string;
|
|
46
|
+
emailsUrl?: string;
|
|
47
|
+
jwksUri?: string;
|
|
48
|
+
scope: string;
|
|
49
|
+
claimMapping: OidcClaimMapping;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export type TemplateFieldNeed = false | 'optional' | 'required';
|
|
53
|
+
|
|
54
|
+
export type OidcTemplateNeeds = {
|
|
55
|
+
instanceUrl: TemplateFieldNeed;
|
|
56
|
+
tenant: TemplateFieldNeed;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const stripSlash = (url: string) => url.replace(/\/$/, '');
|
|
60
|
+
|
|
61
|
+
const withHttps = (url: string) => {
|
|
62
|
+
const trimmed = stripSlash(url.trim());
|
|
63
|
+
if (!trimmed) return '';
|
|
64
|
+
return /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const isGitHubDotCom = (web: string) =>
|
|
68
|
+
/^https?:\/\/(www\.)?github\.com$/i.test(web);
|
|
69
|
+
|
|
70
|
+
const oidcClaims = (
|
|
71
|
+
handle = 'preferred_username',
|
|
72
|
+
displayName = 'name',
|
|
73
|
+
avatar = 'picture',
|
|
74
|
+
): OidcClaimMapping => ({
|
|
75
|
+
email: 'email',
|
|
76
|
+
handle,
|
|
77
|
+
displayName,
|
|
78
|
+
avatar,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export const ssoOidcKind = (provider: { kind?: string }): SsoOidcKind =>
|
|
82
|
+
SSO_OIDC_KINDS.includes(provider.kind as SsoOidcKind)
|
|
83
|
+
? (provider.kind as SsoOidcKind)
|
|
84
|
+
: 'oidc';
|
|
85
|
+
|
|
86
|
+
export const oidcTemplateNeeds = (kind: SsoProviderKind): OidcTemplateNeeds => {
|
|
87
|
+
switch (kind) {
|
|
88
|
+
case 'github':
|
|
89
|
+
return { instanceUrl: 'optional', tenant: false };
|
|
90
|
+
case 'gitlab-selfhosted':
|
|
91
|
+
case 'auth0':
|
|
92
|
+
case 'okta':
|
|
93
|
+
case 'pocketid':
|
|
94
|
+
return {
|
|
95
|
+
instanceUrl: 'required',
|
|
96
|
+
tenant: kind === 'okta' ? 'optional' : false,
|
|
97
|
+
};
|
|
98
|
+
case 'keycloak':
|
|
99
|
+
case 'authentik':
|
|
100
|
+
return { instanceUrl: 'required', tenant: 'required' };
|
|
101
|
+
case 'entra':
|
|
102
|
+
return { instanceUrl: false, tenant: 'optional' };
|
|
103
|
+
default:
|
|
104
|
+
return { instanceUrl: false, tenant: false };
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const isSsoAddKind = (value: string): value is SsoProviderKind =>
|
|
109
|
+
(SSO_ADD_KINDS as readonly string[]).includes(value);
|
|
110
|
+
|
|
111
|
+
export const ssoKindDefaultId = (kind: SsoProviderKind): string => {
|
|
112
|
+
switch (kind) {
|
|
113
|
+
case 'gitlab-selfhosted':
|
|
114
|
+
return 'gitlab';
|
|
115
|
+
case 'entra':
|
|
116
|
+
return 'microsoft';
|
|
117
|
+
default:
|
|
118
|
+
return kind;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export const uniqueSsoProviderId = (
|
|
123
|
+
base: string,
|
|
124
|
+
used: Iterable<string>,
|
|
125
|
+
): string => {
|
|
126
|
+
const taken = new Set(used);
|
|
127
|
+
if (!taken.has(base)) return base;
|
|
128
|
+
let n = 2;
|
|
129
|
+
while (taken.has(`${base}-${n}`)) n += 1;
|
|
130
|
+
return `${base}-${n}`;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const ssoKindDefaultName = (kind: SsoProviderKind): string => {
|
|
134
|
+
switch (kind) {
|
|
135
|
+
case 'gitlab':
|
|
136
|
+
case 'gitlab-selfhosted':
|
|
137
|
+
return 'GitLab';
|
|
138
|
+
case 'github':
|
|
139
|
+
return 'GitHub';
|
|
140
|
+
case 'google':
|
|
141
|
+
return 'Google';
|
|
142
|
+
case 'entra':
|
|
143
|
+
return 'Microsoft';
|
|
144
|
+
case 'discord':
|
|
145
|
+
return 'Discord';
|
|
146
|
+
case 'auth0':
|
|
147
|
+
return 'Auth0';
|
|
148
|
+
case 'okta':
|
|
149
|
+
return 'Okta';
|
|
150
|
+
case 'keycloak':
|
|
151
|
+
return 'Keycloak';
|
|
152
|
+
case 'authentik':
|
|
153
|
+
return 'Authentik';
|
|
154
|
+
case 'pocketid':
|
|
155
|
+
return 'Pocket ID';
|
|
156
|
+
default:
|
|
157
|
+
return '';
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export const githubOidcEndpoints = (instanceUrl?: string): OidcEndpoints => {
|
|
162
|
+
const web = withHttps(instanceUrl || '') || 'https://github.com';
|
|
163
|
+
const api = isGitHubDotCom(web) ? 'https://api.github.com' : `${web}/api/v3`;
|
|
164
|
+
return {
|
|
165
|
+
authorizationUrl: `${web}/login/oauth/authorize`,
|
|
166
|
+
tokenUrl: `${web}/login/oauth/access_token`,
|
|
167
|
+
userinfoUrl: `${api}/user`,
|
|
168
|
+
emailsUrl: `${api}/user/emails`,
|
|
169
|
+
scope: 'read:user user:email',
|
|
170
|
+
claimMapping: oidcClaims('login', 'name', 'avatar_url'),
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export const gitlabOidcEndpoints = (instanceUrl?: string): OidcEndpoints => {
|
|
175
|
+
const base = withHttps(instanceUrl || '') || 'https://gitlab.com';
|
|
176
|
+
return {
|
|
177
|
+
authorizationUrl: `${base}/oauth/authorize`,
|
|
178
|
+
tokenUrl: `${base}/oauth/token`,
|
|
179
|
+
userinfoUrl: `${base}/oauth/userinfo`,
|
|
180
|
+
jwksUri: `${base}/oauth/discovery/keys`,
|
|
181
|
+
scope: 'openid email profile',
|
|
182
|
+
claimMapping: oidcClaims('nickname'),
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export const oidcEndpoints = (
|
|
187
|
+
kind: SsoOidcKind,
|
|
188
|
+
fields: { instanceUrl?: string; tenant?: string } = {},
|
|
189
|
+
): OidcEndpoints | undefined => {
|
|
190
|
+
const instance = withHttps(fields.instanceUrl || '');
|
|
191
|
+
const tenant = fields.tenant?.trim();
|
|
192
|
+
|
|
193
|
+
switch (kind) {
|
|
194
|
+
case 'github':
|
|
195
|
+
return githubOidcEndpoints(fields.instanceUrl);
|
|
196
|
+
case 'gitlab':
|
|
197
|
+
case 'gitlab-selfhosted':
|
|
198
|
+
return gitlabOidcEndpoints(
|
|
199
|
+
kind === 'gitlab-selfhosted' ? instance : fields.instanceUrl,
|
|
200
|
+
);
|
|
201
|
+
case 'google':
|
|
202
|
+
return {
|
|
203
|
+
authorizationUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
204
|
+
tokenUrl: 'https://oauth2.googleapis.com/token',
|
|
205
|
+
userinfoUrl: 'https://openidconnect.googleapis.com/v1/userinfo',
|
|
206
|
+
jwksUri: 'https://www.googleapis.com/oauth2/v3/certs',
|
|
207
|
+
scope: 'openid email profile',
|
|
208
|
+
claimMapping: oidcClaims('email'),
|
|
209
|
+
};
|
|
210
|
+
case 'entra': {
|
|
211
|
+
const directory = tenant || 'common';
|
|
212
|
+
return {
|
|
213
|
+
authorizationUrl: `https://login.microsoftonline.com/${directory}/oauth2/v2.0/authorize`,
|
|
214
|
+
tokenUrl: `https://login.microsoftonline.com/${directory}/oauth2/v2.0/token`,
|
|
215
|
+
userinfoUrl: 'https://graph.microsoft.com/oidc/userinfo',
|
|
216
|
+
jwksUri: `https://login.microsoftonline.com/${directory}/discovery/v2.0/keys`,
|
|
217
|
+
scope: 'openid email profile',
|
|
218
|
+
claimMapping: oidcClaims(),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
case 'auth0':
|
|
222
|
+
return {
|
|
223
|
+
authorizationUrl: `${instance}/authorize`,
|
|
224
|
+
tokenUrl: `${instance}/oauth/token`,
|
|
225
|
+
userinfoUrl: `${instance}/userinfo`,
|
|
226
|
+
jwksUri: `${instance}/.well-known/jwks.json`,
|
|
227
|
+
scope: 'openid email profile',
|
|
228
|
+
claimMapping: oidcClaims(),
|
|
229
|
+
};
|
|
230
|
+
case 'keycloak': {
|
|
231
|
+
const realm = `${instance}/realms/${tenant || 'master'}`;
|
|
232
|
+
return {
|
|
233
|
+
authorizationUrl: `${realm}/protocol/openid-connect/auth`,
|
|
234
|
+
tokenUrl: `${realm}/protocol/openid-connect/token`,
|
|
235
|
+
userinfoUrl: `${realm}/protocol/openid-connect/userinfo`,
|
|
236
|
+
jwksUri: `${realm}/protocol/openid-connect/certs`,
|
|
237
|
+
scope: 'openid email profile',
|
|
238
|
+
claimMapping: oidcClaims(),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
case 'authentik':
|
|
242
|
+
return {
|
|
243
|
+
authorizationUrl: `${instance}/application/o/authorize/`,
|
|
244
|
+
tokenUrl: `${instance}/application/o/token/`,
|
|
245
|
+
userinfoUrl: `${instance}/application/o/userinfo/`,
|
|
246
|
+
jwksUri: tenant
|
|
247
|
+
? `${instance}/application/o/${tenant}/jwks/`
|
|
248
|
+
: undefined,
|
|
249
|
+
scope: 'openid email profile',
|
|
250
|
+
claimMapping: oidcClaims(),
|
|
251
|
+
};
|
|
252
|
+
case 'okta': {
|
|
253
|
+
const server = tenant || 'default';
|
|
254
|
+
return {
|
|
255
|
+
authorizationUrl: `${instance}/oauth2/${server}/v1/authorize`,
|
|
256
|
+
tokenUrl: `${instance}/oauth2/${server}/v1/token`,
|
|
257
|
+
userinfoUrl: `${instance}/oauth2/${server}/v1/userinfo`,
|
|
258
|
+
jwksUri: `${instance}/oauth2/${server}/v1/keys`,
|
|
259
|
+
scope: 'openid email profile',
|
|
260
|
+
claimMapping: oidcClaims(),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
case 'discord':
|
|
264
|
+
return {
|
|
265
|
+
authorizationUrl: 'https://discord.com/api/oauth2/authorize',
|
|
266
|
+
tokenUrl: 'https://discord.com/api/oauth2/token',
|
|
267
|
+
userinfoUrl: 'https://discord.com/api/users/@me',
|
|
268
|
+
scope: 'identify email',
|
|
269
|
+
claimMapping: oidcClaims('username', 'global_name', 'avatar'),
|
|
270
|
+
};
|
|
271
|
+
case 'pocketid':
|
|
272
|
+
return {
|
|
273
|
+
authorizationUrl: `${instance}/authorize`,
|
|
274
|
+
tokenUrl: `${instance}/api/oidc/token`,
|
|
275
|
+
userinfoUrl: `${instance}/api/oidc/userinfo`,
|
|
276
|
+
jwksUri: `${instance}/.well-known/jwks.json`,
|
|
277
|
+
scope: 'openid email profile',
|
|
278
|
+
claimMapping: oidcClaims(),
|
|
279
|
+
};
|
|
280
|
+
default:
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
export type OidcProviderFields = {
|
|
286
|
+
id: string;
|
|
287
|
+
name: string;
|
|
288
|
+
instanceUrl?: string;
|
|
289
|
+
tenant?: string;
|
|
290
|
+
clientId: string;
|
|
291
|
+
clientSecret?: string;
|
|
292
|
+
scope?: string;
|
|
293
|
+
approvalRequired?: boolean;
|
|
294
|
+
authorizationUrl?: string;
|
|
295
|
+
tokenUrl?: string;
|
|
296
|
+
userinfoUrl?: string;
|
|
297
|
+
emailsUrl?: string;
|
|
298
|
+
jwksUri?: string;
|
|
299
|
+
claimMapping?: OidcProvider['claimMapping'];
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
export const applyOidcProvider = (
|
|
303
|
+
kind: SsoOidcKind,
|
|
304
|
+
fields: OidcProviderFields,
|
|
305
|
+
): OidcProvider => {
|
|
306
|
+
const tpl = oidcEndpoints(kind, fields);
|
|
307
|
+
if (tpl) {
|
|
308
|
+
return {
|
|
309
|
+
kind,
|
|
310
|
+
id: fields.id.trim(),
|
|
311
|
+
name: fields.name.trim(),
|
|
312
|
+
instanceUrl: fields.instanceUrl?.trim() || undefined,
|
|
313
|
+
tenant: fields.tenant?.trim() || undefined,
|
|
314
|
+
clientId: fields.clientId.trim(),
|
|
315
|
+
clientSecret: fields.clientSecret,
|
|
316
|
+
approvalRequired: fields.approvalRequired,
|
|
317
|
+
scope: fields.scope?.trim() || tpl.scope,
|
|
318
|
+
authorizationUrl: tpl.authorizationUrl,
|
|
319
|
+
tokenUrl: tpl.tokenUrl,
|
|
320
|
+
userinfoUrl: tpl.userinfoUrl,
|
|
321
|
+
emailsUrl: tpl.emailsUrl,
|
|
322
|
+
jwksUri: tpl.jwksUri,
|
|
323
|
+
claimMapping: tpl.claimMapping,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
kind: 'oidc',
|
|
329
|
+
id: fields.id.trim(),
|
|
330
|
+
name: fields.name.trim(),
|
|
331
|
+
clientId: fields.clientId.trim(),
|
|
332
|
+
clientSecret: fields.clientSecret,
|
|
333
|
+
approvalRequired: fields.approvalRequired,
|
|
334
|
+
scope: fields.scope?.trim() || 'openid email profile',
|
|
335
|
+
authorizationUrl: fields.authorizationUrl?.trim() || '',
|
|
336
|
+
tokenUrl: fields.tokenUrl?.trim() || '',
|
|
337
|
+
userinfoUrl: fields.userinfoUrl?.trim() || '',
|
|
338
|
+
emailsUrl: fields.emailsUrl?.trim() || undefined,
|
|
339
|
+
jwksUri: fields.jwksUri?.trim() || undefined,
|
|
340
|
+
claimMapping: {
|
|
341
|
+
email: fields.claimMapping?.email || 'email',
|
|
342
|
+
handle: fields.claimMapping?.handle || 'preferred_username',
|
|
343
|
+
displayName: fields.claimMapping?.displayName || 'name',
|
|
344
|
+
avatar: fields.claimMapping?.avatar || 'picture',
|
|
345
|
+
},
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export const applyGenericProvider = (fields: {
|
|
350
|
+
id: string;
|
|
351
|
+
name: string;
|
|
352
|
+
loginLink?: string;
|
|
353
|
+
profileUrl: string;
|
|
354
|
+
authHeader?: string;
|
|
355
|
+
emailPath: string;
|
|
356
|
+
handlePath?: string;
|
|
357
|
+
displayNamePath?: string;
|
|
358
|
+
avatarPath?: string;
|
|
359
|
+
createAccounts?: boolean;
|
|
360
|
+
createProfiles?: boolean;
|
|
361
|
+
}): GenericProvider => ({
|
|
362
|
+
id: fields.id.trim(),
|
|
363
|
+
name: fields.name.trim(),
|
|
364
|
+
loginLink: fields.loginLink?.trim() || undefined,
|
|
365
|
+
userProfileRequest: {
|
|
366
|
+
url: fields.profileUrl.trim(),
|
|
367
|
+
authHeader: fields.authHeader?.trim() || undefined,
|
|
368
|
+
},
|
|
369
|
+
userProfilePaths: {
|
|
370
|
+
email: fields.emailPath.trim(),
|
|
371
|
+
handle: fields.handlePath?.trim() || undefined,
|
|
372
|
+
displayName: fields.displayNamePath?.trim() || undefined,
|
|
373
|
+
avatar: fields.avatarPath?.trim() || undefined,
|
|
374
|
+
},
|
|
375
|
+
createAccounts: fields.createAccounts,
|
|
376
|
+
createProfiles: fields.createProfiles,
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
export const githubEmailsUrl = (provider: OidcProvider): string | undefined =>
|
|
380
|
+
provider.emailsUrl ||
|
|
381
|
+
(ssoOidcKind(provider) === 'github'
|
|
382
|
+
? githubOidcEndpoints(provider.instanceUrl).emailsUrl
|
|
383
|
+
: undefined);
|
|
384
|
+
|
|
385
|
+
export const discordAvatarUrl = (
|
|
386
|
+
claims: Record<string, unknown>,
|
|
387
|
+
): string | undefined => {
|
|
388
|
+
const id = claims.id;
|
|
389
|
+
if (typeof id !== 'string' || !id) return undefined;
|
|
390
|
+
const avatar = claims.avatar;
|
|
391
|
+
if (typeof avatar === 'string' && avatar) {
|
|
392
|
+
const format = avatar.startsWith('a_') ? 'gif' : 'png';
|
|
393
|
+
return `https://cdn.discordapp.com/avatars/${id}/${avatar}.${format}`;
|
|
394
|
+
}
|
|
395
|
+
const discriminator =
|
|
396
|
+
typeof claims.discriminator === 'string' ? claims.discriminator : '0';
|
|
397
|
+
const index =
|
|
398
|
+
discriminator === '0'
|
|
399
|
+
? Number(BigInt(id) >> BigInt(22)) % 6
|
|
400
|
+
: Number.parseInt(discriminator, 10) % 5;
|
|
401
|
+
return `https://cdn.discordapp.com/embed/avatars/${Number.isFinite(index) ? index : 0}.png`;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
export const pickOAuthEmail = (emails: unknown): string | undefined => {
|
|
405
|
+
if (!Array.isArray(emails)) return undefined;
|
|
406
|
+
const rows = emails.flatMap((entry) => {
|
|
407
|
+
if (!entry || typeof entry !== 'object') return [];
|
|
408
|
+
const email = (entry as { email?: unknown }).email;
|
|
409
|
+
if (typeof email !== 'string' || !email.includes('@')) return [];
|
|
410
|
+
return [
|
|
411
|
+
{
|
|
412
|
+
email,
|
|
413
|
+
primary: !!(entry as { primary?: unknown }).primary,
|
|
414
|
+
verified: (entry as { verified?: unknown }).verified !== false,
|
|
415
|
+
},
|
|
416
|
+
];
|
|
417
|
+
});
|
|
418
|
+
return (
|
|
419
|
+
rows.find((row) => row.primary && row.verified)?.email ??
|
|
420
|
+
rows.find((row) => row.verified)?.email ??
|
|
421
|
+
rows[0]?.email
|
|
422
|
+
);
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
export const parseOAuthTokenBody = (
|
|
426
|
+
body: string,
|
|
427
|
+
contentType: string | null,
|
|
428
|
+
): Record<string, unknown> => {
|
|
429
|
+
const trimmed = body.trim();
|
|
430
|
+
const type = contentType ?? '';
|
|
431
|
+
if (type.includes('application/json') || trimmed.startsWith('{')) {
|
|
432
|
+
return JSON.parse(trimmed) as Record<string, unknown>;
|
|
433
|
+
}
|
|
434
|
+
return Object.fromEntries(new URLSearchParams(trimmed));
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
438
|
+
!!value && typeof value === 'object' && !Array.isArray(value);
|
|
439
|
+
|
|
440
|
+
const recordId = (value: unknown): string | undefined => {
|
|
441
|
+
if (!isRecord(value) || typeof value.id !== 'string') return undefined;
|
|
442
|
+
return value.id;
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
/** Keep stored secrets when a sanitized patch still carries the placeholder. */
|
|
446
|
+
export const restorePasswordPlaceholders = (
|
|
447
|
+
incoming: unknown,
|
|
448
|
+
existing: unknown,
|
|
449
|
+
): unknown => {
|
|
450
|
+
if (incoming === passwordPlaceHolder) {
|
|
451
|
+
return existing === undefined ? incoming : existing;
|
|
452
|
+
}
|
|
453
|
+
if (Array.isArray(incoming)) {
|
|
454
|
+
const existingArr = Array.isArray(existing) ? existing : [];
|
|
455
|
+
return incoming.map((item, index) => {
|
|
456
|
+
const id = recordId(item);
|
|
457
|
+
const match = id
|
|
458
|
+
? existingArr.find((entry) => recordId(entry) === id)
|
|
459
|
+
: existingArr[index];
|
|
460
|
+
return restorePasswordPlaceholders(item, match);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
if (isRecord(incoming) && isRecord(existing)) {
|
|
464
|
+
return Object.fromEntries(
|
|
465
|
+
Object.entries(incoming).map(([key, value]) => [
|
|
466
|
+
key,
|
|
467
|
+
restorePasswordPlaceholders(value, existing[key]),
|
|
468
|
+
]),
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
return incoming;
|
|
472
|
+
};
|
package/src/lib/text.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Compact unread badge label. 3+ digits overflow a 20px circle; ∞ fits. */
|
|
2
|
+
export const formatBadgeCount = (count: number): string =>
|
|
3
|
+
count > 99 ? '∞' : String(count);
|
|
4
|
+
|
|
5
|
+
export const truncateText = (text?: string | null, maxLength: number = 15) => {
|
|
6
|
+
if (!text) return '';
|
|
7
|
+
if (text.length <= maxLength) return text;
|
|
8
|
+
return text.length > maxLength ? `${text.substring(0, maxLength)}...` : text;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const randomString = (
|
|
12
|
+
length: number,
|
|
13
|
+
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
|
14
|
+
) => {
|
|
15
|
+
let result = '';
|
|
16
|
+
const charactersLength = characters.length;
|
|
17
|
+
let counter = 0;
|
|
18
|
+
while (counter < length) {
|
|
19
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
20
|
+
counter += 1;
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const capitalizeFirstLetter = (string: string) =>
|
|
26
|
+
string.charAt(0).toUpperCase() + string.slice(1);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const FALLBACK_TIME_ZONE = 'UTC';
|
|
2
|
+
|
|
3
|
+
export const isIanaTimeZone = (
|
|
4
|
+
value: string | null | undefined,
|
|
5
|
+
): value is string => {
|
|
6
|
+
if (!value) return false;
|
|
7
|
+
try {
|
|
8
|
+
new Intl.DateTimeFormat('en-US', { timeZone: value }).format();
|
|
9
|
+
return true;
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const browserTimeZone = (): string => {
|
|
16
|
+
try {
|
|
17
|
+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
18
|
+
return isIanaTimeZone(timeZone) ? timeZone : FALLBACK_TIME_ZONE;
|
|
19
|
+
} catch {
|
|
20
|
+
return FALLBACK_TIME_ZONE;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const ianaTimeZones = (): string[] => {
|
|
25
|
+
const supportedValuesOf = (
|
|
26
|
+
Intl as typeof Intl & {
|
|
27
|
+
supportedValuesOf?: (key: 'timeZone') => string[];
|
|
28
|
+
}
|
|
29
|
+
).supportedValuesOf;
|
|
30
|
+
if (typeof supportedValuesOf === 'function') {
|
|
31
|
+
return supportedValuesOf('timeZone');
|
|
32
|
+
}
|
|
33
|
+
const timeZone = browserTimeZone();
|
|
34
|
+
return timeZone === FALLBACK_TIME_ZONE
|
|
35
|
+
? [FALLBACK_TIME_ZONE]
|
|
36
|
+
: [timeZone, FALLBACK_TIME_ZONE];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Profile timezone, then community timezone, then `fallback` (browser TZ
|
|
41
|
+
* when omitted; emails pass UTC so the worker host TZ is never used).
|
|
42
|
+
*/
|
|
43
|
+
export const resolveTimeZone = (
|
|
44
|
+
profileTimeZone?: string | null,
|
|
45
|
+
communityDefault?: string | null,
|
|
46
|
+
fallback?: string | null,
|
|
47
|
+
): string => {
|
|
48
|
+
if (isIanaTimeZone(profileTimeZone)) return profileTimeZone;
|
|
49
|
+
if (isIanaTimeZone(communityDefault)) return communityDefault;
|
|
50
|
+
if (isIanaTimeZone(fallback)) return fallback;
|
|
51
|
+
return browserTimeZone();
|
|
52
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeJwt,
|
|
3
|
+
decodeProtectedHeader,
|
|
4
|
+
importJWK,
|
|
5
|
+
jwtVerify,
|
|
6
|
+
type JWTPayload,
|
|
7
|
+
} from 'jose';
|
|
8
|
+
import { authorizationSchema } from '../types/auth';
|
|
9
|
+
import type { Scope } from '../types/models';
|
|
10
|
+
|
|
11
|
+
export const decodeJwtPayloadUnverified = (token: string): JWTPayload =>
|
|
12
|
+
decodeJwt(token);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Reads `scopes` from a JWT by validating the payload as {@link Authorization} (no signature verification).
|
|
16
|
+
* Session and access tokens embed that shape in the claim set.
|
|
17
|
+
*/
|
|
18
|
+
export const parseScopesFromJwt = (
|
|
19
|
+
token: string | undefined | null,
|
|
20
|
+
): Scope[] => {
|
|
21
|
+
if (!token?.trim()) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const parsed = authorizationSchema.safeParse(decodeJwt(token));
|
|
26
|
+
return parsed.success ? parsed.data.scopes : [];
|
|
27
|
+
} catch {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* True for JWTs that authenticate a service identity without a profile
|
|
34
|
+
* (e.g. jam observer / egress links). These cannot load `profiles/current`.
|
|
35
|
+
*/
|
|
36
|
+
export const isServiceOnlyJwt = (token: string | undefined | null): boolean => {
|
|
37
|
+
if (!token?.trim()) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const parsed = authorizationSchema.safeParse(decodeJwt(token));
|
|
42
|
+
if (!parsed.success) return false;
|
|
43
|
+
const { service, profile } = parsed.data.identities;
|
|
44
|
+
return !!service && !profile;
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* True for guest-pass JWTs: a profile identity with no account (1d TTL).
|
|
52
|
+
* These must not use the login JWT refresh path or `/accounts/current`.
|
|
53
|
+
*/
|
|
54
|
+
export const isAccountlessJwt = (token: string | undefined | null): boolean => {
|
|
55
|
+
if (!token?.trim()) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const parsed = authorizationSchema.safeParse(decodeJwt(token));
|
|
60
|
+
if (!parsed.success) return false;
|
|
61
|
+
const { profile, account, service } = parsed.data.identities;
|
|
62
|
+
return !!profile && !account && !service;
|
|
63
|
+
} catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* True when the token has a numeric `exp`, is not expired yet, and seconds until expiry is at least
|
|
70
|
+
* `minRemainingSeconds`. Non-finite or negative `minRemainingSeconds` yields false. Invalid or undecodable
|
|
71
|
+
* tokens return false.
|
|
72
|
+
*/
|
|
73
|
+
export const jwtHasRemainingValidityAtLeast = (
|
|
74
|
+
token: string | undefined | null,
|
|
75
|
+
minRemainingSeconds: number,
|
|
76
|
+
): boolean => {
|
|
77
|
+
if (
|
|
78
|
+
!Number.isFinite(minRemainingSeconds) ||
|
|
79
|
+
minRemainingSeconds < 0 ||
|
|
80
|
+
!token?.trim()
|
|
81
|
+
) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const { exp } = decodeJwt(token);
|
|
86
|
+
if (typeof exp !== 'number') {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const nowSec = Date.now() / 1000;
|
|
90
|
+
return exp - nowSec >= minRemainingSeconds;
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const verifyJwt = async (
|
|
97
|
+
token: string,
|
|
98
|
+
publicKey: string,
|
|
99
|
+
): Promise<JWTPayload | undefined> => {
|
|
100
|
+
const jwkPublicKey = await importJWK({
|
|
101
|
+
kty: 'OKP',
|
|
102
|
+
crv: 'Ed25519',
|
|
103
|
+
x: publicKey,
|
|
104
|
+
});
|
|
105
|
+
const verification = await jwtVerify(token, jwkPublicKey, {
|
|
106
|
+
algorithms: ['EdDSA'],
|
|
107
|
+
}).catch(() => undefined);
|
|
108
|
+
|
|
109
|
+
return verification?.payload;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const parseJwtKeyId = (token: string): string | undefined => {
|
|
113
|
+
try {
|
|
114
|
+
const { kid } = decodeProtectedHeader(token);
|
|
115
|
+
return typeof kid === 'string' ? kid : undefined;
|
|
116
|
+
} catch {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
};
|