From 4c68a93fc14d4a8c608a426da6c3e56a4470292f Mon Sep 17 00:00:00 2001 From: Desmond Obisi Date: Tue, 24 Sep 2024 17:39:45 +0100 Subject: [PATCH] chore: remove designate API for event and merge with project --- providers/event-github/auth.js | 69 ---------------------------------- providers/github/auth.js | 49 +++++++++++++++++++++--- providers/index.js | 6 --- routes/index.js | 12 +++--- 4 files changed, 49 insertions(+), 87 deletions(-) delete mode 100644 providers/event-github/auth.js diff --git a/providers/event-github/auth.js b/providers/event-github/auth.js deleted file mode 100644 index 8d895a1..0000000 --- a/providers/event-github/auth.js +++ /dev/null @@ -1,69 +0,0 @@ -const axios = require("axios"); -const { Octokit } = require("@octokit/rest"); -const { encrypt, decrypt, convertToMarkdown } = require("../../helpers/crypto"); - -const issueCreationAuth = (req, res) => { - if (!process.env.GITHUB_AUTH_CLIENT_ID) { - res.status(500).send("GitHub Issue Creation is not configured"); - return; - } - - const scopes = ["repo"]; - const encryptedFormData = encrypt(JSON.stringify(req.body)); - const url = `https://github.com/login/oauth/authorize?client_id=${ - process.env.GITHUB_AUTH_CLIENT_ID - }&scope=${scopes.join(",")}&state=${encryptedFormData}`; - - res.send({ authorizationLink: url }); -}; - -const issueCreationCallback = async (req, res) => { - const code = req.query.code; - const encryptedState = req.query.state; - - const formData = decrypt(encryptedState); - const parsedFormData = JSON.parse(formData); - const issueTitle = parsedFormData.title; - const markdown = convertToMarkdown(parsedFormData.body); - - if (!formData) { - return res.status(400).json({ error: "No form data found" }); - } - - try { - const tokenResponse = await axios.post( - "https://github.com/login/oauth/access_token", - { - client_id: process.env.GITHUB_AUTH_CLIENT_ID, - client_secret: process.env.GITHUB_AUTH_CLIENT_SECRET, - code, - }, - { - headers: { - Accept: "application/json", - }, - } - ); - - const accessToken = tokenResponse.data.access_token; - - const octokit = new Octokit({ auth: accessToken }); - - const { data: issue } = await octokit.rest.issues.create({ - owner: "badging", - repo: "event-diversity-and-inclusion", - title: issueTitle, - body: markdown, - }); - - res.redirect(issue.html_url); - } catch (error) { - console.error("Error in issue creation callback:", error); - res.status(500).send("An error occurred during issue creation"); - } -}; - -module.exports = { - issueCreationAuth, - issueCreationCallback, -}; diff --git a/providers/github/auth.js b/providers/github/auth.js index cbed8d4..86ccc37 100644 --- a/providers/github/auth.js +++ b/providers/github/auth.js @@ -4,6 +4,11 @@ require("dotenv").config(); const axios = require("axios"); const { saveUser } = require("../../database/controllers/user.controller.js"); const { getUserInfo, getUserRepositories } = require("./APICalls.js"); +const { + encrypt, + decrypt, + convertToMarkdown, +} = require("../../helpers/crypto.js"); // instantiate Github App for event handling (webhooks) const githubApp = new App({ @@ -21,17 +26,28 @@ const githubApp = new App({ * @param {*} res Response to send back to the caller */ const githubAuth = (req, res) => { + const { type } = req.body; if (!process.env.GITHUB_AUTH_CLIENT_ID) { res.status(500).send("GitHub provider is not configured"); return; } - const scopes = ["user", "repo"]; - const url = `https://github.com/login/oauth/authorize?client_id=${ - process.env.GITHUB_AUTH_CLIENT_ID - }&scope=${scopes.join(",")}`; + if (type === "event-badging") { + const scopes = ["repo"]; + const encryptedFormData = encrypt(JSON.stringify(req.body)); + const url = `https://github.com/login/oauth/authorize?client_id=${ + process.env.GITHUB_AUTH_CLIENT_ID + }&scope=${scopes.join(",")}&state=${encryptedFormData}`; - res.redirect(url); + res.send({ authorizationLink: url }); + } else { + const scopes = ["user", "repo"]; + const url = `https://github.com/login/oauth/authorize?client_id=${ + process.env.GITHUB_AUTH_CLIENT_ID + }&scope=${scopes.join(",")}`; + + res.redirect(url); + } }; /** @@ -72,6 +88,17 @@ const requestAccessToken = async (code) => { const handleOAuthCallback = async (req, res) => { const code = req.body.code ?? req.query.code; + let issueTitle; + let markdown; + + if (req.query.state) { + const encryptedState = req.query.state; + const formData = decrypt(encryptedState); + const parsedFormData = JSON.parse(formData); + issueTitle = parsedFormData.title; + markdown = convertToMarkdown(parsedFormData.body); + } + const { access_token: accessToken, errors: accessTokenErrors } = await requestAccessToken(code); if (accessTokenErrors.length > 0) { @@ -81,6 +108,18 @@ const handleOAuthCallback = async (req, res) => { const octokit = new Octokit({ auth: `${accessToken}` }); + if (issueTitle && markdown) { + const { data: issue } = await octokit.rest.issues.create({ + owner: "badging", + repo: "event-diversity-and-inclusion", + title: issueTitle, + body: markdown, + }); + + res.redirect(issue.html_url); + return; + } + // Authenticated user details const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo( octokit diff --git a/providers/index.js b/providers/index.js index fea9369..5a8108b 100644 --- a/providers/index.js +++ b/providers/index.js @@ -1,7 +1,3 @@ -const { - issueCreationCallback, - issueCreationAuth, -} = require("./event-github/auth.js"); const { githubAuth, githubAuthCallback, @@ -15,6 +11,4 @@ module.exports = { githubApp, gitlabAuth, gitlabAuthCallback, - issueCreationAuth, - issueCreationCallback, }; diff --git a/routes/index.js b/routes/index.js index 6b55a9e..d69a962 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,8 +10,6 @@ const { githubApp, gitlabAuth, gitlabAuthCallback, - issueCreationCallback, - issueCreationAuth, } = require("../providers/index.js"); /** @@ -151,6 +149,11 @@ const setupRoutes = (app) => { githubAuth(req, res); }); + // for event badging + app.post("/api/auth/github", (req, res) => { + githubAuth(req, res); + }); + app.get("/api/auth/gitlab", (req, res) => { gitlabAuth(req, res); }); @@ -161,7 +164,6 @@ const setupRoutes = (app) => { gitlabAuthCallback(app); app.get("/api/badgedRepos", badgedRepos); app.post("/api/repos-to-badge", reposToBadge); - app.get("/api/issue-callback", issueCreationCallback); // github app routes app.post("/api/event_badging", async (req, res) => { @@ -177,10 +179,6 @@ const setupRoutes = (app) => { res.send("ok"); }); - app.post("/api/submit-form", (req, res) => { - issueCreationAuth(req, res); - }); - // route to get all events app.get("/api/badged_events", getAllEvents);