From 6ad5f703dbc2f173c9c35ef9eb953c162b98d232 Mon Sep 17 00:00:00 2001
From: oitsjustjose
Date: Tue, 31 Dec 2024 17:22:29 -0600
Subject: [PATCH] More Cleanup & Fixes
---
backend/src/app/controllers/job/put.ts | 36 ++++++++++++--
frontend/src/App.css | 10 ----
frontend/src/components/Help/Info.jsx | 9 +---
frontend/src/pages/Home.jsx | 68 ++++++++++++++------------
frontend/src/shared/index.js | 28 ++---------
processor/.gitignore | 4 +-
6 files changed, 77 insertions(+), 78 deletions(-)
diff --git a/backend/src/app/controllers/job/put.ts b/backend/src/app/controllers/job/put.ts
index ba71cb8..81b8812 100644
--- a/backend/src/app/controllers/job/put.ts
+++ b/backend/src/app/controllers/job/put.ts
@@ -3,6 +3,12 @@ import Job from "../../models/Job";
import Status from "../../status";
import { Fernet } from "fernet-nodejs";
+type OAuthTokenResponse = {
+ access_token: string;
+ token_type: string;
+ expires_in: number;
+};
+
const getNextQueuePlace = async () => {
const allActiveJobs = await Job.find({ status: Status.ENQUEUED }).sort({
queuePlace: "desc",
@@ -16,7 +22,7 @@ const getNextQueuePlace = async () => {
export default async (req: Request, res: Response) => {
try {
if (
- !req.body.oauthToken ||
+ !req.body.clientOauthCode ||
!req.body.curseforgeSlug ||
!req.body.modrinthId
) {
@@ -25,15 +31,37 @@ export default async (req: Request, res: Response) => {
.send(
[
"Missing a required parameter in the JSON body",
- "Required params: oauthToken: string, curseforgeSlug: string, modrinthId: string",
+ "Required params: clientOauthCode: string, curseforgeSlug: string, modrinthId: string",
].join(" ")
);
}
- const fern = new Fernet(process.env.SECRET);
+ const response = await fetch(
+ "https://api.modrinth.com/_internal/oauth/token",
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded",
+ Authorization: process.env.MODRINTH_OAUTH_CLIENT_SECRET,
+ },
+ body: new URLSearchParams({
+ code: req.body.clientOauthCode,
+ client_id: process.env.MODRINTH_OAUTH_CLIENT_ID,
+ redirect_uri: "https://ctm.oitsjustjose.com/",
+ grant_type: "authorization_code",
+ }),
+ }
+ );
+ if (!response.ok) {
+ return res.status(400).send(response);
+ }
+
+ const body = (await response.json()) as OAuthTokenResponse;
+
+ const fern = new Fernet(process.env.SECRET);
const newJob = new Job({
- oauthToken: fern.encrypt(req.body.oauthToken),
+ oauthToken: fern.encrypt(body.access_token),
curseforgeSlug: req.body.curseforgeSlug,
modrinthId: req.body.modrinthId,
queuePlace: await getNextQueuePlace(),
diff --git a/frontend/src/App.css b/frontend/src/App.css
index c96ebe4..750ea4a 100644
--- a/frontend/src/App.css
+++ b/frontend/src/App.css
@@ -65,13 +65,3 @@ button.info svg {
button svg {
vertical-align: top;
}
-
-button.modrinth {
- background-color: #1bd96a;
- color: black;
-}
-
-button.modrinth:hover {
- background-color: #14a551;
- color: black;
-}
diff --git a/frontend/src/components/Help/Info.jsx b/frontend/src/components/Help/Info.jsx
index 6da0009..1d4c691 100644
--- a/frontend/src/components/Help/Info.jsx
+++ b/frontend/src/components/Help/Info.jsx
@@ -1,7 +1,6 @@
import React, { useState } from 'react';
-import { Modal, Image, Button } from 'react-bootstrap';
+import { Image, Modal } from 'react-bootstrap';
import ThirdPartyHelp from '../../img/cftpd.png';
-import { DoModrinthOauth } from '../../shared';
export default ({ dark, override, propagateOnHide }) => {
const [show, setShow] = useState(!window.localStorage.getItem('info-dismissed'));
@@ -67,12 +66,6 @@ export default ({ dark, override, propagateOnHide }) => {
Share this tool on Twitter, Discord or whatever you please!
-
-
-
-
);
diff --git a/frontend/src/pages/Home.jsx b/frontend/src/pages/Home.jsx
index 4da3747..2417fdd 100644
--- a/frontend/src/pages/Home.jsx
+++ b/frontend/src/pages/Home.jsx
@@ -8,7 +8,7 @@ import OutputLog from '../components/OutputLog';
import Info from '../components/Help/Info';
import HeaderImg from '../img/ctm.png';
import ModrinthProjId from '../components/Help/ModrinthProjId';
-import { DoModrinthOauth, GetModrinthOauth, ModrinthOauthCallbackHandler } from '../shared';
+import { CreateModrinthUri, ModrinthOauthCallbackHandler } from '../shared';
export default () => {
const [dark, setDark] = useState(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
@@ -19,43 +19,47 @@ export default () => {
modrinthId: '',
});
- const makeJob = async (evt) => {
+ /*
+ Begins the Modrinth OAuth flow, creating a URL with a callback that has the
+ data we need in the query params and will return back with the client's
+ oauth code that will be needed back on the backend
+ */
+ const startOauthFlow = (evt) => {
evt && evt.preventDefault();
- if (!formData.curseforgeSlug.length) return;
- if (!formData.modrinthId.length) return;
- /* OAuth block to verify we're logged in */
- const oauthToken = GetModrinthOauth();
- if (!oauthToken) {
- DoModrinthOauth(formData.curseforgeSlug, formData.modrinthId);
- return;
- }
-
- const body = {
- curseforgeSlug: formData.curseforgeSlug,
- modrinthId: formData.modrinthId,
- oauthToken,
- };
-
- const resp = await fetch('/api/v1/jobs', {
- method: 'PUT',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(body),
- });
-
- if (resp && resp.ok) {
- const data = await resp.text();
- setJobId(data);
- window.localStorage.setItem('last-jobid', data);
- }
+ const uri = CreateModrinthUri(formData.curseforgeSlug, formData.modrinthId);
+ window.location.href = uri;
};
useEffect(() => { // Handle the Modrinth OAuth callback
+ const internalCreateJob = async (formDataIn) => {
+ if (!formDataIn.curseforgeSlug.length) return;
+ if (!formDataIn.modrinthId.length) return;
+ if (!formDataIn.code.length) return;
+
+ const body = {
+ curseforgeSlug: formDataIn.curseforgeSlug,
+ modrinthId: formDataIn.modrinthId,
+ clientOauthCode: formDataIn.code,
+ };
+
+ const resp = await fetch('/api/v1/jobs', {
+ method: 'PUT',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(body),
+ });
+
+ if (resp && resp.ok) {
+ const data = await resp.text();
+ setJobId(data);
+ window.localStorage.setItem('last-jobid', data);
+ }
+ };
+
const newFormData = ModrinthOauthCallbackHandler();
if (!newFormData) return () => { };
- setFormData({ ...newFormData });
- makeJob();
+ internalCreateJob(newFormData);
// Push state w/o URLQuery to avoid infinite reload of the page
if (window.history.pushState) {
@@ -65,7 +69,7 @@ export default () => {
}
return () => { };
- }, [ModrinthOauthCallbackHandler, setFormData, makeJob]);
+ }, [ModrinthOauthCallbackHandler]);
useEffect(() => { // DARK MODE STUFF HERE
// Set initial state before even bothering with the update state
@@ -111,7 +115,7 @@ export default () => {
M
igrator
-
CurseForge Project Slug
{
+export const CreateModrinthUri = (curseforgeSlug, modrinthId) => {
+ console.log(process.env);
const uri = new URL(`${window.location.origin}${window.location.pathname}`);
- curseforgeSlug && curseforgeSlug.length && uri.searchParams.set('curseforgeSlug', curseforgeSlug);
- modrinthId && curseforgeSlug.length && uri.searchParams.set('modrinthId', modrinthId);
- return `https://modrinth.com/auth/authorize?client_id=50zijYLk&redirect_uri=${encodeURIComponent(uri.toString())}&scope=PROJECT_WRITE+VERSION_CREATE`;
-};
-
-export const GetModrinthOauth = () => window.localStorage.getItem(OAUTH_KEY);
-
-/**
- *
- * @param {string | null} curseforgeSlug
- * @param {string | null} modrinthId
- * @returns
- */
-export const DoModrinthOauth = (curseforgeSlug, modrinthId) => {
- const uri = CreateModrinthUri(curseforgeSlug, modrinthId);
- window.location.href = uri;
+ uri.searchParams.set('curseforgeSlug', curseforgeSlug);
+ uri.searchParams.set('modrinthId', modrinthId);
+ return `https://modrinth.com/auth/authorize?client_id=${process.env.REACT_APP_MODRINTH_OAUTH_CLIENT_ID}&redirect_uri=${encodeURIComponent(uri.toString())}&scope=PROJECT_WRITE+VERSION_CREATE`;
};
/**
@@ -32,10 +18,6 @@ export const DoModrinthOauth = (curseforgeSlug, modrinthId) => {
export const ModrinthOauthCallbackHandler = () => {
const params = new URLSearchParams(window.location.search);
if (!params.has('code')) return null;
-
- window.localStorage.setItem(OAUTH_KEY, params.get('code'));
- params.delete('code');
-
const ret = Object.fromEntries(params);
return Object.keys(ret).length ? ret : null;
};
diff --git a/processor/.gitignore b/processor/.gitignore
index 13a49d8..aaeab35 100644
--- a/processor/.gitignore
+++ b/processor/.gitignore
@@ -8,4 +8,6 @@ filekey.key
__pycache__
.envrc
-*.jar
\ No newline at end of file
+*.jar
+
+.venv
\ No newline at end of file