From dd0225fea4748e63d33d30fb19bd1e2589be0a66 Mon Sep 17 00:00:00 2001 From: MeowcaTheoRange Date: Thu, 25 Apr 2024 17:51:50 -0500 Subject: [PATCH] DISCORD INTEGRATION FOR SOME GOD FORSAKEN REASON --- src/app/jams/jam/[jam]/page.tsx | 13 +++++++------ src/app/jams/oauth/code/route.ts | 25 ++++++++++++++++++++----- src/app/jams/oauth/login/route.ts | 2 ++ src/app/jams/page.tsx | 10 ++++++---- src/lib/mastoauth/mastoauth.ts | 24 +++++++++++++++++++++--- 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/app/jams/jam/[jam]/page.tsx b/src/app/jams/jam/[jam]/page.tsx index a8949f7..8ec8b1f 100644 --- a/src/app/jams/jam/[jam]/page.tsx +++ b/src/app/jams/jam/[jam]/page.tsx @@ -95,15 +95,16 @@ export default async function Home({

Hosted by {`${jamOwner.username}@${jamOwner.instance}`}

{jam.description}

-

Join with the Fediverse

-

If you'd like to participate in this jam, you should join!

-

You'll also be able to participate an any future jams should they come to fruition.

-

To join with GitHub instead, enter "github.com".

+

Join abtmtr.link Jams

+

If you'd like to participate in any jam, you should join!

+

To join with the Fediverse, enter your instance domain.
+ To join with GitHub, enter "github.com".
+ To join with Discord, enter "discord.com".

- +
-

Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey

+

Tested on Discord, GitHub, Mastodon, GoToSocial, Pleroma, and Misskey

diff --git a/src/app/jams/oauth/code/route.ts b/src/app/jams/oauth/code/route.ts index 2a97fc2..c435b73 100644 --- a/src/app/jams/oauth/code/route.ts +++ b/src/app/jams/oauth/code/route.ts @@ -30,7 +30,7 @@ export async function GET(request: NextRequest) { // Get temporary user token let tUserToken = await mauth.getApplicationToken(existingInstanceApp, code); - console.log(tUserToken); + console.log("a", tUserToken); if (tUserToken == null) return new Response('', { status: 500 @@ -64,12 +64,27 @@ export async function GET(request: NextRequest) { // Impersonation check - if (!tUserExists.url.includes(instance) && !tUserExists.url.includes(tUserExists.acct)) return new Response(`URL is invalid`, { - status: 401 - }); + if (instance == "discord.com") { + 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; - 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), instance, username: tUserExists.login, diff --git a/src/app/jams/oauth/login/route.ts b/src/app/jams/oauth/login/route.ts index c7154be..a8884b0 100644 --- a/src/app/jams/oauth/login/route.ts +++ b/src/app/jams/oauth/login/route.ts @@ -82,6 +82,8 @@ export async function GET(request: NextRequest) { 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`) 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}/oauth/authorize?response_type=code&client_id=${existingInstanceApp.client_id}&redirect_uri=${existingInstanceApp.redirect_uri}&scope=read`); diff --git a/src/app/jams/page.tsx b/src/app/jams/page.tsx index d9b1beb..c99aca8 100644 --- a/src/app/jams/page.tsx +++ b/src/app/jams/page.tsx @@ -60,14 +60,16 @@ export default async function Home({

These serve many purposes. To create a community bond, to help creatives find their way, and most importantly, to make me popular. X3

Enjoy!

-

Join with the Fediverse

+

Join abtmtr.link Jams

If you'd like to participate in any jam, you should join!

-

To join with GitHub, enter "github.com".

+

To join with the Fediverse, enter your instance domain.
+ To join with GitHub, enter "github.com".
+ To join with Discord, enter "discord.com".

- +
-

Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey

+

Tested on Discord, GitHub, Mastodon, GoToSocial, Pleroma, and Misskey

Logged in as {existingUser?.username}@{existingUser?.instance} (Logout)

diff --git a/src/lib/mastoauth/mastoauth.ts b/src/lib/mastoauth/mastoauth.ts index 264c793..3b15cfd 100644 --- a/src/lib/mastoauth/mastoauth.ts +++ b/src/lib/mastoauth/mastoauth.ts @@ -76,12 +76,22 @@ export class MastoAuth { if (this.instance != "github.com") formData.append('grant_type', 'authorization_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'); let appRequest; 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`, { body: formData, method: "post", @@ -96,6 +106,7 @@ export class MastoAuth { }); } } catch (err) { + console.log(err); return null; } @@ -113,7 +124,13 @@ export class MastoAuth { async verifyUser(auth: string) { let appRequest; 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`, { headers: { "Authorization": auth @@ -127,6 +144,7 @@ export class MastoAuth { }); } } catch (err) { + console.log(err); return null; }