Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async getRoom(@QueryParam("access_token") homeserverToken: string, @PathParam("alias") roomAlias: string): Promise {
try {
const appservice = await AppserviceStore.getByHomeserverToken(homeserverToken);
// We don't support room lookups
LogService.verbose("MatrixAppServiceApiService", "404ing request for room " + roomAlias + " at appservice " + appservice.id);
throw new ApiError(404, {errcode: "IO.T2BOT.DIMENSION.ROOMS_NOT_SUPPORTED"});
} catch (err) {
if (err instanceof ApiError) throw err;
LogService.error("MatrixAppServiceApiService", err);
throw new ApiError(403, {errcode: "M_FORBIDDEN"});
}
}
public async getAuthUrl(@QueryParam("scalar_token") scalarToken: string): Promise<{ authUrl: string }> {
const userId = await this.accountController.getTokenOwner(scalarToken);
try {
const slack = new SlackBridge(userId);
const authUrl = await slack.getAuthUrl();
return {authUrl};
} catch (e) {
LogService.error("DimensionSlackService", e);
throw new ApiError(400, "Error getting auth info");
}
}
}
public async getChannels(@QueryParam("scalar_token") scalarToken: string, @PathParam("teamId") teamId: string): Promise {
const userId = await this.accountController.getTokenOwner(scalarToken);
try {
const slack = new SlackBridge(userId);
return slack.getChannels(teamId);
} catch (e) {
LogService.error("DimensionSlackService", e);
throw new ApiError(400, "Error getting channel info");
}
}
public async getLink(@QueryParam("scalar_token") scalarToken: string, @PathParam("roomId") roomId: string): Promise {
const userId = await this.accountController.getTokenOwner(scalarToken);
try {
const gitter = new GitterBridge(userId);
return gitter.getLink(roomId);
} catch (e) {
LogService.error("DimensionGitterService", e);
throw new ApiError(400, "Error getting bridge info");
}
}
public async getLink(@QueryParam("scalar_token") scalarToken: string, @PathParam("roomId") roomId: string): Promise {
const userId = await this.accountController.getTokenOwner(scalarToken);
try {
const slack = new SlackBridge(userId);
return slack.getLink(roomId);
} catch (e) {
LogService.error("DimensionSlackService", e);
throw new ApiError(400, "Error getting bridge info");
}
}
public async getHooks(roomId: string): Promise {
const bridge = await this.getDefaultBridge();
try {
const response = await this.doProvisionRequest(bridge, "GET", `/api/v1/provision/${roomId}/hooks`);
if (!response.success) throw new Error("Failed to get webhooks");
return response.results;
} catch (e) {
LogService.error("WebhooksBridge", e);
return [];
}
}
this._app.use((err, req, res, next) => {
LogService.error("WebService", err);
let status = 500;
let message = "Internal Server Error";
if (err.message.startsWith("Unexpected token ") && err.message.includes("in JSON at position")) {
message = err.message;
status = 400;
}
res.status(status);
res.set('Content-Type', "application/json");
res.send(JSON.stringify(this._constructError(res, message)));
next('Error encountered during hook processing');
});
}, (err, res, _body) => {
if (err) {
LogService.error("matrix", "Error calling " + endpoint);
LogService.error("matrix", err);
reject(err);
} else if (res.statusCode !== 200) {
LogService.error("matrix", "Got status code " + res.statusCode + " while calling federated endpoint " + endpoint);
reject(new Error("Error in request: invalid status code"));
} else {
if (typeof (res.body) === "string") res.body = JSON.parse(res.body);
resolve(res.body);
}
});
});
_provisioningApiCatch(error, response) {
if (error === ProvisioningService.PERMISSION_ERROR_MESSAGE) {
response.status(400).send({success: false, message: error});
return;
}
LogService.error("WebService", error);
response.status(500).send({success: false, message: "Unknown error processing request"});
}
request(params, (err, response, body) => {
if (err) {
LogService.error("MatrixLiteClient (REQ-" + requestId + ")", err);
reject(err);
} else {
if (typeof(body) === 'string') {
try {
body = JSON.parse(body);
} catch (e) {
}
}
LogService.verbose("MatrixLiteClient (REQ-" + requestId + " RESP-H" + response.statusCode + ")", response.body);
if (response.statusCode < 200 || response.statusCode >= 300) {
LogService.error("MatrixLiteClient (REQ-" + requestId + ")", response.body);
reject(response);
} else resolve(raw ? response : body);
}
});