Skip to content
Closed
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
13 changes: 12 additions & 1 deletion apps/meteor/app/api/server/middlewares/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
cookies?: boolean;
};

/**
* Creates an Express middleware that authenticates requests using header/cookie tokens or OAuth2.
*
* The middleware sets `req.user` when authentication succeeds and `req.userId` to the authenticated user's `_id`.
* If `rejectUnauthorized` is true and no user is authenticated, the middleware responds with HTTP 401 and stops the request.
*
* @param config - Configuration for the middleware.
* - `rejectUnauthorized` (default: `true`): If true, unauthenticated requests are rejected with HTTP 401.
* - `cookies` (default: `false`): If true, authentication values are read from cookies when available.
* @returns An Express middleware function that enforces authentication and populates `req.user` and `req.userId`.
*/
export function authenticationMiddleware(
config: AuthenticationMiddlewareConfig = {
rejectUnauthorized: true,
Expand Down Expand Up @@ -69,4 +80,4 @@
}
next();
};
}
}

Check failure on line 83 in apps/meteor/app/api/server/middlewares/authentication.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Insert `⏎`
14 changes: 12 additions & 2 deletions apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
debug: false,
});

// https://github.com/RocketChat/rocketchat-oauth2-server/blob/e758fd7ef69348c7ceceabe241747a986c32d036/model.coffee#L27-L27
/**
* Fetches an access token record by its token string.
*
* @returns The access token record matching `accessToken`, or `undefined` if none is found.
*/
async function getAccessToken(accessToken: string) {
return OAuthAccessTokens.findOneByAccessToken(accessToken);
}

/**
* Authenticate a request using an OAuth2 access token and return the corresponding user.
*
* @param partialRequest - Object containing `headers` and `query` used to locate the access token (`Authorization: Bearer <token>` header or `access_token` query parameter)
* @returns The authenticated `IUser` when the token is present, valid, and maps to an existing user; `undefined` if the token is missing, invalid, expired, or the user is not found
*/
export async function oAuth2ServerAuth(partialRequest: {
headers: Record<string, string | undefined>;
query: Record<string, string | undefined>;
Expand Down Expand Up @@ -82,4 +92,4 @@
return oAuth2ServerAuth({ headers, query });
});

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

Check failure on line 95 in apps/meteor/app/oauth2-server-config/server/oauth/oauth2-server.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Insert `⏎`
Loading