From d0c936bd12272ed932ab4c37ed7889f918eb7ffb Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Mon, 22 Nov 2021 18:07:08 +0000 Subject: [PATCH] Break out JIRA oauth handling --- src/Config/Config.ts | 10 +++++++- src/Jira/Router.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/Webhooks.ts | 11 ++++++--- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/Jira/Router.ts diff --git a/src/Config/Config.ts b/src/Config/Config.ts index 2d84cacc..e42a8c0f 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -39,10 +39,18 @@ interface BridgeConfigGitLab { instances: {[name: string]: GitLabInstance}; } -interface BridgeConfigJira { +export interface BridgeConfigJira { webhook: { secret: string; }; + oauth: { + // eslint-disable-next-line camelcase + client_id: string; + // eslint-disable-next-line camelcase + client_secret: string; + // eslint-disable-next-line camelcase + redirect_uri: string; + }; } interface BridgeGenericWebhooksConfig { diff --git a/src/Jira/Router.ts b/src/Jira/Router.ts new file mode 100644 index 00000000..5b6f73b3 --- /dev/null +++ b/src/Jira/Router.ts @@ -0,0 +1,55 @@ +import axios from "axios"; +import { Router, Request, Response } from "express"; +import qs from "querystring"; +import { BridgeConfigJira } from "../Config/Config"; +import LogWrapper from "../LogWrapper"; +import { MessageQueue } from "../MessageQueue/MessageQueue"; +import { OAuthRequest, OAuthTokens } from "../Webhooks"; + +const log = new LogWrapper("JiraRouter"); + +export default class JiraRouter { + constructor(private readonly config: BridgeConfigJira, private readonly queue: MessageQueue) { } + + private async onOAuth(req: Request, res: Response) { + log.info("Got new JIRA oauth request"); + try { + const exists = await this.queue.pushWait({ + eventName: "jira.oauth.response", + sender: "GithubWebhooks", + data: { + code: req.query.code as string, + state: req.query.state as string, + }, + }); + if (!exists) { + res.status(404).send(`

Could not find user which authorised this request. Has it timed out?

`); + return; + } + const accessTokenRes = await axios.post(`https://github.com/login/oauth/access_token?${qs.encode({ + client_id: this.config.oauth.client_id, + client_secret: this.config.oauth.client_secret, + code: req.query.code as string, + redirect_uri: this.config.oauth.redirect_uri, + state: req.query.state as string, + })}`); + // eslint-disable-next-line camelcase + const result = qs.parse(accessTokenRes.data) as { access_token: string, token_type: string }; + await this.queue.push({ + eventName: "oauth.tokens", + sender: "GithubWebhooks", + data: { state: req.query.state as string, ... result }, + }); + res.send(`

Your account has been bridged

`); + } catch (ex) { + log.error("Failed to handle oauth request:", ex); + res.status(500).send(`

Encountered an error handing oauth request

`); + } + } + + public getRouter() { + const router = Router(); + router.get("/oauth", this.onOAuth.bind(this)); + return router; + } +} diff --git a/src/Webhooks.ts b/src/Webhooks.ts index 49f72fa8..1a45ad05 100644 --- a/src/Webhooks.ts +++ b/src/Webhooks.ts @@ -8,7 +8,8 @@ import { Server } from "http"; import axios from "axios"; import { IGitLabWebhookEvent } from "./Gitlab/WebhookTypes"; import { EmitterWebhookEvent, Webhooks as OctokitWebhooks } from "@octokit/webhooks" -import { IJiraWebhookEvent, JiraIssueEvent } from "./Jira/WebhookTypes"; +import { IJiraWebhookEvent } from "./Jira/WebhookTypes"; +import JiraRouter from "./Jira/Router"; const log = new LogWrapper("GithubWebhooks"); export interface GenericWebhookEvent { @@ -70,7 +71,11 @@ export class Webhooks extends EventEmitter { verify: this.verifyRequest.bind(this), })); this.expressApp.post("/", this.onPayload.bind(this)); - this.expressApp.get("/oauth", this.onGetOauth.bind(this)); + this.expressApp.get("/oauth", this.onGitHubGetOauth.bind(this)); + this.queue = createMessageQueue(config); + if (this.config.jira) { + this.expressApp.use("/jira", new JiraRouter(this.config.jira, this.queue).getRouter()); + } this.queue = createMessageQueue(config); } @@ -201,7 +206,7 @@ export class Webhooks extends EventEmitter { } } - public async onGetOauth(req: Request, res: Response) { + public async onGitHubGetOauth(req: Request, res: Response) { log.info("Got new oauth request"); try { if (!this.config.github) {