Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Adding oauth crud on the rocket.chat side #26220

Merged
merged 9 commits into from
Aug 24, 2022
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 apps/meteor/app/apps/server/bridges/bridges.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { AppUploadBridge } from './uploads';
import { UiInteractionBridge } from './uiInteraction';
import { AppSchedulerBridge } from './scheduler';
import { AppVideoConferenceBridge } from './videoConferences';
import { AppOAuthAppsBridge } from './oauthApps';

export class RealAppBridges extends AppBridges {
constructor(orch) {
Expand All @@ -43,6 +44,7 @@ export class RealAppBridges extends AppBridges {
this._schedulerBridge = new AppSchedulerBridge(orch);
this._cloudWorkspaceBridge = new AppCloudBridge(orch);
this._videoConfBridge = new AppVideoConferenceBridge(orch);
this._oAuthBridge = new AppOAuthAppsBridge(orch);
}

getCommandBridge() {
Expand Down Expand Up @@ -120,4 +122,8 @@ export class RealAppBridges extends AppBridges {
getVideoConferenceBridge() {
return this._videoConfBridge;
}

getOAuthAppsBridge() {
return this._oAuthBridge;
}
}
2 changes: 2 additions & 0 deletions apps/meteor/app/apps/server/bridges/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AppInternalBridge } from './internal';
import { AppSettingBridge } from './settings';
import { AppUserBridge } from './users';
import { AppSchedulerBridge } from './scheduler';
import { AppOAuthAppsBridge } from './oauthApps';

export {
RealAppBridges,
Expand All @@ -26,4 +27,5 @@ export {
AppUserBridge,
AppInternalBridge,
AppSchedulerBridge,
AppOAuthAppsBridge,
};
81 changes: 81 additions & 0 deletions apps/meteor/app/apps/server/bridges/oauthApps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { IOAuthApp, IOAuthAppParams } from '@rocket.chat/apps-engine/definition/accessors/IOAuthApp';
import { OAuthAppsBridge } from '@rocket.chat/apps-engine/server/bridges/OAuthAppsBridge';
import type { IOAuthApps } from '@rocket.chat/core-typings';
import { OAuthApps, Users } from '@rocket.chat/models';
import { Random } from 'meteor/random';
import { v4 as uuidv4 } from 'uuid';

import type { AppServerOrchestrator } from '../orchestrator';

export class AppOAuthAppsBridge extends OAuthAppsBridge {
constructor(private readonly orch: AppServerOrchestrator) {
super();
}

protected async create(oAuthApp: IOAuthAppParams, appId: string): Promise<string | null> {
this.orch.debugLog(`The App ${appId} is creating a new OAuth app.`);
const { clientId, clientSecret } = oAuthApp;
const botUser = await Users.findOne({ appId });

if (!botUser) {
throw new Error(`The user for app ${appId} is not registered.`);
}

const { _id, username } = botUser;

return (
await OAuthApps.insertOne({
...oAuthApp,
_id: uuidv4(),
appId,
clientId: clientId ?? Random.id(),
clientSecret: clientSecret ?? Random.secret(),
_createdAt: new Date(),
_createdBy: {
_id,
username,
},
} as unknown as IOAuthApps)
).insertedId;
}

protected async getById(id: string, appId: string): Promise<IOAuthApp | null> {
this.orch.debugLog(`The App ${appId} is getting the OAuth app by ID ${id}.`);
const data = await OAuthApps.findOne({ _id: id, appId });
if (data) {
const { _id, _createdAt, _createdBy, _updatedAt, ...rest } = data as any;
return {
...rest,
id: _id,
createdAt: _createdAt.toDateString(),
createdBy: {
id: _createdBy._id,
username: _createdBy.username,
},
updatedAt: _updatedAt,
};
}

return null;
}

protected async getByName(name: string, appId: string): Promise<Array<IOAuthApp>> {
this.orch.debugLog(`The App ${appId} is getting the OAuth apps by name.`);
return OAuthApps.find({ name, appId }).toArray() as unknown as Array<IOAuthApp>;
}

protected async update(oAuthApp: IOAuthAppParams, id: string, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is updating the OAuth app ${id}.`);
await OAuthApps.updateOne({ _id: id, appId }, { $set: oAuthApp }, { upsert: true });
}

protected async delete(id: string, appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is deleting the OAuth app ${id}.`);
await OAuthApps.deleteOne({ _id: id, appId });
}

protected async purge(appId: string): Promise<void> {
this.orch.debugLog(`The App ${appId} is deleting an OAuth app.`);
await OAuthApps.deleteMany({ appId });
}
}
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"@nivo/pie": "0.79.1",
"@rocket.chat/agenda": "workspace:^",
"@rocket.chat/api-client": "workspace:^",
"@rocket.chat/apps-engine": "1.33.0",
"@rocket.chat/apps-engine": "1.34.0-alpha.13",
"@rocket.chat/cas-validate": "workspace:^",
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/css-in-js": "next",
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/tests/data/apps/apps-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const APP_URL = 'https://github.com/RocketChat/Apps.RocketChat.Tester/blob/master/dist/appsrocketchattester_0.0.4.zip?raw=true';
export const APP_URL = 'https://github.com/RocketChat/Apps.RocketChat.Tester/blob/master/dist/appsrocketchattester_0.0.5.zip?raw=true';
export const APP_NAME = 'Apps.RocketChat.Tester';
export const APP_USERNAME = 'appsrocketchattester.bot';
export const apps = (path = '') => `/api/apps${path}`;
3 changes: 2 additions & 1 deletion packages/core-typings/src/IOAuthApps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface IOAuthApps {
_id: string;

name: string;
active: boolean;
clientId: string;
Expand All @@ -11,4 +10,6 @@ export interface IOAuthApps {
_id: string;
username: string;
};
_updatedAt: Date;
appId?: string;
}
18 changes: 16 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,21 @@ __metadata:
languageName: unknown
linkType: soft

"@rocket.chat/apps-engine@npm:1.33.0, @rocket.chat/apps-engine@npm:^1.32.0":
"@rocket.chat/apps-engine@npm:1.34.0-alpha.13":
version: 1.34.0-alpha.13
resolution: "@rocket.chat/apps-engine@npm:1.34.0-alpha.13"
dependencies:
adm-zip: ^0.5.9
cryptiles: ^4.1.3
lodash.clonedeep: ^4.5.0
semver: ^5.7.1
stack-trace: 0.0.10
uuid: ^3.4.0
checksum: b31bd9a7e7599b81ddbf8cd28399e83a65d2fb107f946516b847a827f15cd7266d00bb613a03c828e779d00b09b44c43eb59929fff2ba17999e134703770b745
languageName: node
linkType: hard

"@rocket.chat/apps-engine@npm:^1.32.0":
version: 1.33.0
resolution: "@rocket.chat/apps-engine@npm:1.33.0"
dependencies:
Expand Down Expand Up @@ -3856,7 +3870,7 @@ __metadata:
"@playwright/test": ^1.22.2
"@rocket.chat/agenda": "workspace:^"
"@rocket.chat/api-client": "workspace:^"
"@rocket.chat/apps-engine": 1.33.0
"@rocket.chat/apps-engine": 1.34.0-alpha.13
"@rocket.chat/cas-validate": "workspace:^"
"@rocket.chat/core-typings": "workspace:^"
"@rocket.chat/css-in-js": next
Expand Down