Skip to content

Commit 8294a06

Browse files
committed
fix: reintroduce authMethods to APIClass
1 parent dc67590 commit 8294a06

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

apps/meteor/app/api/server/ApiClass.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class APIClass<
170170

171171
private _routes: { path: string; options: Options; endpoints: Record<string, string> }[] = [];
172172

173-
public authMethods: ((...args: any[]) => any)[];
173+
public authMethods: ((req: Request) => Promise<IUser | undefined>)[];
174174

175175
protected helperMethods: Map<string, () => any> = new Map();
176176

@@ -252,7 +252,7 @@ export class APIClass<
252252
return parseJsonQuery(this);
253253
}
254254

255-
public addAuthMethod(func: (this: PartialThis, ...args: any[]) => any): void {
255+
public addAuthMethod(func: (req: Request) => Promise<IUser | undefined>): void {
256256
this.authMethods.push(func);
257257
}
258258

@@ -823,7 +823,7 @@ export class APIClass<
823823
(operations[method as keyof Operations<TPathPattern, TOptions>] as Record<string, any>).action =
824824
async function _internalRouteActionHandler() {
825825
if (options.authRequired || options.authOrAnonRequired) {
826-
const user = await api.authenticatedRoute.call(this, this.request);
826+
const user = await api.authenticatedRoute.call(api, this.request);
827827
this.user = user!;
828828
this.userId = this.user?._id;
829829
const authToken = this.request.headers.get('x-auth-token');
@@ -973,11 +973,8 @@ export class APIClass<
973973
}
974974

975975
protected async authenticatedRoute(req: Request): Promise<IUser | null> {
976-
const headers = Object.fromEntries(req.headers.entries());
977-
978-
const { 'x-user-id': userId } = headers;
979-
980-
const userToken = String(headers['x-auth-token']);
976+
const userId = req.headers.get('x-user-id');
977+
const userToken = req.headers.get('x-auth-token');
981978

982979
if (userId && userToken) {
983980
return Users.findOne(
@@ -990,6 +987,16 @@ export class APIClass<
990987
},
991988
);
992989
}
990+
991+
for (const method of this.authMethods) {
992+
// eslint-disable-next-line no-await-in-loop -- we want serial execution
993+
const user = await method(req);
994+
995+
if (user) {
996+
return user;
997+
}
998+
}
999+
9931000
return null;
9941001
}
9951002

apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ async function getAccessToken(accessToken: string) {
1919
}
2020

2121
export async function oAuth2ServerAuth(partialRequest: {
22-
headers: Record<string, any>;
23-
query: Record<string, any>;
24-
}): Promise<{ user: IUser } | undefined> {
22+
headers: Record<string, string | undefined>;
23+
query: Record<string, string | undefined>;
24+
}): Promise<IUser | undefined> {
2525
const headerToken = partialRequest.headers.authorization?.replace('Bearer ', '');
2626
const queryToken = partialRequest.query.access_token;
27+
const incomingToken = headerToken || queryToken;
2728

28-
const accessToken = await getAccessToken(headerToken || queryToken);
29+
if (!incomingToken) {
30+
return;
31+
}
32+
33+
const accessToken = await getAccessToken(incomingToken);
2934

3035
// If there is no token available or the token has expired, return undefined
3136
if (!accessToken || (accessToken.expires != null && accessToken.expires < new Date())) {
@@ -38,7 +43,7 @@ export async function oAuth2ServerAuth(partialRequest: {
3843
return;
3944
}
4045

41-
return { user };
46+
return user;
4247
}
4348

4449
oauth2server.app.disable('x-powered-by');
@@ -69,8 +74,13 @@ oauth2server.app.get('/oauth/userinfo', async (req: Request, res: Response) => {
6974
});
7075
});
7176

72-
API.v1.addAuthMethod(async function () {
73-
return oAuth2ServerAuth(this.request);
77+
API.v1.addAuthMethod((request: globalThis.Request) => {
78+
const url = new URL(request.url);
79+
const headers = Object.fromEntries(request.headers.entries());
80+
const query = Object.fromEntries(url.searchParams.entries());
81+
82+
return oAuth2ServerAuth({ headers, query });
7483
});
7584

7685
(WebApp.connectHandlers as unknown as ReturnType<typeof express>).use(oauth2server.app);
86+

0 commit comments

Comments
 (0)