diff --git a/src/app/jams/jam/[jam]/page.tsx b/src/app/jams/jam/[jam]/page.tsx index d0b5214..32ef85b 100644 --- a/src/app/jams/jam/[jam]/page.tsx +++ b/src/app/jams/jam/[jam]/page.tsx @@ -86,11 +86,12 @@ export default async function Home({

Log in with the Fediverse

If you'd like to participate in this jam, feel free to log in!

+

To log in with GitHub, enter "github.com"

-

Tested on Mastodon, GoToSocial, Pleroma, and Misskey

+

Tested on 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 3ffcf8e..2a97fc2 100644 --- a/src/app/jams/oauth/code/route.ts +++ b/src/app/jams/oauth/code/route.ts @@ -37,7 +37,10 @@ export async function GET(request: NextRequest) { }); // Test for user existence - let tUserExists = await mauth.verifyUser(tUserToken.token_type + " " + tUserToken.access_token); + let tUserExists; + if (instance == "github.com") + tUserExists = await mauth.verifyUser("Bearer " + tUserToken.access_token); + else tUserExists = await mauth.verifyUser(tUserToken.token_type + " " + tUserToken.access_token); console.log(tUserExists); @@ -65,7 +68,17 @@ export async function GET(request: NextRequest) { status: 401 }); - let currentUser = { + let currentUser; + if (instance == "github.com") currentUser = { + id: nanoid(21), + instance, + username: tUserExists.login, + admin: false, + url: tUserExists.html_url, + banned: false, // GitHub should not be on tier1. + joined: Date.now() + } as UserTable; + else currentUser = { id: nanoid(21), instance, username: tUserExists.acct, diff --git a/src/app/jams/oauth/login/route.ts b/src/app/jams/oauth/login/route.ts index b601b5c..c7154be 100644 --- a/src/app/jams/oauth/login/route.ts +++ b/src/app/jams/oauth/login/route.ts @@ -43,6 +43,7 @@ export async function GET(request: NextRequest) { }); // Check if instance app exists + // For github.com, it should let existingInstanceApp = await db .selectFrom('apps') .where('apps.instance_domain', '=', instance) @@ -81,5 +82,7 @@ export async function GET(request: NextRequest) { cookieStore.set("instance", instance, { expires: Date.now() + 604800000 }); + 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`); } \ No newline at end of file diff --git a/src/app/jams/page.tsx b/src/app/jams/page.tsx index fb35977..2844a5d 100644 --- a/src/app/jams/page.tsx +++ b/src/app/jams/page.tsx @@ -58,11 +58,12 @@ export default async function Home({

Enjoy!

Log in with the Fediverse

+

To log in with GitHub, enter "github.com"

-

Tested on Mastodon, GoToSocial, Pleroma, and Misskey

+

Tested on 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 b04a7c9..264c793 100644 --- a/src/lib/mastoauth/mastoauth.ts +++ b/src/lib/mastoauth/mastoauth.ts @@ -73,16 +73,28 @@ export class MastoAuth { formData.append('client_id', client.client_id); formData.append('client_secret', client.client_secret); formData.append('redirect_uri', client.redirect_uri); - formData.append('grant_type', 'authorization_code'); + if (this.instance != "github.com") + formData.append('grant_type', 'authorization_code'); formData.append('code', code); - formData.append('scope', 'read'); + if (this.instance != "github.com") + formData.append('scope', 'read'); let appRequest; - try { - appRequest = await fetch(`https://${this.instance}/oauth/token`, { - body: formData, - method: "post" - }); + try { + if (this.instance == "github.com") { + appRequest = await fetch(`https://${this.instance}/login/oauth/access_token`, { + body: formData, + method: "post", + headers: { + "Accept": "application/json" + } + }); + } else { + appRequest = await fetch(`https://${this.instance}/oauth/token`, { + body: formData, + method: "post" + }); + } } catch (err) { return null; } @@ -92,7 +104,7 @@ export class MastoAuth { access_token: string, token_type: string, scope: string, - created_at: number + created_at?: number } = await appRequest.json(); return reqEntities; } else return null; @@ -101,11 +113,19 @@ export class MastoAuth { async verifyUser(auth: string) { let appRequest; try { - appRequest = await fetch(`https://${this.instance}/api/v1/accounts/verify_credentials`, { - headers: { - "Authorization": auth - } - }); + if (this.instance == "github.com") { + appRequest = await fetch(`https://api.${this.instance}/user`, { + headers: { + "Authorization": auth + } + }); + } else { + appRequest = await fetch(`https://${this.instance}/api/v1/accounts/verify_credentials`, { + headers: { + "Authorization": auth + } + }); + } } catch (err) { return null; }