Skip to content

Commit

Permalink
Chore: Migrate oauth2server to typescript (#25126)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Apr 8, 2022
1 parent 6b0be61 commit 049e476
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ type Request = {
body: any;
};

type PartialThis = {
readonly request: Request & { query: Record<string, string> };
};

type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptions> = {
urlParams: UrlParams<TPathPattern>;
// TODO make it unsafe
Expand Down Expand Up @@ -159,6 +163,8 @@ declare class APIClass<TBasePath extends string = '/'> {
operations: Operations<TPathPattern, TOptions>,
): void;

addAuthMethod(func: (this: PartialThis, ...args: any[]) => any): void;

success<T>(result: T): SuccessResult<T>;

success(): SuccessResult<void>;
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
});
Expand All @@ -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');
}
Expand All @@ -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;
}
Expand Down
30 changes: 30 additions & 0 deletions apps/meteor/definition/externals/meteor/oauth2server.d.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>;
debug: boolean;
});

oauth: {
model: {
AccessTokens: Mongo.Collection<unknown>;
};
};

app: HandleFunction & {
disable(name: string): void;
};

routes: {
disable(name: string): void;
get(path: string, callback: (req: Request, res: Response) => void): void;
};
}
}

0 comments on commit 049e476

Please sign in to comment.