@openpeepshq/common 0.1.46 → 0.3.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.d.ts +37 -0
- package/dist/lib/markdownHelpers.js +151 -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 +290 -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,220 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GroupWithMeta,
|
|
3
|
+
InviteLinkWithMeta,
|
|
4
|
+
ProfileSettings,
|
|
5
|
+
ProfileWithMeta,
|
|
6
|
+
PublicProfile,
|
|
7
|
+
StripeSubscriptionData,
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
const containsIgnoreCase = (candidate: string, query: string) =>
|
|
11
|
+
candidate.toLowerCase().indexOf(query.toLowerCase()) > -1;
|
|
12
|
+
|
|
13
|
+
export const PROFILE_DISPLAY_NAME_MAX_LENGTH = 30;
|
|
14
|
+
|
|
15
|
+
/** Public sentinel shown for soft-deleted authors (API + clients). */
|
|
16
|
+
export const DELETED_AUTHOR_DISPLAY_NAME = 'Deleted author';
|
|
17
|
+
export const DELETED_AUTHOR_HANDLE = 'deleted';
|
|
18
|
+
|
|
19
|
+
export const clampProfileDisplayName = (
|
|
20
|
+
displayName: string | undefined | null,
|
|
21
|
+
): string | undefined => {
|
|
22
|
+
if (!displayName) return undefined;
|
|
23
|
+
const clamped = displayName.slice(0, PROFILE_DISPLAY_NAME_MAX_LENGTH);
|
|
24
|
+
return clamped || undefined;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const isDeletedProfile = (
|
|
28
|
+
profile: { deletedAt?: string | null } | undefined | null,
|
|
29
|
+
): boolean => !!profile?.deletedAt;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Strip PII from a soft-deleted profile for public API / UI surfaces.
|
|
33
|
+
* Keeps id + timestamps (including deletedAt) so clients can detect the state.
|
|
34
|
+
*/
|
|
35
|
+
export const toPublicDeletedProfile = <T extends PublicProfile>(
|
|
36
|
+
profile: T,
|
|
37
|
+
): T =>
|
|
38
|
+
({
|
|
39
|
+
...profile,
|
|
40
|
+
displayName: DELETED_AUTHOR_DISPLAY_NAME,
|
|
41
|
+
handle: DELETED_AUTHOR_HANDLE,
|
|
42
|
+
avatar: null,
|
|
43
|
+
header: null,
|
|
44
|
+
bio: undefined,
|
|
45
|
+
location: undefined,
|
|
46
|
+
locked: undefined,
|
|
47
|
+
bot: undefined,
|
|
48
|
+
discoverable: undefined,
|
|
49
|
+
memberships: [],
|
|
50
|
+
fields: undefined,
|
|
51
|
+
profileStats: undefined,
|
|
52
|
+
pinnedPostId: undefined,
|
|
53
|
+
roles: [],
|
|
54
|
+
}) as T;
|
|
55
|
+
|
|
56
|
+
export const anonymizeProfileIfDeleted = <T extends PublicProfile>(
|
|
57
|
+
profile: T | undefined | null,
|
|
58
|
+
): T | undefined => {
|
|
59
|
+
if (!profile) return undefined;
|
|
60
|
+
return isDeletedProfile(profile) ? toPublicDeletedProfile(profile) : profile;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Tombstone for internal `ProfileWithMeta` surfaces (e.g. moderation reports).
|
|
65
|
+
* Built explicitly rather than by spreading the original so no PII of the
|
|
66
|
+
* deleted account (email/controllers, social graph, time zone, guest data)
|
|
67
|
+
* can leak, while still satisfying `profileWithMetaSchema` — unlike
|
|
68
|
+
* {@link toPublicDeletedProfile}, whose stripped shape is only valid for the
|
|
69
|
+
* looser `publicProfileSchema`.
|
|
70
|
+
*/
|
|
71
|
+
export const toDeletedProfileWithMeta = (
|
|
72
|
+
profile: ProfileWithMeta,
|
|
73
|
+
): ProfileWithMeta => ({
|
|
74
|
+
id: profile.id,
|
|
75
|
+
type: profile.type,
|
|
76
|
+
createdAt: profile.createdAt,
|
|
77
|
+
updatedAt: profile.updatedAt,
|
|
78
|
+
deletedAt: profile.deletedAt,
|
|
79
|
+
handle: DELETED_AUTHOR_HANDLE,
|
|
80
|
+
displayName: DELETED_AUTHOR_DISPLAY_NAME,
|
|
81
|
+
avatar: null,
|
|
82
|
+
header: null,
|
|
83
|
+
roles: [],
|
|
84
|
+
followers: [],
|
|
85
|
+
following: [],
|
|
86
|
+
controllers: [],
|
|
87
|
+
memberships: [],
|
|
88
|
+
profileStats: { followersCount: 0, followingCount: 0 },
|
|
89
|
+
blockingIds: [],
|
|
90
|
+
blockedByIds: [],
|
|
91
|
+
followedHashtags: [],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/** Tombstone a `ProfileWithMeta` only when it is soft-deleted. */
|
|
95
|
+
export const tombstoneProfileWithMetaIfDeleted = (
|
|
96
|
+
profile: ProfileWithMeta | undefined,
|
|
97
|
+
): ProfileWithMeta | undefined =>
|
|
98
|
+
profile && isDeletedProfile(profile)
|
|
99
|
+
? toDeletedProfileWithMeta(profile)
|
|
100
|
+
: profile;
|
|
101
|
+
|
|
102
|
+
export const profileName = (profile?: PublicProfile) =>
|
|
103
|
+
profile?.displayName || profile?.handle;
|
|
104
|
+
|
|
105
|
+
export const matchesQuery = (
|
|
106
|
+
profile: PublicProfile | GroupWithMeta | undefined,
|
|
107
|
+
query: string,
|
|
108
|
+
queryOptions = { handle: true, displayName: true },
|
|
109
|
+
) =>
|
|
110
|
+
!!(
|
|
111
|
+
profile &&
|
|
112
|
+
(query.startsWith('@')
|
|
113
|
+
? profile.handle && containsIgnoreCase(profile.handle, query.substring(1))
|
|
114
|
+
: (queryOptions.handle &&
|
|
115
|
+
profile.handle &&
|
|
116
|
+
containsIgnoreCase(profile.handle, query)) ||
|
|
117
|
+
(queryOptions.displayName &&
|
|
118
|
+
profile.displayName &&
|
|
119
|
+
containsIgnoreCase(profile.displayName, query)))
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
export const inviteLinkMatchesQuery = (
|
|
123
|
+
inviteLink: InviteLinkWithMeta,
|
|
124
|
+
query: string,
|
|
125
|
+
queryOptions = { handle: true, displayName: true, slug: true, group: true },
|
|
126
|
+
) => {
|
|
127
|
+
const profile = inviteLink.profile as PublicProfile;
|
|
128
|
+
if (query.startsWith('@')) {
|
|
129
|
+
return !!(
|
|
130
|
+
profile?.handle && containsIgnoreCase(profile.handle, query.substring(1))
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const groupMatches =
|
|
134
|
+
queryOptions.group &&
|
|
135
|
+
inviteLink.groups.some(
|
|
136
|
+
(group) =>
|
|
137
|
+
containsIgnoreCase(group.handle, query) ||
|
|
138
|
+
(group.displayName && containsIgnoreCase(group.displayName, query)),
|
|
139
|
+
);
|
|
140
|
+
return !!(
|
|
141
|
+
groupMatches ||
|
|
142
|
+
(profile &&
|
|
143
|
+
((queryOptions.handle &&
|
|
144
|
+
profile.handle &&
|
|
145
|
+
containsIgnoreCase(profile.handle, query)) ||
|
|
146
|
+
(queryOptions.displayName &&
|
|
147
|
+
profile.displayName &&
|
|
148
|
+
containsIgnoreCase(profile.displayName, query)) ||
|
|
149
|
+
(queryOptions.slug &&
|
|
150
|
+
inviteLink.slug &&
|
|
151
|
+
containsIgnoreCase(inviteLink.slug, query))))
|
|
152
|
+
);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const sortProfiles = (profiles: PublicProfile[]): PublicProfile[] =>
|
|
156
|
+
profiles?.sort((a, b) =>
|
|
157
|
+
(profileName(a) as string).localeCompare(profileName(b) as string),
|
|
158
|
+
) ?? [];
|
|
159
|
+
|
|
160
|
+
export const isStripeActive = (
|
|
161
|
+
sub: StripeSubscriptionData | undefined | null,
|
|
162
|
+
) => sub?.status === 'active' || sub?.status === 'trialing';
|
|
163
|
+
|
|
164
|
+
export const getStripeCustomerId = (profileSettings: ProfileSettings) =>
|
|
165
|
+
profileSettings?.stripeSettings?.customerId;
|
|
166
|
+
|
|
167
|
+
export const isOwnerProfile = (profile: ProfileWithMeta) => {
|
|
168
|
+
return profile?.roles.some((role) => role.key === 'owner');
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
type BlockIdProfile = {
|
|
172
|
+
blockingIds?: string[] | null;
|
|
173
|
+
blockedByIds?: string[] | null;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export const blockedPairIds = (profile?: BlockIdProfile | null): string[] => [
|
|
177
|
+
...new Set([
|
|
178
|
+
...(profile?.blockingIds ?? []),
|
|
179
|
+
...(profile?.blockedByIds ?? []),
|
|
180
|
+
]),
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
export const isBlockedPair = (
|
|
184
|
+
profile: BlockIdProfile | undefined | null,
|
|
185
|
+
otherId: string | undefined | null,
|
|
186
|
+
): boolean => !!otherId && blockedPairIds(profile).includes(otherId);
|
|
187
|
+
|
|
188
|
+
export const viewerBlockedTarget = (
|
|
189
|
+
profile: BlockIdProfile | undefined | null,
|
|
190
|
+
otherId: string | undefined | null,
|
|
191
|
+
): boolean => !!otherId && (profile?.blockingIds ?? []).includes(otherId);
|
|
192
|
+
|
|
193
|
+
export const targetBlockedViewer = (
|
|
194
|
+
profile: BlockIdProfile | undefined | null,
|
|
195
|
+
otherId: string | undefined | null,
|
|
196
|
+
): boolean => !!otherId && (profile?.blockedByIds ?? []).includes(otherId);
|
|
197
|
+
|
|
198
|
+
export const sharesControllingAccount = (
|
|
199
|
+
a: Pick<ProfileWithMeta, 'controllers'>,
|
|
200
|
+
b: Pick<ProfileWithMeta, 'controllers'>,
|
|
201
|
+
): boolean => {
|
|
202
|
+
const otherIds = new Set((b.controllers ?? []).map((account) => account.id));
|
|
203
|
+
return (a.controllers ?? []).some((account) => otherIds.has(account.id));
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export const toBlockedProfileStub = (
|
|
207
|
+
profile: Pick<
|
|
208
|
+
ProfileWithMeta,
|
|
209
|
+
'id' | 'type' | 'createdAt' | 'updatedAt' | 'handle' | 'displayName'
|
|
210
|
+
>,
|
|
211
|
+
): PublicProfile =>
|
|
212
|
+
({
|
|
213
|
+
id: profile.id,
|
|
214
|
+
type: profile.type,
|
|
215
|
+
createdAt: profile.createdAt,
|
|
216
|
+
updatedAt: profile.updatedAt,
|
|
217
|
+
handle: profile.handle,
|
|
218
|
+
displayName: profile.displayName,
|
|
219
|
+
blockedByMe: true,
|
|
220
|
+
}) as PublicProfile;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { PushSubscription, PushSubscriptionData } from '../types';
|
|
2
|
+
|
|
3
|
+
export const pushSubscriptionDeviceName = (
|
|
4
|
+
subscription: PushSubscription,
|
|
5
|
+
): string | undefined =>
|
|
6
|
+
'deviceName' in subscription ? subscription.deviceName : undefined;
|
|
7
|
+
|
|
8
|
+
/** Identity used to send a push. Duplicate keys mean duplicate notifications. */
|
|
9
|
+
export const pushSubscriptionDeliveryKey = (
|
|
10
|
+
subscription: PushSubscriptionData,
|
|
11
|
+
): string => {
|
|
12
|
+
switch (subscription.type) {
|
|
13
|
+
case 'web':
|
|
14
|
+
return `web:${subscription.endpoint}`;
|
|
15
|
+
case 'webhook':
|
|
16
|
+
return `webhook:${subscription.url}`;
|
|
17
|
+
case 'apn':
|
|
18
|
+
return `apn:${subscription.apnToken}`;
|
|
19
|
+
case 'fcm':
|
|
20
|
+
return `fcm:${subscription.fcmToken}`;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const pushSubscriptionEndpoint = (
|
|
25
|
+
subscription: PushSubscription,
|
|
26
|
+
): string | undefined =>
|
|
27
|
+
'endpoint' in subscription ? subscription.endpoint : undefined;
|
|
28
|
+
|
|
29
|
+
export const pushSubscriptionIsMobile = (
|
|
30
|
+
subscription: PushSubscription,
|
|
31
|
+
): boolean =>
|
|
32
|
+
pushSubscriptionDeviceName(subscription)
|
|
33
|
+
?.toLowerCase()
|
|
34
|
+
.match(/phone|android|mobile|ios/) != null;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const AP_LIKE_REACTION = '👍';
|
|
2
|
+
export const AP_DISLIKE_REACTION = '👎';
|
|
3
|
+
|
|
4
|
+
export const activityPubActivityForReaction = (
|
|
5
|
+
reaction: string,
|
|
6
|
+
): 'Like' | 'Dislike' | null => {
|
|
7
|
+
if (reaction === AP_LIKE_REACTION) return 'Like';
|
|
8
|
+
if (reaction === AP_DISLIKE_REACTION) return 'Dislike';
|
|
9
|
+
return null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type ReactionLogEntry = {
|
|
13
|
+
type: string;
|
|
14
|
+
createdAt: string;
|
|
15
|
+
data?: unknown;
|
|
16
|
+
profile: { id: string };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const reactionFromData = (data: unknown): string | undefined => {
|
|
20
|
+
if (!data || typeof data !== 'object' || !('reaction' in data)) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
const reaction = data.reaction;
|
|
24
|
+
return typeof reaction === 'string' && reaction.length > 0
|
|
25
|
+
? reaction
|
|
26
|
+
: undefined;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Latest reaction/unreaction per (profile, emoji) wins. */
|
|
30
|
+
export const currentReactionsFromEntries = <T extends ReactionLogEntry>(
|
|
31
|
+
entries: readonly T[],
|
|
32
|
+
): Array<{ reaction: string; profile: T['profile'] }> => {
|
|
33
|
+
const latest = new Map<string, { entry: T; reaction: string }>();
|
|
34
|
+
const sorted = [...entries].filter(
|
|
35
|
+
(entry) => entry.type === 'reaction' || entry.type === 'unreaction',
|
|
36
|
+
);
|
|
37
|
+
sorted.sort((a, b) => (a.createdAt < b.createdAt ? -1 : 1));
|
|
38
|
+
for (const entry of sorted) {
|
|
39
|
+
const reaction = reactionFromData(entry.data);
|
|
40
|
+
if (!reaction) continue;
|
|
41
|
+
latest.set(`${entry.profile.id}\0${reaction}`, { entry, reaction });
|
|
42
|
+
}
|
|
43
|
+
return [...latest.values()]
|
|
44
|
+
.filter(({ entry }) => entry.type === 'reaction')
|
|
45
|
+
.map(({ entry, reaction }) => ({
|
|
46
|
+
reaction,
|
|
47
|
+
profile: entry.profile,
|
|
48
|
+
}));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const isReactionHistoryEntry = (type: string) =>
|
|
52
|
+
type === 'reaction' || type === 'unreaction';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** True when the reporter is the reported profile or authored a reported post. */
|
|
2
|
+
export const isSelfReport = (
|
|
3
|
+
reporterId: string,
|
|
4
|
+
reportedProfileId: string,
|
|
5
|
+
postAuthorIds: readonly string[],
|
|
6
|
+
): boolean =>
|
|
7
|
+
reporterId === reportedProfileId || postAuthorIds.includes(reporterId);
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthorizationData,
|
|
3
|
+
PublicPost,
|
|
4
|
+
Resource,
|
|
5
|
+
Scope,
|
|
6
|
+
ScopeLevel,
|
|
7
|
+
} from '../types';
|
|
8
|
+
|
|
9
|
+
const includesScopeLevel = (
|
|
10
|
+
scopeLevel: ScopeLevel,
|
|
11
|
+
requiredScopeLevel: ScopeLevel,
|
|
12
|
+
) => {
|
|
13
|
+
switch (requiredScopeLevel) {
|
|
14
|
+
case 'write':
|
|
15
|
+
return scopeLevel === 'write' || scopeLevel === 'admin';
|
|
16
|
+
case 'read':
|
|
17
|
+
return (
|
|
18
|
+
scopeLevel === 'read' ||
|
|
19
|
+
scopeLevel === 'write' ||
|
|
20
|
+
scopeLevel === 'admin'
|
|
21
|
+
);
|
|
22
|
+
case 'admin':
|
|
23
|
+
return scopeLevel === 'admin';
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const calculateRequiredScopeLevel = (
|
|
28
|
+
requiredCapabilities: string[],
|
|
29
|
+
): ScopeLevel => {
|
|
30
|
+
return requiredCapabilities.reduce((acc: ScopeLevel, capability: string) => {
|
|
31
|
+
if (acc === 'admin') {
|
|
32
|
+
return 'admin';
|
|
33
|
+
}
|
|
34
|
+
if (
|
|
35
|
+
capability.endsWith('-create') ||
|
|
36
|
+
capability.endsWith('-update') ||
|
|
37
|
+
capability.endsWith('-delete')
|
|
38
|
+
) {
|
|
39
|
+
return 'write';
|
|
40
|
+
} else if (!capability.endsWith('-read')) {
|
|
41
|
+
return 'admin';
|
|
42
|
+
}
|
|
43
|
+
return acc;
|
|
44
|
+
}, 'read' as ScopeLevel);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const calculateRequiredScope = ({
|
|
48
|
+
resource,
|
|
49
|
+
requiredCapabilities,
|
|
50
|
+
}: {
|
|
51
|
+
resource: Resource;
|
|
52
|
+
requiredCapabilities: string[];
|
|
53
|
+
}): Scope => ({
|
|
54
|
+
scopeLevel: calculateRequiredScopeLevel(requiredCapabilities),
|
|
55
|
+
resource,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export const scopeMatches = ({
|
|
59
|
+
scopes,
|
|
60
|
+
requiredScope,
|
|
61
|
+
}: {
|
|
62
|
+
scopes?: Scope[];
|
|
63
|
+
requiredScope: Scope;
|
|
64
|
+
}) =>
|
|
65
|
+
Array.isArray(scopes) &&
|
|
66
|
+
!!scopes.find(
|
|
67
|
+
(s) =>
|
|
68
|
+
(!s.scopeLevel ||
|
|
69
|
+
!requiredScope.scopeLevel ||
|
|
70
|
+
includesScopeLevel(s.scopeLevel, requiredScope.scopeLevel)) &&
|
|
71
|
+
(!s.resource.id ||
|
|
72
|
+
s.resource.id === requiredScope.resource.id ||
|
|
73
|
+
s.resource.id === '*' ||
|
|
74
|
+
requiredScope.resource.id === '*') &&
|
|
75
|
+
(s.resource.type === requiredScope.resource.type ||
|
|
76
|
+
s.resource.type === '*' ||
|
|
77
|
+
requiredScope.resource.type === '*'),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
export const getPublicPostReadScope = (postId: string): Scope => ({
|
|
81
|
+
scopeLevel: 'read',
|
|
82
|
+
resource: { type: 'posts', id: postId },
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Posts with `public` visibility — and posts in groups that grant the needed
|
|
87
|
+
* *-read caps to `none` — are readable without auth; grant read scope for
|
|
88
|
+
* that post id (same pattern as {@link withPublicGroupReadScopes}).
|
|
89
|
+
*/
|
|
90
|
+
export const withPublicPostReadScopes = (
|
|
91
|
+
authData: AuthorizationData,
|
|
92
|
+
post: Pick<PublicPost, 'id' | 'visibility'> & {
|
|
93
|
+
/** Only the group's `none` capabilities matter for the public check. */
|
|
94
|
+
group?: { capabilities?: { none?: { add?: string[] } } | null } | null;
|
|
95
|
+
},
|
|
96
|
+
neededCapabilities: string[],
|
|
97
|
+
): AuthorizationData => {
|
|
98
|
+
if (!neededCapabilities.every((c) => c.endsWith('-read'))) {
|
|
99
|
+
return authData;
|
|
100
|
+
}
|
|
101
|
+
const publicGroupPost =
|
|
102
|
+
post.visibility === 'group' &&
|
|
103
|
+
neededCapabilities.every(
|
|
104
|
+
(c) => post.group?.capabilities?.none?.add?.includes(c) ?? false,
|
|
105
|
+
);
|
|
106
|
+
if (post.visibility !== 'public' && !publicGroupPost) {
|
|
107
|
+
return authData;
|
|
108
|
+
}
|
|
109
|
+
const publicReadScope = getPublicPostReadScope(post.id);
|
|
110
|
+
const scopes = authData.scopes ?? [];
|
|
111
|
+
if (scopeMatches({ scopes, requiredScope: publicReadScope })) {
|
|
112
|
+
return authData;
|
|
113
|
+
}
|
|
114
|
+
return { ...authData, scopes: [...scopes, publicReadScope] };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const getPublicGroupReadScope = (groupId: string): Scope => ({
|
|
118
|
+
scopeLevel: 'read',
|
|
119
|
+
resource: { type: 'groups', id: groupId },
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Groups that grant the needed *-read caps to relationship `none` are readable
|
|
124
|
+
* without auth (same pattern as {@link withPublicPostReadScopes}).
|
|
125
|
+
*/
|
|
126
|
+
export const withPublicGroupReadScopes = (
|
|
127
|
+
authData: AuthorizationData,
|
|
128
|
+
group: { id: string; capabilities?: { none?: { add?: string[] } } | null },
|
|
129
|
+
neededCapabilities: string[],
|
|
130
|
+
): AuthorizationData => {
|
|
131
|
+
if (!neededCapabilities.every((c) => c.endsWith('-read'))) {
|
|
132
|
+
return authData;
|
|
133
|
+
}
|
|
134
|
+
const noneAdd = group.capabilities?.none?.add ?? [];
|
|
135
|
+
if (!neededCapabilities.every((c) => noneAdd.includes(c))) {
|
|
136
|
+
return authData;
|
|
137
|
+
}
|
|
138
|
+
const publicReadScope = getPublicGroupReadScope(group.id);
|
|
139
|
+
const scopes = authData.scopes ?? [];
|
|
140
|
+
if (scopeMatches({ scopes, requiredScope: publicReadScope })) {
|
|
141
|
+
return authData;
|
|
142
|
+
}
|
|
143
|
+
return { ...authData, scopes: [...scopes, publicReadScope] };
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export const getPublicProfileReadScope = (profileId: string): Scope => ({
|
|
147
|
+
scopeLevel: 'read',
|
|
148
|
+
resource: { type: 'profiles', id: profileId },
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Profiles whose capabilities config grants the needed *-read caps to
|
|
153
|
+
* relationship `none` are readable without auth (same pattern as
|
|
154
|
+
* {@link withPublicGroupReadScopes}). Community-wide `publicContent` is
|
|
155
|
+
* still enforced by `ensureAccess` on the profile endpoints.
|
|
156
|
+
*/
|
|
157
|
+
export const withPublicProfileReadScopes = (
|
|
158
|
+
authData: AuthorizationData,
|
|
159
|
+
profile: { id: string },
|
|
160
|
+
neededCapabilities: string[],
|
|
161
|
+
config: { profile?: { none?: { add?: string[] } } | null },
|
|
162
|
+
): AuthorizationData => {
|
|
163
|
+
if (!neededCapabilities.every((c) => c.endsWith('-read'))) {
|
|
164
|
+
return authData;
|
|
165
|
+
}
|
|
166
|
+
const noneAdd = config.profile?.none?.add ?? [];
|
|
167
|
+
if (!neededCapabilities.every((c) => noneAdd.includes(c))) {
|
|
168
|
+
return authData;
|
|
169
|
+
}
|
|
170
|
+
const publicReadScope = getPublicProfileReadScope(profile.id);
|
|
171
|
+
const scopes = authData.scopes ?? [];
|
|
172
|
+
if (scopeMatches({ scopes, requiredScope: publicReadScope })) {
|
|
173
|
+
return authData;
|
|
174
|
+
}
|
|
175
|
+
return { ...authData, scopes: [...scopes, publicReadScope] };
|
|
176
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { CoreConfig, ServerInfo } from '../types';
|
|
2
|
+
|
|
3
|
+
/** Query param that keeps the native password form when `sso.onlySSO` is set. */
|
|
4
|
+
export const LOCAL_LOGIN_PARAM = 'local';
|
|
5
|
+
|
|
6
|
+
export type SsoLoginDestination = {
|
|
7
|
+
href: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
testId: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const isAllowedSsoLoginUrl = (url: string): boolean => {
|
|
13
|
+
const trimmed = url.trim();
|
|
14
|
+
if (!trimmed) return false;
|
|
15
|
+
if (trimmed.startsWith('/') && !trimmed.startsWith('//')) return true;
|
|
16
|
+
try {
|
|
17
|
+
const parsed = new URL(trimmed);
|
|
18
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const oidcAuthorizePath = (id: string): string =>
|
|
25
|
+
`/api/openpeeps/core/v1/sso/oidc/${encodeURIComponent(id)}/authorize`;
|
|
26
|
+
|
|
27
|
+
export const ssoLoginDestinations = (
|
|
28
|
+
sso: Pick<NonNullable<ServerInfo['sso']>, 'generic' | 'oidc'> | undefined,
|
|
29
|
+
): SsoLoginDestination[] => {
|
|
30
|
+
const destinations: SsoLoginDestination[] = [];
|
|
31
|
+
for (const provider of sso?.generic ?? []) {
|
|
32
|
+
destinations.push({
|
|
33
|
+
href: provider.loginLink,
|
|
34
|
+
name: provider.name,
|
|
35
|
+
testId: `auth-login-generic-${provider.id}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
for (const provider of sso?.oidc ?? []) {
|
|
39
|
+
destinations.push({
|
|
40
|
+
href: oidcAuthorizePath(provider.id),
|
|
41
|
+
name: provider.name,
|
|
42
|
+
testId: `auth-login-oidc-${provider.id}`,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return destinations;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type OnlySsoView =
|
|
49
|
+
| { mode: 'form' }
|
|
50
|
+
| { mode: 'redirect'; href: string }
|
|
51
|
+
| { mode: 'chooser'; destinations: SsoLoginDestination[] };
|
|
52
|
+
|
|
53
|
+
export const resolveOnlySsoView = (
|
|
54
|
+
sso: ServerInfo['sso'],
|
|
55
|
+
searchParams: { has: (name: string) => boolean },
|
|
56
|
+
): OnlySsoView => {
|
|
57
|
+
if (!sso?.onlySSO || searchParams.has(LOCAL_LOGIN_PARAM)) {
|
|
58
|
+
return { mode: 'form' };
|
|
59
|
+
}
|
|
60
|
+
const destinations = ssoLoginDestinations(sso);
|
|
61
|
+
const [only] = destinations;
|
|
62
|
+
if (only && destinations.length === 1) {
|
|
63
|
+
return { mode: 'redirect', href: only.href };
|
|
64
|
+
}
|
|
65
|
+
return { mode: 'chooser', destinations };
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const publicSsoInfo = (sso: CoreConfig['sso']): ServerInfo['sso'] => {
|
|
69
|
+
const oidc = sso.oidc.map((provider) => ({
|
|
70
|
+
id: provider.id,
|
|
71
|
+
name: provider.name,
|
|
72
|
+
}));
|
|
73
|
+
const generic = sso.generic.flatMap((provider) => {
|
|
74
|
+
const loginLink = provider.loginLink?.trim();
|
|
75
|
+
if (!loginLink || !isAllowedSsoLoginUrl(loginLink)) return [];
|
|
76
|
+
return [
|
|
77
|
+
{
|
|
78
|
+
id: provider.id,
|
|
79
|
+
name: provider.name,
|
|
80
|
+
loginLink,
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
});
|
|
84
|
+
const onlySSO = !!sso.onlySSO;
|
|
85
|
+
if (oidc.length === 0 && generic.length === 0 && !onlySSO) {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
oidc,
|
|
90
|
+
generic,
|
|
91
|
+
...(onlySSO ? { onlySSO: true } : {}),
|
|
92
|
+
};
|
|
93
|
+
};
|