/
resetUserE2EKey.ts
77 lines (65 loc) · 2.12 KB
/
resetUserE2EKey.ts
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
import { Meteor } from 'meteor/meteor';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import type { IUser } from '@rocket.chat/core-typings';
import { isUserFederated } from '@rocket.chat/core-typings';
import { Users, Subscriptions } from '../../app/models/server';
import { settings } from '../../app/settings/server';
import * as Mailer from '../../app/mailer';
const sendResetNotitification = function (uid: string): void {
const user: IUser = Users.findOneById(uid, {});
if (!user) {
throw new Meteor.Error('invalid-user');
}
const language = user.language || settings.get('Language') || 'en';
const addresses = user.emails?.filter(({ verified }) => verified).map((e) => e.address);
if (!addresses?.length) {
return;
}
const t = (s: string): string => TAPi18n.__(s, { lng: language });
const text = `
${t('Your_e2e_key_has_been_reset')}
${t('E2E_Reset_Email_Content')}
`;
const html = `
<p>${t('Your_e2e_key_has_been_reset')}</p>
<p>${t('E2E_Reset_Email_Content')}</p>
`;
const from = settings.get('From_Email');
const subject = t('E2E_key_reset_email');
for (const address of addresses) {
Meteor.defer(() => {
try {
Mailer.send({
to: address,
from,
subject,
text,
html,
} as any);
} catch (error) {
throw new Meteor.Error(
'error-email-send-failed',
`Error trying to send email: ${error instanceof Error ? error.message : String(error)}`,
{
function: 'resetUserE2EEncriptionKey',
message: error instanceof Error ? error.message : String(error),
},
);
}
});
}
};
export function resetUserE2EEncriptionKey(uid: string, notifyUser: boolean): boolean {
if (notifyUser) {
sendResetNotitification(uid);
}
const user = Users.findOneById(uid, { fields: { federated: 1, username: 1 } });
if (isUserFederated(user)) {
throw new Meteor.Error('error-not-allowed', 'Federated Users cant have e2e encryption', { function: 'resetUserE2EEncriptionKey' });
}
Users.resetE2EKey(uid);
Subscriptions.resetUserE2EKey(uid);
// Force the user to logout, so that the keys can be generated again
Users.unsetLoginTokens(uid);
return true;
}