@@ -7,14 +7,23 @@ import {
7
7
ise2eUpdateGroupKeyParamsPOST ,
8
8
isE2EProvideUsersGroupKeyProps ,
9
9
isE2EFetchUsersWaitingForGroupKeyProps ,
10
+ isE2EResetRoomKeyProps ,
10
11
} from '@rocket.chat/rest-typings' ;
12
+ import ExpiryMap from 'expiry-map' ;
11
13
import { Meteor } from 'meteor/meteor' ;
12
14
15
+ import { canAccessRoomIdAsync } from '../../../authorization/server/functions/canAccessRoom' ;
16
+ import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission' ;
13
17
import { handleSuggestedGroupKey } from '../../../e2e/server/functions/handleSuggestedGroupKey' ;
14
18
import { provideUsersSuggestedGroupKeys } from '../../../e2e/server/functions/provideUsersSuggestedGroupKeys' ;
19
+ import { resetRoomKey } from '../../../e2e/server/functions/resetRoomKey' ;
15
20
import { settings } from '../../../settings/server' ;
16
21
import { API } from '../api' ;
17
22
23
+ // After 10s the room lock will expire, meaning that if for some reason the process never completed
24
+ // The next reset will be available 10s after
25
+ const LockMap = new ExpiryMap < string , boolean > ( 10000 ) ;
26
+
18
27
API . v1 . addRoute (
19
28
'e2e.fetchMyKeys' ,
20
29
{
@@ -284,3 +293,36 @@ API.v1.addRoute(
284
293
} ,
285
294
} ,
286
295
) ;
296
+
297
+ // This should have permissions
298
+ API . v1 . addRoute (
299
+ 'e2e.resetRoomKey' ,
300
+ { authRequired : true , validateParams : isE2EResetRoomKeyProps } ,
301
+ {
302
+ async post ( ) {
303
+ const { rid, e2eKey, e2eKeyId } = this . bodyParams ;
304
+ if ( ! ( await hasPermissionAsync ( this . userId , 'toggle-room-e2e-encryption' , rid ) ) ) {
305
+ return API . v1 . unauthorized ( ) ;
306
+ }
307
+ if ( LockMap . has ( rid ) ) {
308
+ throw new Error ( 'error-e2e-key-reset-in-progress' ) ;
309
+ }
310
+
311
+ LockMap . set ( rid , true ) ;
312
+
313
+ if ( ! ( await canAccessRoomIdAsync ( rid , this . userId ) ) ) {
314
+ throw new Error ( 'error-not-allowed' ) ;
315
+ }
316
+
317
+ try {
318
+ await resetRoomKey ( rid , this . userId , e2eKey , e2eKeyId ) ;
319
+ return API . v1 . success ( ) ;
320
+ } catch ( e ) {
321
+ console . error ( e ) ;
322
+ return API . v1 . failure ( 'error-e2e-key-reset-failed' ) ;
323
+ } finally {
324
+ LockMap . delete ( rid ) ;
325
+ }
326
+ } ,
327
+ } ,
328
+ ) ;
0 commit comments