Skip to content

Commit

Permalink
fix: separate concern for project and event badging
Browse files Browse the repository at this point in the history
  • Loading branch information
DesmondSanctity committed Nov 21, 2024
1 parent 9b2609c commit 7c447e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 11 additions & 5 deletions providers/github/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const githubAuth = (req, res) => {
const scopes = ["public_repo"];
const encryptedFormData = encrypt(JSON.stringify(req.body));
const url = `https://github.com/login/oauth/authorize?client_id=${
process.env.GITHUB_AUTH_CLIENT_ID
process.env.GITHUB_AUTH_CLIENT_ID_EVENT
}&scope=${scopes.join(",")}&state=${encryptedFormData}`;

res.send({ authorizationLink: url });
Expand All @@ -55,15 +55,19 @@ const githubAuth = (req, res) => {
* @param {*} code Code returned by the GitHub OAuth authorization API
* @returns A json object with `access_token` and `errors`
*/
const requestAccessToken = async (code) => {
const requestAccessToken = async (code, isEventBadging = false) => {
try {
const {
data: { access_token },
} = 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,
client_id: isEventBadging
? process.env.GITHUB_AUTH_CLIENT_ID_EVENT
: process.env.GITHUB_AUTH_CLIENT_ID,
client_secret: isEventBadging
? process.env.GITHUB_AUTH_CLIENT_SECRET_EVENT
: process.env.GITHUB_AUTH_CLIENT_SECRET,
code,
},
{
Expand All @@ -90,17 +94,19 @@ const handleOAuthCallback = async (req, res) => {

let issueTitle;
let markdown;
let isEventBadging = false;

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);
isEventBadging = true;
}

const { access_token: accessToken, errors: accessTokenErrors } =
await requestAccessToken(code);
await requestAccessToken(code, isEventBadging);
if (accessTokenErrors.length > 0) {
res.status(500).send(accessTokenErrors.join());
return;
Expand Down
3 changes: 3 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ const setupRoutes = (app) => {
app.get("/api/callback/github", handleOAuthCallback);
app.get("/api/callback/gitlab", handleOAuthCallbackGitlab);

app.post("/api/callback/github", handleOAuthCallback);
app.post("/api/callback/gitlab", handleOAuthCallbackGitlab);

app.get("/api/badgedRepos", badgedRepos);
app.post("/api/repos-to-badge", reposToBadge);

Expand Down

0 comments on commit 7c447e2

Please sign in to comment.