Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved email whitelisting for self-hosters #877

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/webapp/app/models/user.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export async function findOrCreateGithubUser({
authenticationProfile,
authenticationExtraParams,
}: FindOrCreateGithub): Promise<LoggedInUser> {
if (env.WHITELISTED_EMAILS && !new RegExp(env.WHITELISTED_EMAILS).test(email)) {
throw new Error("This email is unauthorized");
}

const name = authenticationProfile._json.name;
let avatarUrl: string | undefined = undefined;
if (authenticationProfile.photos[0]) {
Expand Down
33 changes: 27 additions & 6 deletions apps/webapp/app/routes/login._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { LoginPageLayout } from "~/components/LoginPageLayout";
import { Button, LinkButton } from "~/components/primitives/Buttons";
import { Fieldset } from "~/components/primitives/Fieldset";
import { FormError } from "~/components/primitives/FormError";
import { Header1 } from "~/components/primitives/Headers";
import { NamedIcon } from "~/components/primitives/NamedIcon";
import { Paragraph } from "~/components/primitives/Paragraph";
Expand All @@ -19,6 +20,10 @@ import type { LoaderType as RootLoader } from "~/root";
import { isGithubAuthSupported } from "~/services/auth.server";
import { commitSession, setRedirectTo } from "~/services/redirectTo.server";
import { getUserId } from "~/services/session.server";
import {
getUserSession,
commitSession as commitAuthSession,
} from "~/services/sessionStorage.server";
import { appEnvTitleTag } from "~/utils";
import { requestUrl } from "~/utils/requestUrl.server";

Expand All @@ -41,22 +46,36 @@ export async function loader({ request }: LoaderFunctionArgs) {
const url = requestUrl(request);
const redirectTo = url.searchParams.get("redirectTo");

const session = await getUserSession(request);
const error = session.get("auth:error");

let githubError: string | undefined;
if (error) {
if ("message" in error) {
githubError = error.message;
} else {
githubError = JSON.stringify(error, null, 2);
}
}

if (redirectTo) {
const session = await setRedirectTo(request, redirectTo);

return typedjson(
{ redirectTo, showGithubAuth: isGithubAuthSupported },
{ redirectTo, showGithubAuth: isGithubAuthSupported, githubError },
{
headers: {
"Set-Cookie": await commitSession(session),
},
}
);
} else {
return typedjson({
redirectTo: null,
showGithubAuth: isGithubAuthSupported,
});
return typedjson(
{ redirectTo: null, showGithubAuth: isGithubAuthSupported, githubError },
{
headers: { "Set-Cookie": await commitAuthSession(session) },
}
);
}
}

Expand All @@ -78,7 +97,7 @@ export default function LoginPage() {
Create an account or login
</Paragraph>
<Fieldset className="w-full">
<div className="flex flex-col gap-y-2">
<div className="flex flex-col items-center gap-y-2">
{data.showGithubAuth && (
<Button
type="submit"
Expand All @@ -102,6 +121,8 @@ export default function LoginPage() {
/>
Continue with Email
</LinkButton>

{data.githubError && <FormError>{data.githubError}</FormError>}
</div>
<Paragraph variant="extra-small" className="mt-2 text-center">
By signing up you agree to our{" "}
Expand Down
16 changes: 15 additions & 1 deletion apps/webapp/app/routes/login.magic/route.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InboxArrowDownIcon } from "@heroicons/react/24/solid";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node";
import { redirect } from "@remix-run/node";
import { Form, useNavigation } from "@remix-run/react";
import { Form, useActionData, useNavigation } from "@remix-run/react";
import { getMatchesData, metaV1 } from "@remix-run/v1-meta";
import {
TypedMetaFunction,
Expand Down Expand Up @@ -74,6 +74,18 @@ export async function action({ request }: ActionFunctionArgs) {
.parse(payload);

if (action === "send") {
const { email } = z
.object({
email: z.string().email(),
})
.parse(payload);

if (process.env.WHITELISTED_EMAILS && !new RegExp(process.env.WHITELISTED_EMAILS).test(email)) {
return {
error: "This email is unauthorized",
};
}

return authenticator.authenticate("email-link", request, {
successRedirect: "/login/magic",
failureRedirect: "/login/magic",
Expand All @@ -92,6 +104,7 @@ export async function action({ request }: ActionFunctionArgs) {

export default function LoginMagicLinkPage() {
const { magicLinkSent, magicLinkError } = useTypedLoaderData<typeof loader>();
const actionData = useActionData<typeof action>();
const navigate = useNavigation();

const isLoading =
Expand Down Expand Up @@ -176,6 +189,7 @@ export default function LoginMagicLinkPage() {
/>
{isLoading ? "Sending…" : "Send a magic link"}
</Button>
{actionData?.error && <FormError>{actionData?.error}</FormError>}
{magicLinkError && <FormError>{magicLinkError}</FormError>}
</Fieldset>
<Paragraph variant="extra-small" className="mb-4 mt-6 text-center">
Expand Down