Skip to content

Commit

Permalink
Wrap SSR with auth requirements (#4129)
Browse files Browse the repository at this point in the history
* Add `withGetServerSidePropsRequirements` wrapper

* 👕

* ✨

* ✂️

* 👕

* Address comments from review
  • Loading branch information
flvndvd authored Mar 5, 2024
1 parent 2d43833 commit 744c8ac
Show file tree
Hide file tree
Showing 57 changed files with 262 additions and 193 deletions.
2 changes: 1 addition & 1 deletion front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ export class Authenticator {
export async function getSession(
req: NextApiRequest | GetServerSidePropsContext["req"],
res: NextApiResponse | GetServerSidePropsContext["res"]
): Promise<any> {
): Promise<any | null> {
return getServerSession(req, res, authOptions);
}

Expand Down
51 changes: 51 additions & 0 deletions front/lib/iam/session.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import type { RoleType, UserTypeWithWorkspaces } from "@dust-tt/types";
import type {
GetServerSideProps,
GetServerSidePropsContext,
PreviewData,
} from "next";
import type { ParsedUrlQuery } from "querystring";
import { Op } from "sequelize";

import { getSession } from "@app/lib/auth";
import {
fetchUserFromSession,
maybeUpdateFromExternalUser,
} from "@app/lib/iam/users";
import { Membership, Workspace } from "@app/lib/models";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export function isGoogleSession(session: any) {
return session.provider.provider === "google";
Expand Down Expand Up @@ -75,3 +83,46 @@ export async function getUserFromSession(
}),
};
}

interface WithGetServerSidePropsRequirementsOptions {
enableLogging?: boolean;
requireAuth?: boolean;
}

const defaultWithGetServerSidePropsRequirements: WithGetServerSidePropsRequirementsOptions =
{
enableLogging: true,
requireAuth: true,
};

export function withGetServerSidePropsRequirements<
T extends { [key: string]: any } = { [key: string]: any }
>(
getServerSideProps: GetServerSideProps<T>,
opts: WithGetServerSidePropsRequirementsOptions = defaultWithGetServerSidePropsRequirements
): GetServerSideProps<T> {
return async (
context: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>
) => {
const { enableLogging, requireAuth } = opts;

if (requireAuth) {
const session = await getSession(context.req, context.res);
if (!session) {
return {
redirect: {
permanent: false,
// TODO(2024-03-04 flav) Add support for `returnTo=`.
destination: "/",
},
};
}
}

if (enableLogging) {
return withGetServerSidePropsLogging(getServerSideProps)(context);
}

return getServerSideProps(context);
};
}
41 changes: 23 additions & 18 deletions front/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,41 @@ import ScrollingHeader from "@app/components/home/scrollingHeader";
import { PricePlans } from "@app/components/PlansTables";
import { getSession } from "@app/lib/auth";
import { getUserFromSession } from "@app/lib/iam/session";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { classNames } from "@app/lib/utils";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

