Skip to content

Commit bc10793

Browse files
committed
refactor(api): update twoFactorRequired and related methods to improve type safety and ensure consistent handling of user context
1 parent 461c8df commit bc10793

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

apps/meteor/app/2fa/server/twoFactorRequired.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import { Meteor } from 'meteor/meteor';
33
import type { ITwoFactorOptions } from './code/index';
44
import { checkCodeForUser } from './code/index';
55

6-
export function twoFactorRequired<TFunction extends (this: Meteor.MethodThisType & { token: string }, ...args: any[]) => any>(
7-
fn: TFunction,
6+
export const twoFactorRequired = <TFunction extends (this: any, ...args: any) => any>(
7+
fn: ThisParameterType<TFunction> extends Meteor.MethodThisType
8+
? TFunction
9+
: (this: Meteor.MethodThisType, ...args: Parameters<TFunction>) => ReturnType<TFunction>,
810
options?: ITwoFactorOptions,
9-
): (
10-
this: Meteor.MethodThisType & {
11-
token: string;
12-
},
13-
...args: Parameters<TFunction>
14-
) => Promise<ReturnType<TFunction>> {
15-
return async function (this: Meteor.MethodThisType & { token: string }, ...args: Parameters<TFunction>): Promise<ReturnType<TFunction>> {
11+
) =>
12+
async function (this, ...args) {
1613
if (!this.userId) {
1714
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'twoFactorRequired' });
1815
}
@@ -40,5 +37,4 @@ export function twoFactorRequired<TFunction extends (this: Meteor.MethodThisType
4037
}
4138

4239
return fn.apply(this, args);
43-
};
44-
}
40+
} as (this: ThisParameterType<TFunction>, ...args: Parameters<TFunction>) => ReturnType<TFunction>;

apps/meteor/app/api/server/v1/users.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { regeneratePersonalAccessTokenOfUser } from '../../../../imports/persona
3131
import { removePersonalAccessTokenOfUser } from '../../../../imports/personal-access-tokens/server/api/methods/removeToken';
3232
import { UserChangedAuditStore } from '../../../../server/lib/auditServerEvents/userChanged';
3333
import { i18n } from '../../../../server/lib/i18n';
34-
import { removeOtherTokens } from '../../../../server/lib/removeOtherTokens';
3534
import { resetUserE2EEncriptionKey } from '../../../../server/lib/resetUserE2EKey';
3635
import { registerUser } from '../../../../server/methods/registerUser';
3736
import { requestDataDownload } from '../../../../server/methods/requestDataDownload';
@@ -188,7 +187,7 @@ API.v1.addRoute(
188187
};
189188

190189
await executeSaveUserProfile.call(
191-
this as unknown as Meteor.MethodThisType,
190+
this as unknown as Meteor.MethodThisType & { token: string },
192191
this.user,
193192
userData,
194193
this.bodyParams.customFields,
@@ -1239,7 +1238,7 @@ API.v1.addRoute(
12391238
{ authRequired: true },
12401239
{
12411240
async post() {
1242-
return API.v1.success(await removeOtherTokens(this.userId, this.connection.id));
1241+
return API.v1.success(await Users.removeNonLoginTokensExcept(this.userId, this.token));
12431242
},
12441243
},
12451244
);

apps/meteor/app/lib/server/methods/saveSetting.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SettingValue } from '@rocket.chat/core-typings';
1+
import type { SettingEditor, SettingValue } from '@rocket.chat/core-typings';
22
import type { ServerMethods } from '@rocket.chat/ddp-client';
33
import { Settings } from '@rocket.chat/models';
44
import { Match, check } from 'meteor/check';
@@ -14,12 +14,12 @@ import { notifyOnSettingChanged } from '../lib/notifyListener';
1414
declare module '@rocket.chat/ddp-client' {
1515
// eslint-disable-next-line @typescript-eslint/naming-convention
1616
interface ServerMethods {
17-
saveSetting(_id: string, value: SettingValue, editor?: string): Promise<boolean>;
17+
saveSetting(_id: string, value: SettingValue, editor: SettingEditor): Promise<boolean>;
1818
}
1919
}
2020

