Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fifty-tools-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/rest-typings": patch
---

Add OpenAPI support for the Rocket.Chat e2e.setRoomKeyID endpoints by migrating to a modern chained route definition syntax and utilizing shared AJV schemas for validation to enhance API documentation and ensure type safety through response validation.
109 changes: 59 additions & 50 deletions apps/meteor/app/api/server/v1/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Subscriptions, Users } from '@rocket.chat/models';
import {
ajv,
validateUnauthorizedErrorResponse,
validateBadRequestErrorResponse,
ise2eGetUsersOfRoomWithoutKeyParamsGET,
ise2eSetRoomKeyIDParamsPOST,
ise2eSetUserPublicAndPrivateKeysParamsPOST,
ise2eUpdateGroupKeyParamsPOST,
isE2EProvideUsersGroupKeyProps,
Expand All @@ -20,12 +22,61 @@ import { setRoomKeyIDMethod } from '../../../e2e/server/methods/setRoomKeyID';
import { setUserPublicAndPrivateKeysMethod } from '../../../e2e/server/methods/setUserPublicAndPrivateKeys';
import { updateGroupKey } from '../../../e2e/server/methods/updateGroupKey';
import { settings } from '../../../settings/server';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';

// After 10s the room lock will expire, meaning that if for some reason the process never completed
// The next reset will be available 10s after
const LockMap = new ExpiryMap<string, boolean>(10000);

type E2eSetRoomKeyIdProps = {
rid: string;
keyID: string;
};

const E2eSetRoomKeyIdSchema = {
type: 'object',
properties: {
rid: {
type: 'string',
},
keyID: {
type: 'string',
},
},
required: ['rid', 'keyID'],
additionalProperties: false,
};

const isE2eSetRoomKeyIdProps = ajv.compile<E2eSetRoomKeyIdProps>(E2eSetRoomKeyIdSchema);

const e2eEndpoints = API.v1.post(
'e2e.setRoomKeyID',
{
authRequired: true,
body: isE2eSetRoomKeyIdProps,
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
200: ajv.compile<void>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
}),
},
},

async function action() {
const { rid, keyID } = this.bodyParams;

await setRoomKeyIDMethod(this.userId, rid, keyID);

return API.v1.success();
},
);

API.v1.addRoute(
'e2e.fetchMyKeys',
{
Expand Down Expand Up @@ -57,55 +108,6 @@ API.v1.addRoute(
},
);

/**
* @openapi
* /api/v1/e2e.setRoomKeyID:
* post:
* description: Sets the end-to-end encryption key ID for a room
* security:
* - autenticated: {}
* requestBody:
* description: A tuple containing the room ID and the key ID
* content:
* application/json:
* schema:
* type: object
* properties:
* rid:
* type: string
* keyID:
* type: string
* responses:
* 200:
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiSuccessV1'
* default:
* description: Unexpected error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ApiFailureV1'
*/

API.v1.addRoute(
'e2e.setRoomKeyID',
{
authRequired: true,
validateParams: ise2eSetRoomKeyIDParamsPOST,
},
{
async post() {
const { rid, keyID } = this.bodyParams;

await setRoomKeyIDMethod(this.userId, rid, keyID);

return API.v1.success();
},
},
);

/**
* @openapi
* /api/v1/e2e.setUserPublicAndPrivateKeys:
Expand Down Expand Up @@ -323,3 +325,10 @@ API.v1.addRoute(
},
},
);

export type E2eEndpoints = ExtractRoutesFromAPI<typeof e2eEndpoints>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends E2eEndpoints {}
}
1 change: 0 additions & 1 deletion packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ export * from './v1/autotranslate/AutotranslateGetSupportedLanguagesParamsGET';
export * from './v1/autotranslate/AutotranslateSaveSettingsParamsPOST';
export * from './v1/autotranslate/AutotranslateTranslateMessageParamsPOST';
export * from './v1/e2e/e2eGetUsersOfRoomWithoutKeyParamsGET';
export * from './v1/e2e/e2eSetRoomKeyIDParamsPOST';
export * from './v1/e2e/e2eSetUserPublicAndPrivateKeysParamsPOST';
export * from './v1/e2e/e2eUpdateGroupKeyParamsPOST';
export * from './v1/e2e';
Expand Down
24 changes: 0 additions & 24 deletions packages/rest-typings/src/v1/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,6 @@ const E2eUpdateGroupKeySchema = {

export const isE2eUpdateGroupKeyProps = ajv.compile<E2eUpdateGroupKeyProps>(E2eUpdateGroupKeySchema);

type E2eSetRoomKeyIdProps = {
rid: string;
keyID: string;
};

const E2eSetRoomKeyIdSchema = {
type: 'object',
properties: {
rid: {
type: 'string',
},
keyID: {
type: 'string',
},
},
required: ['rid', 'keyID'],
additionalProperties: false,
};

export const isE2eSetRoomKeyIdProps = ajv.compile<E2eSetRoomKeyIdProps>(E2eSetRoomKeyIdSchema);

type E2EProvideUsersGroupKeyProps = {
usersSuggestedGroupKeys: Record<IRoom['_id'], { _id: IUser['_id']; key: string; oldKeys: ISubscription['suggestedOldRoomKeys'] }[]>;
};
Expand Down Expand Up @@ -187,9 +166,6 @@ export type E2eEndpoints = {
'/v1/e2e.rejectSuggestedGroupKey': {
POST: (params: E2eGetUsersOfRoomWithoutKeyProps) => void;
};
'/v1/e2e.setRoomKeyID': {
POST: (params: E2eSetRoomKeyIdProps) => void;
};
'/v1/e2e.fetchMyKeys': {
GET: () => { public_key: string; private_key: string };
};
Expand Down
26 changes: 0 additions & 26 deletions packages/rest-typings/src/v1/e2e/e2eSetRoomKeyIDParamsPOST.ts

This file was deleted.

Loading