DISCORD INTEGRATION FOR SOME GOD FORSAKEN REASON

This commit is contained in:
MeowcaTheoRange 2024-04-25 17:51:50 -05:00
parent 4f6298d380
commit dd0225fea4
5 changed files with 56 additions and 18 deletions

View file

@ -95,15 +95,16 @@ export default async function Home({
<p>Hosted by <a href={`/jams/user/${jamOwner.id}`}>{`${jamOwner.username}@${jamOwner.instance}`}</a></p> <p>Hosted by <a href={`/jams/user/${jamOwner.id}`}>{`${jamOwner.username}@${jamOwner.instance}`}</a></p>
<p>{jam.description}</p> <p>{jam.description}</p>
<ConditionalNull condition={existingUser == null}> <ConditionalNull condition={existingUser == null}>
<h2>Join with the Fediverse</h2> <h2>Join abtmtr.link Jams</h2>
<p>If you'd like to participate in this jam, you should join!</p> <p>If you'd like to participate in any jam, you should join!</p>
<p>You'll also be able to participate an any future jams should they come to fruition.</p> <p><small>To join with the Fediverse, enter your instance domain.<br />
<p>To join with GitHub instead, enter "github.com".</p> To join with GitHub, enter "github.com".<br />
To join with Discord, enter "discord.com".</small></p>
<form action="/jams/oauth/login"> <form action="/jams/oauth/login">
<input name="instance" placeholder="Instance URL (e.g. &quot;social.besties.house&quot; or &quot;woem.men&quot;)" type="text" /> <input name="instance" placeholder="Domain (e.g. &quot;social.besties.house&quot; or &quot;woem.men&quot;)" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
<p><small>Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p> <p><small>Tested on Discord, GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
</ConditionalNull> </ConditionalNull>
<ConditionalNull condition={existingUser != null}> <ConditionalNull condition={existingUser != null}>
<div> <div>

View file

@ -30,7 +30,7 @@ export async function GET(request: NextRequest) {
// Get temporary user token // Get temporary user token
let tUserToken = await mauth.getApplicationToken(existingInstanceApp, code); let tUserToken = await mauth.getApplicationToken(existingInstanceApp, code);
console.log(tUserToken); console.log("a", tUserToken);
if (tUserToken == null) return new Response('', { if (tUserToken == null) return new Response('', {
status: 500 status: 500
@ -64,12 +64,27 @@ export async function GET(request: NextRequest) {
// Impersonation check // Impersonation check
if (!tUserExists.url.includes(instance) && !tUserExists.url.includes(tUserExists.acct)) return new Response(`URL is invalid`, { if (instance == "discord.com") {
status: 401 if (tUserExists.application.id != existingInstanceApp.client_id) return new Response(`URL is invalid`, {
}); status: 401
});
} else {
if (!tUserExists.url.includes(instance) && !tUserExists.url.includes(tUserExists.acct)) return new Response(`URL is invalid`, {
status: 401
});
}
let currentUser; let currentUser;
if (instance == "github.com") currentUser = { if (instance == "discord.com") currentUser = {
id: nanoid(21),
instance,
username: tUserExists.user.username,
admin: false,
url: `https://discord.com/users/${tUserExists.user.id}`,
banned: false, // Discord should be on tier1.
joined: Date.now()
} as UserTable;
else if (instance == "github.com") currentUser = {
id: nanoid(21), id: nanoid(21),
instance, instance,
username: tUserExists.login, username: tUserExists.login,

View file

@ -82,6 +82,8 @@ export async function GET(request: NextRequest) {
cookieStore.set("instance", instance, { cookieStore.set("instance", instance, {
expires: Date.now() + 604800000 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`)
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=${Math.random().toString(36).slice(2)}`)
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`);

View file

@ -60,14 +60,16 @@ export default async function Home({
<p>These serve many purposes. To create a community bond, to help creatives find their way, and most importantly, <s>to make me popular</s>. X3</p> <p>These serve many purposes. To create a community bond, to help creatives find their way, and most importantly, <s>to make me popular</s>. X3</p>
<p>Enjoy!</p> <p>Enjoy!</p>
<ConditionalNull condition={existingUser == null}> <ConditionalNull condition={existingUser == null}>
<h2>Join with the Fediverse</h2> <h2>Join abtmtr.link Jams</h2>
<p>If you'd like to participate in any jam, you should join!</p> <p>If you'd like to participate in any jam, you should join!</p>
<p>To join with GitHub, enter "github.com".</p> <p><small>To join with the Fediverse, enter your instance domain.<br />
To join with GitHub, enter "github.com".<br />
To join with Discord, enter "discord.com".</small></p>
<form action="/jams/oauth/login"> <form action="/jams/oauth/login">
<input name="instance" placeholder="Instance URL (e.g. &quot;social.besties.house&quot; or &quot;woem.men&quot;)" type="text" /> <input name="instance" placeholder="Domain (e.g. &quot;social.besties.house&quot; or &quot;woem.men&quot;)" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
<p><small>Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p> <p><small>Tested on Discord, GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
</ConditionalNull> </ConditionalNull>
<ConditionalNull condition={existingUser != null}> <ConditionalNull condition={existingUser != null}>
<p>Logged in as <a href={`/jams/user/${existingUser?.id}`}>{existingUser?.username}@{existingUser?.instance}</a> (<a href="/jams/oauth/logout">Logout</a>)</p> <p>Logged in as <a href={`/jams/user/${existingUser?.id}`}>{existingUser?.username}@{existingUser?.instance}</a> (<a href="/jams/oauth/logout">Logout</a>)</p>

View file

@ -76,12 +76,22 @@ export class MastoAuth {
if (this.instance != "github.com") if (this.instance != "github.com")
formData.append('grant_type', 'authorization_code'); formData.append('grant_type', 'authorization_code');
formData.append('code', code); formData.append('code', code);
if (this.instance != "github.com") if (this.instance == "discord.com")
formData.append('scope', 'identify');
else if (this.instance != "github.com")
formData.append('scope', 'read'); formData.append('scope', 'read');
let appRequest; let appRequest;
try { try {
if (this.instance == "github.com") { if (this.instance == "discord.com") {
appRequest = await fetch(`https://${this.instance}/api/v10/oauth2/token`, {
body: formData,
method: "post",
headers: {
"Accept": "application/json"
}
});
} else if (this.instance == "github.com") {
appRequest = await fetch(`https://${this.instance}/login/oauth/access_token`, { appRequest = await fetch(`https://${this.instance}/login/oauth/access_token`, {
body: formData, body: formData,
method: "post", method: "post",
@ -96,6 +106,7 @@ export class MastoAuth {
}); });
} }
} catch (err) { } catch (err) {
console.log(err);
return null; return null;
} }
@ -113,7 +124,13 @@ export class MastoAuth {
async verifyUser(auth: string) { async verifyUser(auth: string) {
let appRequest; let appRequest;
try { try {
if (this.instance == "github.com") { if (this.instance == "discord.com") {
appRequest = await fetch(`https://${this.instance}/api/v10/oauth2/@me`, {
headers: {
"Authorization": auth
}
});
} else if (this.instance == "github.com") {
appRequest = await fetch(`https://api.${this.instance}/user`, { appRequest = await fetch(`https://api.${this.instance}/user`, {
headers: { headers: {
"Authorization": auth "Authorization": auth
@ -127,6 +144,7 @@ export class MastoAuth {
}); });
} }
} catch (err) { } catch (err) {
console.log(err);
return null; return null;
} }