From 049e4768229a5c3771f0fcde9bdf6c9b41d99c3d Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Fri, 8 Apr 2022 13:59:31 -0600 Subject: [PATCH] Chore: Migrate oauth2server to typescript (#25126) --- apps/meteor/app/api/server/api.d.ts | 6 ++++ .../{oauth2-server.js => oauth2-server.ts} | 16 ++++++---- .../externals/meteor/oauth2server.d.ts | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) rename apps/meteor/app/oauth2-server-config/server/oauth/{oauth2-server.js => oauth2-server.ts} (85%) create mode 100644 apps/meteor/definition/externals/meteor/oauth2server.d.ts diff --git a/apps/meteor/app/api/server/api.d.ts b/apps/meteor/app/api/server/api.d.ts index 79caba6f763b..cd58b002453a 100644 --- a/apps/meteor/app/api/server/api.d.ts +++ b/apps/meteor/app/api/server/api.d.ts @@ -65,6 +65,10 @@ type Request = { body: any; }; +type PartialThis = { + readonly request: Request & { query: Record }; +}; + type ActionThis = { urlParams: UrlParams; // TODO make it unsafe @@ -159,6 +163,8 @@ declare class APIClass { operations: Operations, ): void; + addAuthMethod(func: (this: PartialThis, ...args: any[]) => any): void; + success(result: T): SuccessResult; success(): SuccessResult; diff --git a/apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.js b/apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.ts similarity index 85% rename from apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.js rename to apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.ts index 69d2f9baaeef..e27805458f55 100644 --- a/apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.js +++ b/apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.ts @@ -1,7 +1,9 @@ +/* eslint-disable @typescript-eslint/camelcase */ import { Meteor } from 'meteor/meteor'; import { Mongo } from 'meteor/mongo'; import { WebApp } from 'meteor/webapp'; import { OAuth2Server } from 'meteor/rocketchat:oauth2-server'; +import { Request, Response } from 'express'; import { Users } from '../../../models/server'; import { OAuthApps } from '../../../models/server/raw'; @@ -17,7 +19,7 @@ const oauth2server = new OAuth2Server({ }); // https://github.com/RocketChat/rocketchat-oauth2-server/blob/e758fd7ef69348c7ceceabe241747a986c32d036/model.coffee#L27-L27 -function getAccessToken(accessToken) { +function getAccessToken(accessToken: string): any { return oauth2server.oauth.model.AccessTokens.findOne({ accessToken, }); @@ -28,7 +30,7 @@ oauth2server.routes.disable('x-powered-by'); WebApp.connectHandlers.use(oauth2server.app); -oauth2server.routes.get('/oauth/userinfo', function (req, res) { +oauth2server.routes.get('/oauth/userinfo', function (req: Request, res: Response) { if (req.headers.authorization == null) { return res.sendStatus(401).send('No token'); } @@ -55,17 +57,19 @@ oauth2server.routes.get('/oauth/userinfo', function (req, res) { }); API.v1.addAuthMethod(function () { - let headerToken = this.request.headers.authorization; + const headerToken = this.request.headers.authorization; const getToken = this.request.query.access_token; + + let token: string | undefined; if (headerToken != null) { const matches = headerToken.match(/Bearer\s(\S+)/); if (matches) { - headerToken = matches[1]; + token = matches[1]; } else { - headerToken = undefined; + token = undefined; } } - const bearerToken = headerToken || getToken; + const bearerToken = token || getToken; if (bearerToken == null) { return; } diff --git a/apps/meteor/definition/externals/meteor/oauth2server.d.ts b/apps/meteor/definition/externals/meteor/oauth2server.d.ts new file mode 100644 index 000000000000..7fb1f963b9be --- /dev/null +++ b/apps/meteor/definition/externals/meteor/oauth2server.d.ts @@ -0,0 +1,30 @@ +declare module 'meteor/rocketchat:oauth2-server' { + import { Mongo } from 'meteor/mongo'; + import { HandleFunction } from 'connect'; + import { Request, Response } from 'express'; + + export class OAuth2Server { + constructor(opts: { + accessTokensCollectionName: string; + refreshTokensCollectionName: string; + authCodesCollectionName: string; + clientsCollection: Mongo.Collection; + debug: boolean; + }); + + oauth: { + model: { + AccessTokens: Mongo.Collection; + }; + }; + + app: HandleFunction & { + disable(name: string): void; + }; + + routes: { + disable(name: string): void; + get(path: string, callback: (req: Request, res: Response) => void): void; + }; + } +}