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 { MastoAuth } from "@/lib/mastoauth/mastoauth";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
|
import crypto from "node:crypto";
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const cookieStore = cookies();
|
const cookieStore = cookies();
|
||||||
|
@ -9,6 +10,7 @@ export async function GET(request: NextRequest) {
|
||||||
if (instance == null) return new Response('', {
|
if (instance == null) return new Response('', {
|
||||||
status: 400
|
status: 400
|
||||||
});
|
});
|
||||||
|
let useMiAuth = false;
|
||||||
const mauth = new MastoAuth(instance);
|
const mauth = new MastoAuth(instance);
|
||||||
|
|
||||||
// Check if instance is fediblocked
|
// Check if instance is fediblocked
|
||||||
|
@ -56,16 +58,23 @@ export async function GET(request: NextRequest) {
|
||||||
// Create new app
|
// Create new app
|
||||||
const temp_iapp = await mauth.newApplication();
|
const temp_iapp = await mauth.newApplication();
|
||||||
|
|
||||||
if (temp_iapp == null) return new Response('', {
|
// Misskey.
|
||||||
status: 400
|
if (temp_iapp != null)
|
||||||
});
|
|
||||||
|
|
||||||
existingInstanceApp = {
|
existingInstanceApp = {
|
||||||
instance_domain: instance,
|
instance_domain: instance,
|
||||||
client_id: temp_iapp.client_id,
|
client_id: temp_iapp.client_id,
|
||||||
client_secret: temp_iapp.client_secret,
|
client_secret: temp_iapp.client_secret,
|
||||||
redirect_uri: temp_iapp.redirect_uri
|
redirect_uri: temp_iapp.redirect_uri
|
||||||
} as AppTable;
|
} 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 {
|
try {
|
||||||
await db.insertInto('apps')
|
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, {
|
cookieStore.set("instance", instance, {
|
||||||
expires: Date.now() + 604800000
|
expires: Date.now() + 604800000
|
||||||
});
|
});
|
||||||
if (instance == "discord.com")
|
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")
|
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`);
|
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-MaterialSymbols": materialSymbols.style.fontFamily,
|
||||||
"--font-Renogare": renogare.style.fontFamily
|
"--font-Renogare": renogare.style.fontFamily
|
||||||
} as {[key: string]: string}}>
|
} as {[key: string]: string}}>
|
||||||
|
<head>
|
||||||
|
<link rel='redirect_uri' href='/jams/oauth/code'></link>
|
||||||
|
</head>
|
||||||
<body>{children}</body>
|
<body>{children}</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue