-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let people log into the server with their github accounts.
- Loading branch information
Showing
16 changed files
with
289 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- CreateTable | ||
CREATE TABLE "GithubUser" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"username" TEXT NOT NULL, | ||
"accessToken" TEXT, | ||
"refreshToken" TEXT, | ||
"accessTokenExpires" DATETIME, | ||
"refreshTokenExpires" DATETIME | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Session" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"userId" TEXT NOT NULL, | ||
"expires" DATETIME NOT NULL, | ||
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "GithubUser" ("id") ON DELETE CASCADE ON UPDATE CASCADE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import type { | ||
GitHubAppUserAuthentication, | ||
GitHubAppUserAuthenticationWithExpiration, | ||
} from "@octokit/auth-oauth-app"; | ||
import type { Prisma } from "@prisma/client"; | ||
import type { AstroCookies } from "astro"; | ||
import crypto from "node:crypto"; | ||
import util from "node:util"; | ||
import { app } from "./github/auth"; | ||
import { prisma } from "./prisma"; | ||
|
||
// Can't use __Host- because of https://crbug.com/40196122. | ||
const LOGIN_COOKIE_NAME = "Session"; | ||
|
||
const randomBytes = util.promisify(crypto.randomBytes); | ||
|
||
export type User = { | ||
/** The active user's GraphQL node ID. */ | ||
githubId: string; | ||
username: string; | ||
}; | ||
|
||
type GithubRestApiUser = { | ||
login: string; | ||
/** GraphQL ID */ | ||
node_id: string; | ||
}; | ||
|
||
export async function finishLogin( | ||
cookies: AstroCookies, | ||
{ login, node_id }: GithubRestApiUser, | ||
{ | ||
token, | ||
refreshToken, | ||
expiresAt, | ||
refreshTokenExpiresAt, | ||
}: GitHubAppUserAuthentication & | ||
Partial<GitHubAppUserAuthenticationWithExpiration>, | ||
) { | ||
const sessionId = (await randomBytes(128 / 8)).toString("base64url"); | ||
const sessionExpires = new Date(); | ||
sessionExpires.setDate(sessionExpires.getDate() + 31); | ||
const update: Omit<Prisma.GithubUserCreateInput, "id"> = { | ||
username: login, | ||
accessToken: token, | ||
accessTokenExpires: expiresAt, | ||
refreshToken, | ||
refreshTokenExpires: refreshTokenExpiresAt, | ||
sessions: { create: { id: sessionId, expires: sessionExpires } }, | ||
}; | ||
await prisma.githubUser.upsert({ | ||
where: { id: node_id }, | ||
create: { | ||
id: node_id, | ||
...update, | ||
}, | ||
update, | ||
}); | ||
cookies.set(LOGIN_COOKIE_NAME, sessionId, { | ||
httpOnly: true, | ||
secure: true, | ||
path: "/", | ||
expires: sessionExpires, | ||
}); | ||
} | ||
|
||
export async function getLogin(cookies: AstroCookies): Promise<User | null> { | ||
const sessionId = cookies.get(LOGIN_COOKIE_NAME)?.value; | ||
if (!sessionId) return null; | ||
const session = await prisma.session.findUnique({ | ||
where: { id: sessionId }, | ||
include: { user: true }, | ||
}); | ||
if (!session) return null; | ||
const now = new Date(); | ||
if (session.expires < now) { | ||
void prisma.session | ||
.deleteMany({ | ||
where: { expires: { lte: now } }, | ||
}) | ||
.catch((e: unknown) => { | ||
console.error(e instanceof Error ? e.stack : e); | ||
}); | ||
cookies.delete(LOGIN_COOKIE_NAME, { path: "/" }); | ||
return null; | ||
} | ||
return { githubId: session.user.id, username: session.user.username }; | ||
} | ||
|
||
export async function logout(cookies: AstroCookies): Promise<void> { | ||
const sessionId = cookies.get(LOGIN_COOKIE_NAME)?.value; | ||
if (!sessionId) return; | ||
const { user } = await prisma.session.delete({ | ||
where: { id: sessionId }, | ||
include: { user: true }, | ||
}); | ||
cookies.delete(LOGIN_COOKIE_NAME); | ||
if (user.accessToken) { | ||
await app?.oauth.deleteToken({ token: user.accessToken }); | ||
} | ||
await prisma.githubUser.update({ | ||
where: { id: user.id }, | ||
data: { | ||
accessToken: null, | ||
accessTokenExpires: null, | ||
refreshToken: null, | ||
refreshTokenExpires: null, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// At this path for compatibility with the octokit middleware. | ||
|
||
import type { APIRoute } from "astro"; | ||
import { app } from "../../../../lib/github/auth"; | ||
import { finishLogin } from "../../../../lib/login"; | ||
|
||
export const GET: APIRoute = async ({ url, cookies, redirect }) => { | ||
const code = url.searchParams.get("code"); | ||
const state = url.searchParams.get("state") ?? "/"; | ||
const next = URL.canParse(state, url.href) ? state : "/"; | ||
if (app && code) { | ||
const { authentication } = await app.oauth.createToken({ code }); | ||
const { | ||
data: { user }, | ||
} = await app.oauth.checkToken({ token: authentication.token }); | ||
if (user) { | ||
await finishLogin(cookies, user, authentication); | ||
} | ||
} | ||
return redirect(next, 303); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { APIRoute } from "astro"; | ||
import { logout } from "../../lib/login"; | ||
|
||
export const GET: APIRoute = async ({ url, cookies, redirect }) => { | ||
await logout(cookies); | ||
const next = url.searchParams.get("next") ?? "/"; | ||
return redirect(next, 303); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.