forked from DeepNotesApp/DeepNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroups.ts
More file actions
193 lines (167 loc) · 5.06 KB
/
Copy pathgroups.ts
File metadata and controls
193 lines (167 loc) · 5.06 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
import { userHasPermission } from '@deeplib/data';
import type { UserModel } from '@deeplib/db';
import { GroupMemberModel, GroupModel } from '@deeplib/db';
import type { GroupRolePermission } from '@deeplib/misc';
import type { DataTransaction } from '@stdlib/data';
import { TRPCError } from '@trpc/server';
import sodium from 'libsodium-wrappers-sumo';
import { once } from 'lodash';
import { z } from 'zod';
import { dataAbstraction } from '../data/data-abstraction';
import {
computePasswordHash,
decryptGroupRehashedPasswordHash,
encryptGroupRehashedPasswordHash,
} from '../utils/crypto';
export const GroupRoleEnum = z.enum([
'owner',
'admin',
'moderator',
'member',
'viewer',
]);
export const groupCreationSchema = once(() =>
z.object({
groupEncryptedName: z.instanceof(Uint8Array),
groupPasswordHash: z.instanceof(Uint8Array).optional(),
groupIsPublic: z.boolean(),
groupAccessKeyring: z.instanceof(Uint8Array),
groupEncryptedInternalKeyring: z.instanceof(Uint8Array),
groupEncryptedContentKeyring: z.instanceof(Uint8Array),
groupPublicKeyring: z.instanceof(Uint8Array),
groupEncryptedPrivateKeyring: z.instanceof(Uint8Array),
groupOwnerEncryptedName: z.instanceof(Uint8Array),
}),
);
export type GroupCreationSchema = z.infer<
ReturnType<typeof groupCreationSchema>
>;
export async function createGroup(
input: {
userId: string;
groupId: string;
groupMainPageId: string;
groupIsPersonal: boolean;
dtrx?: DataTransaction;
} & GroupCreationSchema,
) {
await dataAbstraction().insert(
'group',
input.groupId,
{
id: input.groupId,
encrypted_name: input.groupEncryptedName ?? new Uint8Array(),
main_page_id: input.groupMainPageId,
encrypted_rehashed_password_hash:
input.groupPasswordHash != null
? encryptGroupRehashedPasswordHash(
computePasswordHash(input.groupPasswordHash),
)
: undefined,
access_keyring: input.groupIsPublic
? input.groupAccessKeyring
: undefined,
encrypted_content_keyring: input.groupEncryptedContentKeyring,
user_id: input.groupIsPersonal ? input.userId : undefined,
public_keyring: input.groupPublicKeyring,
encrypted_private_keyring: input.groupEncryptedPrivateKeyring,
},
{ dtrx: input.dtrx },
);
await dataAbstraction().insert(
'group-member',
`${input.groupId}:${input.userId}`,
{
group_id: input.groupId,
user_id: input.userId,
role: 'owner',
encrypted_access_keyring: input.groupIsPublic
? undefined
: input.groupAccessKeyring,
encrypted_internal_keyring: input.groupEncryptedInternalKeyring,
encrypted_name: input.groupOwnerEncryptedName ?? new Uint8Array(),
},
{ dtrx: input.dtrx },
);
}
export async function assertCorrectGroupPassword(input: {
groupId: string;
groupPasswordHash: Uint8Array;
}) {
const group = await GroupModel.query()
.findById(input.groupId)
.select('encrypted_rehashed_password_hash');
if (group == null) {
throw new TRPCError({
message: 'Group not found.',
code: 'NOT_FOUND',
});
}
if (group.encrypted_rehashed_password_hash == null) {
throw new TRPCError({
message: 'This group is not password protected.',
code: 'BAD_REQUEST',
});
}
if (
!sodium.crypto_pwhash_str_verify(
decryptGroupRehashedPasswordHash(group.encrypted_rehashed_password_hash),
input.groupPasswordHash,
)
) {
throw new TRPCError({
message: 'Group password is incorrect.',
code: 'BAD_REQUEST',
});
}
}
export async function getGroupManagers(
groupId: string,
extraUserIds?: string[],
): Promise<{ userId: string; publicKeyring: Uint8Array }[]> {
return (
(await GroupMemberModel.query()
.leftJoin('users', 'users.id', 'group_members.user_id')
.where('group_id', groupId)
.whereIn('group_members.role', ['owner', 'admin', 'moderator'])
.orWhereIn('users.id', extraUserIds ?? [])
.select('users.id', 'users.public_keyring')) as unknown as UserModel[]
).map((groupMember) => ({
userId: groupMember.id,
publicKeyring: groupMember.public_keyring,
}));
}
export async function getGroupMembers(
groupId: string,
extraUserIds?: string[],
): Promise<{ userId: string; publicKeyring: Uint8Array }[]> {
return (
(await GroupMemberModel.query()
.leftJoin('users', 'users.id', 'group_members.user_id')
.where('group_id', groupId)
.orWhereIn('users.id', extraUserIds ?? [])
.select('users.id', 'users.public_keyring')) as unknown as UserModel[]
).map((groupMember) => ({
userId: groupMember.id,
publicKeyring: groupMember.public_keyring,
}));
}
export async function assertSufficientGroupPermissions(input: {
userId: any;
groupId: any;
permission: GroupRolePermission;
}) {
if (
!(await userHasPermission(
dataAbstraction(),
input.userId,
input.groupId,
input.permission,
))
) {
throw new TRPCError({
code: 'FORBIDDEN',
message: 'Insufficient permissions.',
});
}
}