mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00
Break out JIRA oauth handling
This commit is contained in:
parent
1d9ed92144
commit
d0c936bd12
@ -39,10 +39,18 @@ interface BridgeConfigGitLab {
|
|||||||
instances: {[name: string]: GitLabInstance};
|
instances: {[name: string]: GitLabInstance};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BridgeConfigJira {
|
export interface BridgeConfigJira {
|
||||||
webhook: {
|
webhook: {
|
||||||
secret: string;
|
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 {
|
interface BridgeGenericWebhooksConfig {
|
||||||
|
55
src/Jira/Router.ts
Normal file
55
src/Jira/Router.ts
Normal file
@ -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<OAuthRequest, boolean>({
|
||||||
|
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(`<p>Could not find user which authorised this request. Has it timed out?</p>`);
|
||||||
|
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<OAuthTokens>({
|
||||||
|
eventName: "oauth.tokens",
|
||||||
|
sender: "GithubWebhooks",
|
||||||
|
data: { state: req.query.state as string, ... result },
|
||||||
|
});
|
||||||
|
res.send(`<p> Your account has been bridged </p>`);
|
||||||
|
} catch (ex) {
|
||||||
|
log.error("Failed to handle oauth request:", ex);
|
||||||
|
res.status(500).send(`<p>Encountered an error handing oauth request</p>`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRouter() {
|
||||||
|
const router = Router();
|
||||||
|
router.get("/oauth", this.onOAuth.bind(this));
|
||||||
|
return router;
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,8 @@ import { Server } from "http";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { IGitLabWebhookEvent } from "./Gitlab/WebhookTypes";
|
import { IGitLabWebhookEvent } from "./Gitlab/WebhookTypes";
|
||||||
import { EmitterWebhookEvent, Webhooks as OctokitWebhooks } from "@octokit/webhooks"
|
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");
|
const log = new LogWrapper("GithubWebhooks");
|
||||||
|
|
||||||
export interface GenericWebhookEvent {
|
export interface GenericWebhookEvent {
|
||||||
@ -70,7 +71,11 @@ export class Webhooks extends EventEmitter {
|
|||||||
verify: this.verifyRequest.bind(this),
|
verify: this.verifyRequest.bind(this),
|
||||||
}));
|
}));
|
||||||
this.expressApp.post("/", this.onPayload.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);
|
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");
|
log.info("Got new oauth request");
|
||||||
try {
|
try {
|
||||||
if (!this.config.github) {
|
if (!this.config.github) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user