Skip to content

Commit

Permalink
Improve SSO login flow (#4206)
Browse files Browse the repository at this point in the history
* Improve SSO login flow

* Expose initiate login url in SSO drawer

* ✨
  • Loading branch information
flvndvd authored Mar 7, 2024
1 parent 676b0ec commit 3bb45e1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
23 changes: 14 additions & 9 deletions front/components/workspace/connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ interface EnterpriseConnectionDetailsProps {
}

export interface EnterpriseConnectionStrategyDetails {
strategy: SupportedEnterpriseConnectionStrategies;
callbackUrl: string;
initiateLoginUrl: string;
strategy: SupportedEnterpriseConnectionStrategies;
}

export function EnterpriseConnectionDetails({
Expand Down Expand Up @@ -170,7 +171,7 @@ function CreateOktaEnterpriseConnectionModal({
domain?: string;
}>({});

const { callbackUrl } = strategyDetails;
const { callbackUrl, initiateLoginUrl } = strategyDetails;

const sendNotification = useContext(SendNotificationsContext);

Expand Down Expand Up @@ -225,15 +226,19 @@ function CreateOktaEnterpriseConnectionModal({
Callback URL:
<Input
name="Callback URL"
placeholder="callback url"
placeholder="Callback url"
value={callbackUrl}
disabled={true}
onChange={(value) =>
setEnterpriseConnectionDetails({
...enterpriseConnectionDetails,
domain: value,
})
}
className="max-w-sm"
/>
</Page.P>
<Page.P>
Initiate login URI:
<Input
name="Initiate login URI"
placeholder="Initiate login URI"
value={initiateLoginUrl}
disabled={true}
className="max-w-sm"
/>
</Page.P>
Expand Down
3 changes: 3 additions & 0 deletions front/lib/api/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EnvironmentConfig } from "@dust-tt/types";

const config = {
getAppUrl: (): string => {
return EnvironmentConfig.getEnvVariable("URL");
},
getAuth0TenantUrl: (): string => {
return EnvironmentConfig.getEnvVariable("AUTH0_TENANT_DOMAIN_URL");
},
Expand Down
30 changes: 22 additions & 8 deletions front/lib/api/enterprise_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ import { ManagementClient } from "auth0";
import config from "@app/lib/api/config";
import type { Authenticator } from "@app/lib/auth";

const management = new ManagementClient({
domain: config.getAuth0TenantUrl(),
clientId: config.getAuth0M2MClientId(),
clientSecret: config.getAuth0M2MClientSecret(),
});
let auth0ManagemementClient: ManagementClient | null = null;

function getAuth0ManagemementClient(): ManagementClient {
if (!auth0ManagemementClient) {
auth0ManagemementClient = new ManagementClient({
domain: config.getAuth0TenantUrl(),
clientId: config.getAuth0M2MClientId(),
clientSecret: config.getAuth0M2MClientSecret(),
});
}

return auth0ManagemementClient;
}

function makeEnterpriseConnectionName(workspaceId: string) {
return `workspace-${workspaceId}`;
}

export function makeEnterpriseConnectionInitiateLoginUrl(workspaceId: string) {
return `${config.getAppUrl()}/api/auth/login?connection=${makeEnterpriseConnectionName(
workspaceId
)}`;
}

export async function getEnterpriseConnectionForWorkspace(
auth: Authenticator,
strategy: SupportedEnterpriseConnectionStrategies = "okta"
Expand All @@ -26,7 +40,7 @@ export async function getEnterpriseConnectionForWorkspace(

// This endpoint supports fetching up to 1000 connections in one page.
// In the future, consider implementing pagination to handle larger datasets.
const connections = await management.connections.getAll({
const connections = await getAuth0ManagemementClient().connections.getAll({
strategy: [strategy],
});

Expand Down Expand Up @@ -54,7 +68,7 @@ export async function createEnterpriseConnection(
}

const { sId } = owner;
const connection = await management.connections.create({
const connection = await getAuth0ManagemementClient().connections.create({
name: makeEnterpriseConnectionName(sId),
display_name: makeEnterpriseConnectionName(sId),
strategy: connectionDetails.strategy,
Expand Down Expand Up @@ -93,7 +107,7 @@ export async function deleteEnterpriseConnection(
throw new Error("Enterprise connection not found.");
}

return management.connections.delete({
return getAuth0ManagemementClient().connections.delete({
id: existingConnection.id,
});
}
18 changes: 15 additions & 3 deletions front/pages/api/auth/[auth0].ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { handleAuth, handleLogin } from "@auth0/nextjs-auth0";
import type { AuthorizationParameters } from "@auth0/nextjs-auth0/dist/auth0-session";

export default handleAuth({
login: handleLogin({
authorizationParams: {
login: handleLogin((req) => {
const connection = "query" in req ? req.query.connection : undefined;

const defaultAuthorizationParams: Partial<AuthorizationParameters> = {
scope: "openid profile email",
},
};

// Set the Auth0 connection based on the provided connection param, redirecting the user to the correct screen.
if (connection) {
defaultAuthorizationParams.connection = connection;
}

return {
authorizationParams: defaultAuthorizationParams,
};
}),
});
5 changes: 4 additions & 1 deletion front/pages/w/[wId]/members/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { SendNotificationsContext } from "@app/components/sparkle/Notification";
import type { EnterpriseConnectionStrategyDetails } from "@app/components/workspace/connection";
import { EnterpriseConnectionDetails } from "@app/components/workspace/connection";
import config from "@app/lib/api/config";
import { makeEnterpriseConnectionInitiateLoginUrl } from "@app/lib/api/enterprise_connection";
import {
checkWorkspaceSeatAvailabilityUsingAuth,
getWorkspaceVerifiedDomain,
Expand Down Expand Up @@ -80,9 +81,11 @@ export const getServerSideProps = withDefaultGetServerSidePropsRequirements<{

const enterpriseConnectionStrategyDetails: EnterpriseConnectionStrategyDetails =
{
strategy: "okta",
callbackUrl: config.getAuth0TenantUrl(),
initiateLoginUrl: makeEnterpriseConnectionInitiateLoginUrl(owner.sId),
strategy: "okta",
};

return {
props: {
user,
Expand Down

0 comments on commit 3bb45e1

Please sign in to comment.