|
| 1 | +"use client" |
| 2 | +import { readAuthData } from "@/auth_context"; |
| 3 | +import { useRouter } from "next/navigation"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | +import { getOauthBrowserKey, getCallbackUrl } from "../auth"; |
| 6 | +import { redirectBack } from "../callback/page"; |
| 7 | + |
| 8 | + |
| 9 | +export default function Inner(props: {redirect_url: string}) { |
| 10 | + const router = useRouter() |
| 11 | + const redirect = props.redirect_url |
| 12 | + // We redirect the user to OAUTH themselves with modrinth |
| 13 | + // This is documented on https://docs.modrinth.com/guide/oauth/ |
| 14 | + // The flow is as follows: |
| 15 | + // * The user tries to access an authenticated page, they do not have a token in localStorage |
| 16 | + // * The user is redirected to this page, the previous page they were on is stored in a query parameter named `r` |
| 17 | + // * The user clicks the "log in with Modrinth" button, and gets sent to a modrinth page |
| 18 | + // * The user completes the login in modrinth, and is redirected to our special "callback" page |
| 19 | + // * The callback page reads the code which modrinth passed through in a query parameter |
| 20 | + // * The callback page sends this code to the server, where it's combined with an application secret key to |
| 21 | + // obtain a modrinth token |
| 22 | + // * The callback page reads the `r` query parameter and redirects back to the original page |
| 23 | + |
| 24 | + // We want to check if the user got sent here erroneously, |
| 25 | + // aka if they got authenticated in the meantime |
| 26 | + const checkLoggedIn = () => { |
| 27 | + const a = readAuthData() |
| 28 | + if (a && a.isValid()) { |
| 29 | + redirectBack(router, redirect) |
| 30 | + } |
| 31 | + }; |
| 32 | + useEffect(() => checkLoggedIn(), []); // Wrapped in useEffect so it only runs on client |
| 33 | + |
| 34 | + // We also want to keep an eye on localStorage. It might be that the user has multiple tabs open, |
| 35 | + // and logged in on aNextRouter different tab. |
| 36 | + useEffect(() => { |
| 37 | + const controller = new AbortController(); |
| 38 | + addEventListener("storage", () => { |
| 39 | + checkLoggedIn() |
| 40 | + }, { signal: controller.signal }) |
| 41 | + return () => controller.abort() |
| 42 | + }, []) |
| 43 | + |
| 44 | + // These need to be in a useEffect because they must run on the client and not the server |
| 45 | + const [oauthBrowserKey, setOauthBrowserKey] = useState<string | undefined>(undefined) |
| 46 | + useEffect(() => { |
| 47 | + if (!oauthBrowserKey) { |
| 48 | + setOauthBrowserKey(getOauthBrowserKey()) |
| 49 | + } |
| 50 | + }, [oauthBrowserKey]); |
| 51 | + |
| 52 | + const [callbackUrl, setCallbackUrl] = useState<string | undefined>(undefined) |
| 53 | + useEffect(() => { |
| 54 | + if (!callbackUrl) { |
| 55 | + setCallbackUrl(getCallbackUrl()) |
| 56 | + } |
| 57 | + }, [callbackUrl]); |
| 58 | + |
| 59 | + if (!callbackUrl) { |
| 60 | + return <main> |
| 61 | + <center> |
| 62 | + <h1>ModFest panel</h1> |
| 63 | + Log in with Modrinth |
| 64 | + </center> |
| 65 | + </main> |
| 66 | + } |
| 67 | + |
| 68 | + // We can provide a "state" parameter to modrinth. This will be passed on unmodified to the callback |
| 69 | + // We use it for two reasons. Firstly, we insert a random browser-specific value into it. |
| 70 | + // If we didn't do this, anyone could create a link to our callback page. When someone clicks |
| 71 | + // that link the callback will blindly assume the token is correct and it'll just be a |
| 72 | + // confusing mess. So we store some random value in localStorage, and any attacker won't be |
| 73 | + // able to guess that. |
| 74 | + // Secondly, we use the state variable to encode the return path. |
| 75 | + const state = oauthBrowserKey+"."+redirect |
| 76 | + |
| 77 | + const queryParams = { |
| 78 | + // This is the only auth type modrinth supports |
| 79 | + "response_type": "code", |
| 80 | + // Identifier for the modrinth app |
| 81 | + "client_id": process.env.NEXT_PUBLIC_MODRINTH_APP_ID!, |
| 82 | + // We only want the bare minimum scope, this should also be set in the modrinth application's settings (https://modrinth.com/settings/applications) |
| 83 | + "scope": "USER_READ", |
| 84 | + // The state variable (which will be passed on unmodified) |
| 85 | + "state": state, |
| 86 | + // The url for our callback page |
| 87 | + "redirect_uri": callbackUrl, |
| 88 | + }; |
| 89 | + |
| 90 | + const oathUrl = `${process.env.NEXT_PUBLIC_MODRINTH_SITE}/auth/authorize?` + Object.entries(queryParams).map(([k,v]) => k+"="+encodeURIComponent(v)).join("&") |
| 91 | + |
| 92 | + return ( |
| 93 | + <main> |
| 94 | + <center> |
| 95 | + <h1>ModFest panel</h1> |
| 96 | + <a href={oathUrl}>Log in with Modrinth</a> |
| 97 | + </center> |
| 98 | + </main> |
| 99 | + ); |
| 100 | +} |
0 commit comments