-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
280 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { User } from "./session.server"; | ||
import type { GitHub } from "../services/github.server"; | ||
import type { SessionStorage } from "@remix-run/cloudflare"; | ||
|
||
import { createCookieSessionStorage } from "@remix-run/cloudflare"; | ||
import { Authenticator } from "remix-auth"; | ||
import { GitHubStrategy } from "remix-auth-github"; | ||
|
||
interface Services { | ||
gh: GitHub; | ||
} | ||
|
||
export class Auth { | ||
protected authenticator: Authenticator<User>; | ||
protected sessionStorage: SessionStorage; | ||
|
||
public authenticate: Authenticator<User>["authenticate"]; | ||
|
||
constructor( | ||
services: Services, | ||
clientID: string, | ||
clientSecret: string, | ||
sessionSecret = "s3cr3t", | ||
) { | ||
this.sessionStorage = createCookieSessionStorage({ | ||
cookie: { | ||
name: "sdx:auth", | ||
path: "/", | ||
maxAge: 60 * 60 * 24 * 365, // 1 year | ||
httpOnly: true, | ||
sameSite: "lax", | ||
secure: process.env.NODE_ENV === "production", | ||
secrets: [sessionSecret], | ||
}, | ||
}); | ||
|
||
this.authenticator = new Authenticator<User>(this.sessionStorage, { | ||
throwOnError: true, | ||
sessionKey: "token", | ||
}); | ||
|
||
this.authenticator.use( | ||
new GitHubStrategy( | ||
{ | ||
clientID, | ||
clientSecret, | ||
callbackURL: "/auth/github/callback", | ||
}, | ||
async ({ profile }) => { | ||
return { | ||
displayName: profile._json.name, | ||
username: profile._json.login, | ||
email: profile._json.email ?? profile.emails?.at(0) ?? null, | ||
avatar: profile._json.avatar_url, | ||
githubId: profile._json.node_id, | ||
isSponsor: await services.gh.isSponsoringMe(profile._json.node_id), | ||
}; | ||
}, | ||
), | ||
); | ||
|
||
this.authenticate = this.authenticator.authenticate.bind( | ||
this.authenticator, | ||
); | ||
} | ||
|
||
public async clear(request: Request) { | ||
let session = await this.sessionStorage.getSession( | ||
request.headers.get("cookie"), | ||
); | ||
return this.sessionStorage.destroySession(session); | ||
} | ||
} |
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,82 @@ | ||
import type { TypedSessionStorage } from "remix-utils/typed-session"; | ||
|
||
import { createWorkersKVSessionStorage, redirect } from "@remix-run/cloudflare"; | ||
import { createTypedSessionStorage } from "remix-utils/typed-session"; | ||
import { z } from "zod"; | ||
|
||
interface Services { | ||
kv: KVNamespace; | ||
} | ||
|
||
export const UserSchema = z.object({ | ||
username: z.string(), | ||
displayName: z.string(), | ||
email: z.string().email().nullable(), | ||
avatar: z.string().url(), | ||
githubId: z.string().min(1), | ||
isSponsor: z.boolean(), | ||
}); | ||
|
||
export type User = z.infer<typeof UserSchema>; | ||
|
||
export const SessionSchema = z.object({ | ||
user: UserSchema.optional(), | ||
}); | ||
|
||
export class SessionStorage { | ||
protected sessionStorage: TypedSessionStorage<typeof SessionSchema>; | ||
|
||
public read: TypedSessionStorage<typeof SessionSchema>["getSession"]; | ||
public commit: TypedSessionStorage<typeof SessionSchema>["commitSession"]; | ||
public destroy: TypedSessionStorage<typeof SessionSchema>["destroySession"]; | ||
|
||
constructor(services: Services, secret = "s3cr3t") { | ||
this.sessionStorage = createTypedSessionStorage({ | ||
sessionStorage: createWorkersKVSessionStorage({ | ||
kv: services.kv, | ||
cookie: { | ||
name: "sdx:session", | ||
path: "/", | ||
maxAge: 60 * 60 * 24 * 365, // 1 year | ||
httpOnly: true, | ||
sameSite: "lax", | ||
secure: process.env.NODE_ENV === "production", | ||
secrets: [secret], | ||
}, | ||
}), | ||
schema: SessionSchema, | ||
}); | ||
|
||
this.read = this.sessionStorage.getSession; | ||
this.commit = this.sessionStorage.commitSession; | ||
this.destroy = this.sessionStorage.destroySession; | ||
} | ||
|
||
static async logout(services: Services, request: Request, secret = "s3cr3t") { | ||
let sessionStorage = new SessionStorage(services, secret); | ||
let session = await sessionStorage.read(request.headers.get("cookie")); | ||
throw redirect("/", { | ||
headers: { "set-cookie": await sessionStorage.destroy(session) }, | ||
}); | ||
} | ||
|
||
static async readUser( | ||
services: Services, | ||
request: Request, | ||
secret = "s3cr3t", | ||
) { | ||
let sessionStorage = new SessionStorage(services, secret); | ||
let session = await sessionStorage.read(request.headers.get("cookie")); | ||
return session.get("user"); | ||
} | ||
|
||
static async requireUser( | ||
services: Services, | ||
request: Request, | ||
secret = "s3cr3t", | ||
) { | ||
let maybeUser = await this.readUser(services, request, secret); | ||
if (!maybeUser) throw redirect("/auth/login"); | ||
return maybeUser; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,37 @@ | ||
import type { LoaderFunctionArgs } from "@remix-run/cloudflare"; | ||
|
||
import { redirect, type LoaderFunctionArgs } from "@remix-run/cloudflare"; | ||
import { z } from "zod"; | ||
|
||
export function loader(_: LoaderFunctionArgs) { | ||
return _.context.time("routes/auth.$provider.callback#loader", async () => { | ||
let provider = z.enum(["github"]).parse(_.params.provider); | ||
import { Auth } from "~/modules/auth.server"; | ||
import { SessionStorage } from "~/modules/session.server"; | ||
import { GitHub } from "~/services/github.server"; | ||
|
||
export async function loader(_: LoaderFunctionArgs) { | ||
let provider = z.enum(["github"]).parse(_.params.provider); | ||
|
||
let gh = new GitHub(_.context.env.GH_APP_ID, _.context.env.GH_APP_PEM); | ||
|
||
let auth = new Auth( | ||
{ gh }, | ||
_.context.env.GITHUB_CLIENT_ID, | ||
_.context.env.GITHUB_CLIENT_SECRET, | ||
); | ||
|
||
let user = await auth.authenticate(provider, _.request); | ||
|
||
if (!user) throw redirect("/auth/login"); | ||
|
||
let sessionStorage = new SessionStorage( | ||
{ kv: _.context.kv.auth }, | ||
_.context.env.COOKIE_SESSION_SECRET, | ||
); | ||
|
||
let session = await sessionStorage.read(_.request.headers.get("cookie")); | ||
session.set("user", user); | ||
|
||
let headers = new Headers(); | ||
|
||
headers.append("set-cookie", await sessionStorage.commit(session)); | ||
headers.append("set-cookie", await auth.clear(_.request)); | ||
|
||
return await _.context.services.auth.authenticator.authenticate( | ||
provider, | ||
_.request, | ||
{ successRedirect: "/", failureRedirect: "/auth/login" }, | ||
); | ||
}); | ||
throw redirect("/", { headers }); | ||
} |
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.