More Cleanup & Fixes
This commit is contained in:
parent
1b21b0e3cf
commit
6ad5f703db
6 changed files with 77 additions and 78 deletions
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 }) => {
|
|||
</li>
|
||||
<li>Share this tool on Twitter, Discord or whatever you please! </li>
|
||||
</ul>
|
||||
|
||||
<div className="w-100 d-flex justify-content-center align-items-center">
|
||||
<Button variant="success" className="modrinth" onClick={DoModrinthOauth}>
|
||||
Authenticate with Modrinth
|
||||
</Button>
|
||||
</div>
|
||||
</Modal.Body>
|
||||
</Modal>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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 () => {
|
|||
<b>M</b>
|
||||
igrator
|
||||
</p>
|
||||
<Form onSubmit={makeJob} className="juse-form">
|
||||
<Form onSubmit={startOauthFlow} className="juse-form">
|
||||
<Form.Group className="mb-3" controlId="slug">
|
||||
<Form.Label>CurseForge Project Slug</Form.Label>
|
||||
<Form.Control
|
||||
|
|
|
|||
|
|
@ -1,29 +1,15 @@
|
|||
const OAUTH_KEY = 'modrinth-oauth-token';
|
||||
|
||||
/**
|
||||
* Creates a modrinth URI with a given URL encoded callback
|
||||
* @param {string | null} curseforgeSlug
|
||||
* @param {string | null} modrinthId
|
||||
* @returns {string} The callback URI
|
||||
*/
|
||||
const CreateModrinthUri = (curseforgeSlug, modrinthId) => {
|
||||
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;
|
||||
};
|
||||
|
|
|
|||
4
processor/.gitignore
vendored
4
processor/.gitignore
vendored
|
|
@ -8,4 +8,6 @@ filekey.key
|
|||
__pycache__
|
||||
.envrc
|
||||
|
||||
*.jar
|
||||
*.jar
|
||||
|
||||
.venv
|
||||
Loading…
Reference in a new issue