const { GA_TRACKING_ID = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
gaTrackingId: string;
}>(async (context) => {
const session = await getSession(context.req, context.res);
const user = await getUserFromSession(session);
}>(
async (context) => {
const session = await getSession(context.req, context.res);
const user = await getUserFromSession(session);

if (user && user.workspaces.length > 0) {
let url = `/w/${user.workspaces[0].sId}`;
if (user && user.workspaces.length > 0) {
let url = `/w/${user.workspaces[0].sId}`;

if (context.query.inviteToken) {
url = `/api/login?inviteToken=${context.query.inviteToken}`;
if (context.query.inviteToken) {
url = `/api/login?inviteToken=${context.query.inviteToken}`;
}

return {
redirect: {
destination: url,
permanent: false,
},
};
}

return {
redirect: {
destination: url,
permanent: false,
},
props: { gaTrackingId: GA_TRACKING_ID },
};
},
{
requireAuth: false,
}

return {
props: { gaTrackingId: GA_TRACKING_ID },
};
});
);

export default function Home({
gaTrackingId,
Expand Down
27 changes: 16 additions & 11 deletions front/pages/login-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import { Button, Logo } from "@dust-tt/sparkle";
import type { InferGetServerSidePropsType } from "next";
import Link from "next/link";

import { withGetServerSidePropsLogging } from "@app/logger/withlogging";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";

const { URL = "", GA_TRACKING_ID = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
domain?: string;
gaTrackingId: string;
baseUrl: string;
}>(async (context) => {
return {
props: {
domain: context.query.domain as string,
baseUrl: URL,
gaTrackingId: GA_TRACKING_ID,
},
};
});
}>(
async (context) => {
return {
props: {
domain: context.query.domain as string,
baseUrl: URL,
gaTrackingId: GA_TRACKING_ID,
},
};
},
{
requireAuth: false,
}
);

export default function LoginError({
domain,
Expand Down
4 changes: 2 additions & 2 deletions front/pages/no-workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { useRouter } from "next/router";

import { getSession } from "@app/lib/auth";
import { getUserFromSession } from "@app/lib/iam/session";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { Membership, Workspace, WorkspaceHasDomain } from "@app/lib/models";
import logger from "@app/logger/logger";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

// Fetch workspace details for scenarios where auto-join is disabled.
async function fetchWorkspaceDetails(
Expand Down Expand Up @@ -55,7 +55,7 @@ async function fetchRevokedWorkspace(
return Workspace.findByPk(revokedWorkspaceId);
}

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
status: "auto-join-disabled" | "revoked";
userFirstName: string;
workspaceName: string;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/[wId]/assistants/[aId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type { InferGetServerSidePropsType } from "next";
import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getAgentConfigurations } from "@app/lib/api/assistant/configuration";
import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
agentConfigurations: AgentConfigurationType[];
}>(async (context) => {
const session = await getSession(context.req, context.res);
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/[wId]/data_sources/[name]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import { getDataSource } from "@app/lib/api/data_sources";
import { Authenticator, getSession } from "@app/lib/auth";
import { useSubmitFunction } from "@app/lib/client/utils";
import { getDisplayNameForDocument } from "@app/lib/data_sources";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { useDocuments } from "@app/lib/swr";
import { formatTimestampToFriendlyDate, timeAgoFrom } from "@app/lib/utils";
import logger from "@app/logger/logger";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

const { TEMPORAL_CONNECTORS_NAMESPACE = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
dataSource: DataSourceType;
coreDataSource: CoreAPIDataSource;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/[wId]/data_sources/[name]/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getDataSource } from "@app/lib/api/data_sources";
import { Authenticator, getSession } from "@app/lib/auth";
import { getDisplayNameForDocument } from "@app/lib/data_sources";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { classNames, timeAgoFrom } from "@app/lib/utils";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
dataSource: DataSourceType;
}>(async (context) => {
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/[wId]/data_sources/[name]/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import type { InferGetServerSidePropsType } from "next";
import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getDataSource } from "@app/lib/api/data_sources";
import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { classNames } from "@app/lib/utils";
import logger from "@app/logger/logger";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
document: CoreAPIDocument;
}>(async (context) => {
const session = await getSession(context.req, context.res);
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/[wId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ import {
import { Authenticator, getSession } from "@app/lib/auth";
import { useSubmitFunction } from "@app/lib/client/utils";
import { isDevelopment } from "@app/lib/development";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import {
FREE_TEST_PLAN_CODE,
FREE_UPGRADED_PLAN_CODE,
isUpgraded,
} from "@app/lib/plans/plan_codes";
import { getPlanInvitation } from "@app/lib/plans/subscription";
import { usePokePlans } from "@app/lib/swr";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
subscription: SubscriptionType;
planInvitation: PlanInvitationType | null;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/[wId]/memberships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import React from "react";
import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getMembers } from "@app/lib/api/workspace";
import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
members: UserTypeWithWorkspaces[];
}>(async (context) => {
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/connectors/[connectorId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { ConnectorType } from "@dust-tt/types";
import { ConnectorsAPI } from "@dust-tt/types";

import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import logger from "@app/logger/logger";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export const getServerSideProps = withGetServerSidePropsLogging<object>(
export const getServerSideProps = withGetServerSidePropsRequirements<object>(
async (context) => {
const session = await getSession(context.req, context.res);
const auth = await Authenticator.fromSuperUserSession(session, null);
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import React, { useState } from "react";

import PokeNavbar from "@app/components/poke/PokeNavbar";
import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { usePokeWorkspaces } from "@app/lib/swr";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export const getServerSideProps = withGetServerSidePropsLogging<object>(
export const getServerSideProps = withGetServerSidePropsRequirements<object>(
async (context) => {
const session = await getSession(context.req, context.res);
const auth = await Authenticator.fromSuperUserSession(session, null);
Expand Down
4 changes: 2 additions & 2 deletions front/pages/poke/plans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
import PokeNavbar from "@app/components/poke/PokeNavbar";
import { SendNotificationsContext } from "@app/components/sparkle/Notification";
import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { usePokePlans } from "@app/lib/swr";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

export const getServerSideProps = withGetServerSidePropsLogging<object>(
export const getServerSideProps = withGetServerSidePropsRequirements<object>(
async (context) => {
const session = await getSession(context.req, context.res);
const auth = await Authenticator.fromSuperUserSession(session, null);
Expand Down
4 changes: 2 additions & 2 deletions front/pages/w/[wId]/a/[aId]/clone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import WorkspacePicker from "@app/components/WorkspacePicker";
import { getApp } from "@app/lib/api/app";
import { Authenticator, getSession } from "@app/lib/auth";
import { getUserFromSession } from "@app/lib/iam/session";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { classNames } from "@app/lib/utils";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

const { GA_TRACKING_ID = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
user: UserTypeWithWorkspaces;
owner: WorkspaceType;
subscription: SubscriptionType;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/w/[wId]/a/[aId]/datasets/[name]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { getApp } from "@app/lib/api/app";
import { getDatasetHash, getDatasetSchema } from "@app/lib/api/datasets";
import { Authenticator, getSession } from "@app/lib/auth";
import { useRegisterUnloadHandlers } from "@app/lib/front";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";

const { GA_TRACKING_ID = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
subscription: SubscriptionType;
readOnly: boolean;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/w/[wId]/a/[aId]/datasets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {
import { getApp } from "@app/lib/api/app";
import { getDatasets } from "@app/lib/api/datasets";
import { Authenticator, getSession } from "@app/lib/auth";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { classNames } from "@app/lib/utils";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

const { GA_TRACKING_ID = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
subscription: SubscriptionType;
readOnly: boolean;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/w/[wId]/a/[aId]/datasets/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { getApp } from "@app/lib/api/app";
import { getDatasets } from "@app/lib/api/datasets";
import { Authenticator, getSession } from "@app/lib/auth";
import { useRegisterUnloadHandlers } from "@app/lib/front";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";

const { GA_TRACKING_ID = "" } = process.env;

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
subscription: SubscriptionType;
app: AppType;
Expand Down
4 changes: 2 additions & 2 deletions front/pages/w/[wId]/a/[aId]/execute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ import {
getDatasetTypes,
getValueType,
} from "@app/lib/datasets";
import { withGetServerSidePropsRequirements } from "@app/lib/iam/session";
import { useSavedRunStatus } from "@app/lib/swr";
import { classNames } from "@app/lib/utils";
import { withGetServerSidePropsLogging } from "@app/logger/withlogging";

const CodeEditor = dynamic(
() => import("@uiw/react-textarea-code-editor").then((mod) => mod.default),
Expand All @@ -57,7 +57,7 @@ type Event = {
};
};

export const getServerSideProps = withGetServerSidePropsLogging<{
export const getServerSideProps = withGetServerSidePropsRequirements<{
owner: WorkspaceType;
subscription: SubscriptionType;
app: AppType;
Expand Down
Loading

0 comments on commit 744c8ac

Please sign in to comment.