import type { IOAuthApps } from '@rocket.chat/core-typings'; import type { ServerMethods } from '@rocket.chat/ddp-client'; import { OAuthApps, Users } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; import { parseUriList } from '../../lib/auth/oauth2-server/parseUriList'; import { hasPermissionAsync } from '../../lib/authorization/hasPermission'; import { methodDeprecationLogger } from '../../lib/deprecationWarningLogger'; declare module '@rocket.chat/ddp-client' { // eslint-disable-next-line @typescript-eslint/naming-convention interface ServerMethods { updateOAuthApp( applicationId: IOAuthApps['_id'], application: Pick, ): Promise; } } export const updateOAuthApp = async ( userId: string, applicationId: IOAuthApps['_id'], application: Pick, ): Promise => { if (!(await hasPermissionAsync(userId, 'manage-oauth-apps'))) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'updateOAuthApp' }); } if (!application.name || typeof application.name.valueOf() !== 'string' || application.name.trim() === '') { throw new Meteor.Error('error-invalid-name', 'Invalid name', { method: 'updateOAuthApp' }); } if (!application.redirectUri || typeof application.redirectUri.valueOf() !== 'string' || application.redirectUri.trim() === '') { throw new Meteor.Error('error-invalid-redirectUri', 'Invalid redirectUri', { method: 'updateOAuthApp', }); } if (typeof application.active !== 'boolean') { throw new Meteor.Error('error-invalid-arguments', 'Invalid arguments', { method: 'updateOAuthApp', }); } const redirectUri = parseUriList(application.redirectUri); if (redirectUri.length === 0) { throw new Meteor.Error('error-invalid-redirectUri', 'Invalid redirectUri', { method: 'updateOAuthApp', }); } const updatedApplication = await OAuthApps.updateById(applicationId, { name: application.name, active: application.active, redirectUri, _updatedBy: await Users.findOneById(userId, { projection: { username: 1, }, }), }); if (updatedApplication == null) { throw new Meteor.Error('error-application-not-found', 'Application not found', { method: 'updateOAuthApp', }); } return updatedApplication; }; Meteor.methods({ async updateOAuthApp(applicationId, application) { methodDeprecationLogger.method('updateOAuthApp', '9.0.0', '/v1/oauth-apps.update'); if (!this.userId) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'updateOAuthApp' }); } return updateOAuthApp(this.userId, applicationId, application); }, });