2121
Meteor.methods<ServerMethods>({
22-
saveSetting: twoFactorRequired(async function (_id, value, editor) {
22+
saveSetting: twoFactorRequired(async function (_id: string, value: SettingValue, editor: SettingEditor) {
2323
const uid = Meteor.userId();
2424
if (!uid) {
2525
throw new Meteor.Error('error-action-not-allowed', 'Editing settings is not allowed', {

apps/meteor/imports/personal-access-tokens/server/api/methods/generateToken.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ declare module '@rocket.chat/ddp-client' {
1414
}
1515
}
1616

17-
export const generatePersonalAccessTokenOfUser = async ({ bypassTwoFactor, tokenName, userId }: {tokenName: string, userId: string, bypassTwoFactor: boolean}): Promise<string> => {
17+
export const generatePersonalAccessTokenOfUser = async ({
18+
bypassTwoFactor,
19+
tokenName,
20+
userId,
21+
}: {
22+
tokenName: string;
23+
userId: string;
24+
bypassTwoFactor: boolean;
25+
}): Promise<string> => {
1826
if (!(await hasPermissionAsync(userId, 'create-personal-access-tokens'))) {
1927
throw new Meteor.Error('not-authorized', 'Not Authorized', {
2028
method: 'personalAccessTokens:generateToken',
@@ -44,17 +52,23 @@ export const generatePersonalAccessTokenOfUser = async ({ bypassTwoFactor, token
4452
},
4553
});
4654
return token;
47-
}
55+
};
4856

4957
Meteor.methods<ServerMethods>({
50-
'personalAccessTokens:generateToken': twoFactorRequired(async function ({ tokenName, bypassTwoFactor }) {
58+
'personalAccessTokens:generateToken': twoFactorRequired(async function ({
59+
tokenName,
60+
bypassTwoFactor,
61+
}: {
62+
tokenName: string;
63+
bypassTwoFactor: boolean;
64+
}) {
5165
const uid = Meteor.userId();
5266
if (!uid) {
5367
throw new Meteor.Error('not-authorized', 'Not Authorized', {
5468
method: 'personalAccessTokens:generateToken',
5569
});
5670
}
57-
71+
5872
return generatePersonalAccessTokenOfUser({ tokenName, userId: uid, bypassTwoFactor });
5973
}),
6074
});

apps/meteor/imports/personal-access-tokens/server/api/methods/regenerateToken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const regeneratePersonalAccessTokenOfUser = async (tokenName: string, use
4444
};
4545

4646
Meteor.methods<ServerMethods>({
47-
'personalAccessTokens:regenerateToken': twoFactorRequired(async function ({ tokenName }) {
47+
'personalAccessTokens:regenerateToken': twoFactorRequired(async function ({ tokenName }: { tokenName: string }) {
4848
const uid = Meteor.userId();
4949
if (!uid) {
5050
throw new Meteor.Error('not-authorized', 'Not Authorized', {

apps/meteor/imports/personal-access-tokens/server/api/methods/removeToken.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export const removePersonalAccessTokenOfUser = async (tokenName: string, userId:
3434
name: tokenName,
3535
},
3636
});
37-
}
37+
};
3838

3939
Meteor.methods<ServerMethods>({
40-
'personalAccessTokens:removeToken': twoFactorRequired(async function ({ tokenName }) {
40+
'personalAccessTokens:removeToken': twoFactorRequired(async function ({ tokenName }: { tokenName: string }) {
4141
const uid = Meteor.userId();
4242
if (!uid) {
4343
throw new Meteor.Error('not-authorized', 'Not Authorized', {

0 commit comments

Comments
 (0)