sergiodxa / remix-auth-oauth2

A OAuth2Strategy for Remix Auth
https://sergiodxa.github.io/remix-auth-oauth2/
MIT License
162 stars 63 forks source link

Enable OAuth to work with sessions that are using sameSite: Strict #11

Open ngbrown opened 2 years ago

ngbrown commented 2 years ago

There is a way, explored by Brock Allen, to use sameSite: "Strict" for the session cookie while using OAuth2 redirects for login.

I prototyped it some, and I used the OAuth state to store the session, but that's probably not ideal. Even when POSTing back to the callback. For the final success or failure redirects, I used the redirectHtml function shown below and that worked well. It allowed the final isAuthorized page to use the strict session cookies as normal.

The final part needed is probably to do the same for the incoming callback. That is, to store the information from the callback into a cookie, then perform an HTML redirect to pick up the session cookies and continue from there.

export function redirectHtml(url: string, init: number | ResponseInit = {}) {
  let responseInit = init;

  if (typeof responseInit === "number") {
    responseInit = {
      status: responseInit,
    };
  }

  let headers = new Headers(responseInit.headers);

  if (!headers.has("Content-Type")) {
    headers.set("Content-Type", "text/html; charset=utf-8");
  }

  const body = `<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="refresh" content="0;url=${url}" />
</head>
</html>`;

  return new Response(body, { ...responseInit, headers });
}
bitofbreeze commented 4 weeks ago

This might help https://github.com/sergiodxa/remix-auth/issues/232#issuecomment-1612485969