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

Support workspace with SSO enforced #4227

Merged
merged 3 commits into from
Mar 8, 2024
Merged
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
1 change: 1 addition & 0 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ export class Authenticator {
ACTIVATE_ALL_FEATURES_DEV && isDevelopment()
? [...WHITELISTABLE_FEATURES]
: this._flags,
ssoEnforced: this._workspace.ssoEnforced,
}
: null;
}
Expand Down
49 changes: 38 additions & 11 deletions front/lib/iam/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RoleType, UserTypeWithWorkspaces } from "@dust-tt/types";
import { isEnterpriseConnectionSub } from "@dust-tt/types";
import type {
GetServerSidePropsContext,
GetServerSidePropsResult,
Expand Down Expand Up @@ -103,6 +104,22 @@ export type CustomGetServerSideProps<
session: RequireUserPrivilege extends "none" ? null : SessionWithUser
) => Promise<GetServerSidePropsResult<Props>>;

export function statisfiesEnforceEntrepriseConnection(
auth: Authenticator,
session: SessionWithUser
) {
const owner = auth.workspace();
if (!owner) {
return true;
}

if (owner.ssoEnforced) {
return isEnterpriseConnectionSub(session.user.sub);
}

return true;
}

async function getAuthenticator(
context: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>,
session: SessionWithUser | null,
Expand Down Expand Up @@ -156,17 +173,27 @@ export function makeGetServerSidePropsRequirementsWrapper<
requireUserPrivilege
);

if (
requireUserPrivilege !== "none" &&
(!session || !isValidSession(session))
) {
return {
redirect: {
permanent: false,
// TODO(2024-03-04 flav) Add support for `returnTo=`.
destination: "/api/auth/login",
},
};
if (requireUserPrivilege !== "none") {
if (!session || !isValidSession(session)) {
return {
redirect: {
permanent: false,
// TODO(2024-03-04 flav) Add support for `returnTo=`.
destination: "/api/auth/login",
},
};
}

// Validate the user's session to guarantee compliance with the workspace's SSO requirements when SSO is enforced.
if (auth && !statisfiesEnforceEntrepriseConnection(auth, session)) {
return {
redirect: {
permanent: false,
// TODO(2024-03-04 flav) Add support for `returnTo=`.
destination: `/sso-enforced?workspaceId=${auth.workspace()?.sId}`,
},
};
}
}

const userSession = session as RequireUserPrivilege extends "none"
Expand Down
5 changes: 5 additions & 0 deletions front/lib/models/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class Workspace extends Model<
declare name: string;
declare description: string | null;
declare segmentation: WorkspaceSegmentationType;
declare ssoEnforced?: boolean;
declare subscriptions: NonAttribute<Subscription[]>;
}
Workspace.init(
Expand Down Expand Up @@ -63,6 +64,10 @@ Workspace.init(
type: DataTypes.STRING,
allowNull: true,
},
ssoEnforced: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
},
{
modelName: "workspace",
Expand Down
1 change: 1 addition & 0 deletions types/src/front/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type LightWorkspaceType = {

export type WorkspaceType = LightWorkspaceType & {
flags: WhitelistableFeature[];
ssoEnforced?: boolean;
};

export type UserProviderType = "github" | "google" | null;
Expand Down
12 changes: 12 additions & 0 deletions types/src/front/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ export interface WorkspaceEnterpriseConnection {
}

export type SupportedEnterpriseConnectionStrategies = "okta";
export const supportedEnterpriseConnectionStrategies: SupportedEnterpriseConnectionStrategies[] =
["okta"];

export function isEnterpriseConnectionSub(
sub: string
): sub is SupportedEnterpriseConnectionStrategies {
const [provider] = sub.split("|");

return supportedEnterpriseConnectionStrategies.includes(
provider as SupportedEnterpriseConnectionStrategies
);
}
Loading