Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(auth): add AzureAD (#396)

Marc Klingenff322704643a
diff --git a/package-lock.json b/package-lock.json
index f59fcac989..76fc432114 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -13395,7 +13395,7 @@
},
"node_modules/nodemailer": {
"version": "6.9.7",
- "resolved": "https://registry.npmmirror.com/nodemailer/-/nodemailer-6.9.7.tgz",
+ "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.7.tgz",
"integrity": "sha512-rUtR77ksqex/eZRLmQ21LKVH5nAAsVicAtAYudK7JgwenEDZ0UIQ1adUGqErz7sMkWYxWTTU1aeP2Jga6WQyJw==",
"engines": {
"node": ">=6.0.0"
diff --git a/prisma/migrations/20231223230008_accounts_add_cols_azure_ad_auth/migration.sql b/prisma/migrations/20231223230008_accounts_add_cols_azure_ad_auth/migration.sql
new file mode 100644
index 0000000000..4e65067ce2
--- /dev/null
+++ b/prisma/migrations/20231223230008_accounts_add_cols_azure_ad_auth/migration.sql
@@ -0,0 +1,3 @@
+-- AlterTable
+ALTER TABLE "Account" ADD COLUMN "expires_in" INTEGER,
+ADD COLUMN "ext_expires_in" INTEGER;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 0e5f527fbd..6a48916c72 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -29,6 +29,8 @@ model Account {
refresh_token String? // @db.Text
access_token String? // @db.Text
expires_at Int?
+ expires_in Int?
+ ext_expires_in Int?
token_type String?
scope String?
id_token String? // @db.Text
diff --git a/src/env.mjs b/src/env.mjs
index 21650a6319..7955868b36 100644
--- a/src/env.mjs
+++ b/src/env.mjs
@@ -43,6 +43,9 @@ export const env = createEnv({
AUTH_GOOGLE_CLIENT_SECRET: z.string().optional(),
AUTH_GITHUB_CLIENT_ID: z.string().optional(),
AUTH_GITHUB_CLIENT_SECRET: z.string().optional(),
+ AUTH_AZURE_AD_CLIENT_ID: z.string().optional(),
+ AUTH_AZURE_AD_CLIENT_SECRET: z.string().optional(),
+ AUTH_AZURE_AD_TENANT_ID: z.string().optional(),
AUTH_DOMAINS_WITH_SSO_ENFORCEMENT: z.string().optional(),
AUTH_DISABLE_USERNAME_PASSWORD: z.enum(["true", "false"]).optional(),
// EMAIL
@@ -99,6 +102,9 @@ export const env = createEnv({
AUTH_GOOGLE_CLIENT_SECRET: process.env.AUTH_GOOGLE_CLIENT_SECRET,
AUTH_GITHUB_CLIENT_ID: process.env.AUTH_GITHUB_CLIENT_ID,
AUTH_GITHUB_CLIENT_SECRET: process.env.AUTH_GITHUB_CLIENT_SECRET,
+ AUTH_AZURE_AD_CLIENT_ID: process.env.AUTH_AZURE_AD_CLIENT_ID,
+ AUTH_AZURE_AD_CLIENT_SECRET: process.env.AUTH_AZURE_AD_CLIENT_SECRET,
+ AUTH_AZURE_AD_TENANT_ID: process.env.AUTH_AZURE_AD_TENANT_ID,
AUTH_DOMAINS_WITH_SSO_ENFORCEMENT:
process.env.AUTH_DOMAINS_WITH_SSO_ENFORCEMENT,
AUTH_DISABLE_USERNAME_PASSWORD: process.env.AUTH_DISABLE_USERNAME_PASSWORD,
diff --git a/src/pages/auth/sign-in.tsx b/src/pages/auth/sign-in.tsx
index f883750969..3e8c306790 100644
--- a/src/pages/auth/sign-in.tsx
+++ b/src/pages/auth/sign-in.tsx
@@ -14,6 +14,7 @@ import { env } from "@/src/env.mjs";
import { zodResolver } from "@hookform/resolvers/zod";
import { FcGoogle } from "react-icons/fc";
import { FaGithub } from "react-icons/fa";
+import { TbBrandAzure } from "react-icons/tb";
import { signIn } from "next-auth/react";
import Head from "next/head";
import Link from "next/link";
@@ -38,6 +39,7 @@ export type PageProps = {
credentials: boolean;
google: boolean;
github: boolean;
+ azureAd: boolean;
};
};
@@ -54,6 +56,10 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async () => {
env.AUTH_GITHUB_CLIENT_ID !== undefined &&
env.AUTH_GITHUB_CLIENT_SECRET !== undefined,
credentials: env.AUTH_DISABLE_USERNAME_PASSWORD !== "true",
+ azureAd:
+ env.AUTH_AZURE_AD_CLIENT_ID !== undefined &&
+ env.AUTH_AZURE_AD_CLIENT_SECRET !== undefined &&
+ env.AUTH_AZURE_AD_TENANT_ID !== undefined,
},
},
};
@@ -72,11 +78,9 @@ export function SSOButtons({
([name, enabled]) => enabled && name !== "credentials",
) ? (
<div>
- {authProviders.credentials ? (
- <Divider className="text-gray-400" />
- ) : null}
+ {authProviders.credentials && <Divider className="text-gray-400" />}
<div className="flex flex-row flex-wrap items-center justify-center gap-4">
- {authProviders.google ? (
+ {authProviders.google && (
<Button
onClick={() => {
posthog.capture("sign_in:google_button_click");
@@ -87,8 +91,8 @@ export function SSOButtons({
<FcGoogle className="mr-3" size={18} />
{action} with Google
</Button>
- ) : null}
- {authProviders.github ? (
+ )}
+ {authProviders.github && (
<Button
onClick={() => {
posthog.capture("sign_in:github_button_click");
@@ -99,7 +103,19 @@ export function SSOButtons({
<FaGithub className="mr-3" size={18} />
{action} with Github
</Button>
- ) : null}
+ )}
+ {authProviders.azureAd && (
+ <Button
+ onClick={() => {
+ posthog.capture("sign_in:azure_ad_button_click");
+ void signIn("azure-ad");
+ }}
+ variant="secondary"
+ >
+ <TbBrandAzure className="mr-3" size={18} />
+ {action} with Azure AD
+ </Button>
+ )}
</div>
</div>
) : null
diff --git a/src/server/auth.ts b/src/server/auth.ts
index 06cf9c6a14..080ba83859 100644
--- a/src/server/auth.ts
+++ b/src/server/auth.ts
@@ -18,6 +18,7 @@ import { type Provider } from "next-auth/providers";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";
import GitHubProvider from "next-auth/providers/github";
+import AzureADProvider from "next-auth/providers/azure-ad";
// Use secure cookies on https hostnames, exception for Vercel which sets NEXTAUTH_URL without the protocol
const useSecureCookies =
@@ -115,6 +116,19 @@ if (env.AUTH_GITHUB_CLIENT_ID && env.AUTH_GITHUB_CLIENT_SECRET)
}),
);
+if (
+ env.AUTH_AZURE_AD_CLIENT_ID &&
+ env.AUTH_AZURE_AD_CLIENT_SECRET &&
+ env.AUTH_AZURE_AD_TENANT_ID
+)
+ providers.push(
+ AzureADProvider({
+ clientId: env.AUTH_AZURE_AD_CLIENT_ID,
+ clientSecret: env.AUTH_AZURE_AD_CLIENT_SECRET,
+ tenantId: env.AUTH_AZURE_AD_TENANT_ID,
+ }),
+ );
+
// Extend Prisma Adapter
const prismaAdapter = PrismaAdapter(prisma);
const extendedPrismaAdapter: Adapter = {
@@ -125,6 +139,12 @@ const extendedPrismaAdapter: Adapter = {
if (env.NEXT_PUBLIC_SIGN_UP_DISABLED === "true") {
throw new Error("Sign up is disabled.");
}
+ if (!profile.email) {
+ throw new Error(
+ "Cannot create db user as login profile does not contain an email: " +
+ JSON.stringify(profile),
+ );
+ }
const user = await prismaAdapter.createUser(profile);