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

Add checkToken back to addAppServicePath and ensure metrics doesn't require a token #435

Merged
merged 5 commits into from
Aug 12, 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
1 change: 1 addition & 0 deletions changelog.d/435.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug introduced in 5.0.0 which caused the `/metrics` endpoint to request authentication. The endpoint no longer requires authentication.
9 changes: 5 additions & 4 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,24 +958,25 @@ export class Bridge {
* Install a custom handler for an incoming HTTP API request. This allows
* callers to add extra functionality, implement new APIs, etc...
* @param opts Named options
* @param opts.authenticate Should the token be automatically checked. Defaults to true.
* @param opts.handler Function to handle requests
* @param opts.method The HTTP method name.
* @param opts.path Path to the endpoint.
* @param opts.checkToken Should the token be automatically checked. Defaults to true.
* @param opts.handler Function to handle requests
* to this endpoint.
*/
public addAppServicePath(opts: {
method: "GET"|"PUT"|"POST"|"DELETE",
path: string,
authenticate?: boolean,
handler: (req: ExRequest, respose: ExResponse, next: NextFunction) => void,
}): void {
if (!this.appservice) {
throw Error('Cannot call addAppServicePath before calling .run()');
}
const app: Application = this.appservice.expressApp;

const authenticate = opts.authenticate ?? true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const authenticate = opts.authenticate ?? true;
authenticate = opts.authenticate ?? true;

Is re-declaring a parameter on the top level of a function valid in JavaScript?
I'm getting SyntaxError: Identifier 'hi' has already been declared in a local test.

Is the linter dead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine? It's an opts object, not a parameter. (Which is why I can't do defaults)

app[opts.method.toLowerCase() as "get"|"put"|"post"|"delete"](opts.path, (req, res, ...args) => {
if (!this.requestCheckToken(req)) {
if (authenticate && !this.requestCheckToken(req)) {
return res.status(403).send({
errcode: "M_FORBIDDEN",
error: "Bad token supplied,"
Expand Down
1 change: 1 addition & 0 deletions src/components/prometheusmetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ export class PrometheusMetrics {
public addAppServicePath(bridge: Bridge): void {
bridge.addAppServicePath({
method: "GET",
authenticate: false,
path: "/metrics",
handler: async (_req: Request, res: Response) => {
try {
Expand Down