GitHub sign-in

This commit is contained in:
MeowcaTheoRange 2024-04-25 10:50:47 -05:00
parent 191a7b6b67
commit 7685cff9ee
5 changed files with 55 additions and 17 deletions

View file

@ -86,11 +86,12 @@ export default async function Home({
<ConditionalNull condition={existingUser == null}>
<h2>Log in with the Fediverse</h2>
<p>If you'd like to participate in this jam, feel free to log in!</p>
<p>To log in with GitHub, enter "github.com"</p>
<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 type="submit" />
</form>
<p><small>Tested on Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
<p><small>Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
</ConditionalNull>
<ConditionalNull condition={existingUser != null}>
<div>

View file

@ -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,

View file

@ -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`);
}

View file

@ -58,11 +58,12 @@ export default async function Home({
<p>Enjoy!</p>
<ConditionalNull condition={existingUser == null}>
<h2>Log in with the Fediverse</h2>
<p>To log in with GitHub, enter "github.com"</p>
<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 type="submit" />
</form>
<p><small>Tested on Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
<p><small>Tested on GitHub, Mastodon, GoToSocial, Pleroma, and Misskey</small></p>
</ConditionalNull>
<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>

View file

@ -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);
if (this.instance != "github.com")
formData.append('grant_type', 'authorization_code');
formData.append('code', code);
if (this.instance != "github.com")
formData.append('scope', 'read');
let appRequest;
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 {
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;
}