Misskey Shit
This commit is contained in:
parent
6e560d8bba
commit
47afdbadd9
2 changed files with 42 additions and 12 deletions
|
@ -2,6 +2,7 @@ import { AppTable, db } from "@/lib/mastoauth/kysely";
|
|||
import { MastoAuth } from "@/lib/mastoauth/mastoauth";
|
||||
import { cookies } from "next/headers";
|
||||
import { NextRequest } from "next/server";
|
||||
import crypto from "node:crypto";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const cookieStore = cookies();
|
||||
|
@ -9,6 +10,7 @@ export async function GET(request: NextRequest) {
|
|||
if (instance == null) return new Response('', {
|
||||
status: 400
|
||||
});
|
||||
let useMiAuth = false;
|
||||
const mauth = new MastoAuth(instance);
|
||||
|
||||
// Check if instance is fediblocked
|
||||
|
@ -56,16 +58,23 @@ export async function GET(request: NextRequest) {
|
|||
// Create new app
|
||||
const temp_iapp = await mauth.newApplication();
|
||||
|
||||
if (temp_iapp == null) return new Response('', {
|
||||
status: 400
|
||||
});
|
||||
|
||||
existingInstanceApp = {
|
||||
instance_domain: instance,
|
||||
client_id: temp_iapp.client_id,
|
||||
client_secret: temp_iapp.client_secret,
|
||||
redirect_uri: temp_iapp.redirect_uri
|
||||
} as AppTable;
|
||||
// Misskey.
|
||||
if (temp_iapp != null)
|
||||
existingInstanceApp = {
|
||||
instance_domain: instance,
|
||||
client_id: temp_iapp.client_id,
|
||||
client_secret: temp_iapp.client_secret,
|
||||
redirect_uri: temp_iapp.redirect_uri
|
||||
} as AppTable;
|
||||
else {
|
||||
useMiAuth = true;
|
||||
existingInstanceApp = {
|
||||
instance_domain: instance,
|
||||
client_id: "https://abtmtr.link/jams/",
|
||||
client_secret: "MISSKEY",
|
||||
redirect_uri: "https://abtmtr.link/jams/oauth/code"
|
||||
} as AppTable;
|
||||
}
|
||||
|
||||
try {
|
||||
await db.insertInto('apps')
|
||||
|
@ -79,12 +88,30 @@ export async function GET(request: NextRequest) {
|
|||
}
|
||||
}
|
||||
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~";
|
||||
const codeVerifier = new Array(128)
|
||||
.fill(0)
|
||||
.map(() => chars[Math.floor(chars.length * Math.random())])
|
||||
.join("");
|
||||
console.log('code_verifier', codeVerifier);
|
||||
|
||||
const codeChallenge = crypto
|
||||
.createHash("sha256")
|
||||
.update(codeVerifier, "ascii")
|
||||
.digest("base64url");
|
||||
console.log('code_challenge', codeChallenge);
|
||||
|
||||
const state = crypto.randomUUID();
|
||||
console.log('state', state);
|
||||
|
||||
cookieStore.set("instance", instance, {
|
||||
expires: Date.now() + 604800000
|
||||
});
|
||||
if (instance == "discord.com")
|
||||
return Response.redirect(`https://${instance}/oauth2/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=identify&state=${Math.random().toString(36).slice(2)}&integration_type=1`)
|
||||
return Response.redirect(`https://${instance}/oauth2/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=identify&state=${state}&integration_type=1`)
|
||||
if (instance == "github.com")
|
||||
return Response.redirect(`https://${instance}/login/oauth/authorize?client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=&state=${Math.random().toString(36).slice(2)}`)
|
||||
return Response.redirect(`https://${instance}/login/oauth/authorize?client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=&state=${state}`)
|
||||
if (useMiAuth)
|
||||
return Response.redirect(`https://${instance}/oauth/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&state=${state}&code_challenge=${codeChallenge}&code_challenge_method=S256&scope=read`);
|
||||
return Response.redirect(`https://${instance}/oauth/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=read`);
|
||||
}
|
|
@ -22,6 +22,9 @@ export default function RootLayout({
|
|||
"--font-MaterialSymbols": materialSymbols.style.fontFamily,
|
||||
"--font-Renogare": renogare.style.fontFamily
|
||||
} as {[key: string]: string}}>
|
||||
<head>
|
||||
<link rel='redirect_uri' href='/jams/oauth/code'></link>
|
||||
</head>
|
||||
<body>{children}</body>
|
||||
</html>
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue