Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/islands/profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ export default function Profile({ isAuthed, userData, authUrl }) {
</div>
</div>
);
}
}
17 changes: 15 additions & 2 deletions src/lib/actions/do-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default async function doAuth(astroGlobal) {

/**
* Generate OAuth Url to start authorization flow
* @todo improvement: store `state` in cookie for later retrieval/comparison with auth `state` in `github/oauth/callback`
* @param {{ path: string }} state
*/
function getAuthUrl(state) {
Expand All @@ -39,10 +38,16 @@ export default async function doAuth(astroGlobal) {
if (otherStates.length > 0) parsedState += `|${otherStates}`;
}

const { url } = app.oauth.getWebFlowAuthorizationUrl({
const { url, state: generatedState } = app.oauth.getWebFlowAuthorizationUrl({
state: parsedState,
});

cookies.set("oauthState", generatedState, {
expires: resolveCookieExpiryDate(600),
path: "/",
encode: (value) => encrypt(value),
});

return url;
}

Expand All @@ -60,6 +65,14 @@ export default async function doAuth(astroGlobal) {
}
}

const storedState = cookies.get("oauthState", {
decode: (value) => decrypt(value),
});

if (storedState !== searchParams.get("state")) {
throw new Error("Invalid OAuth state");
}

const userOctokit = app.getUserOctokit({ token: accessToken.value });
const { data } = await userOctokit.request("GET /user");

Expand Down
14 changes: 13 additions & 1 deletion src/pages/api/github/oauth/callback.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { decrypt } from "../../../../lib/utils/crypto.js";

/**
* GitHub OAuth Callback route handler
* @param {import("astro").APIContext} context
*/
export async function GET({ url: { searchParams }, redirect }) {
export async function GET({ url: { searchParams }, redirect, cookies }) {
const code = searchParams.get("code");
const state = searchParams.get("state");

if (!code || !state) {
return new Response(null, { status: 400 });
}

const storedState = cookies.get("oauthState", {
decode: (value) => decrypt(value),
});

if (storedState !== state) {
return new Response("Invalid OAuth state", { status: 400 });
}

cookies.delete("oauthState");

const path = state.includes("path") && state.split("|")[0].split(":")[1];

if (path) return redirect(`${path}?code=${code}`);
Expand Down
14 changes: 10 additions & 4 deletions src/pages/logout.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
---
import doLogout from "../lib/actions/do-logout.js";
import doAuth from "../lib/actions/do-auth.js";

const { url: { searchParams }, redirect } = Astro;

const { isLoggedOut } = await doLogout(Astro);
if (isLoggedOut) return redirect(searchParams.get("return_to") || "/");
---
const { isAuthed, getAuthUrl } = await doAuth(Astro);

if (isAuthed) {
const { isLoggedOut } = await doAuth(Astro, { action: "logout" });
if (isLoggedOut) return redirect(searchParams.get("return_to") || "/");
} else {
return redirect(getAuthUrl({ path: "/logout" }));
}
---