Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(auth): require a verified email before linking an OIDC account

Atlas (by M. Pfennig)7eebc2fa2dab
diff --git a/apps/web/app/(org)/login/form.tsx b/apps/web/app/(org)/login/form.tsx
index 014d5f5bc..63521755f 100644
--- a/apps/web/app/(org)/login/form.tsx
+++ b/apps/web/app/(org)/login/form.tsx
@@ -442,6 +442,7 @@ export function LoginForm() {
loading={loading}
oauthError={oauthError}
handleGoogleSignIn={handleGoogleSignIn}
+ handleOidcSignIn={handleOidcSignIn}
/>
</motion.form>
)}
@@ -542,6 +543,7 @@ const NormalLogin = ({
loading,
oauthError,
handleGoogleSignIn,
+ handleOidcSignIn,
}: {
setShowOrgInput: (show: boolean) => void;
email: string;
@@ -550,6 +552,7 @@ const NormalLogin = ({
loading: boolean;
oauthError: boolean;
handleGoogleSignIn: () => void;
+ handleOidcSignIn: () => void;
}) => {
const publicEnv = usePublicEnv();
const emailInputId = useId();
diff --git a/packages/database/auth/auth-options.ts b/packages/database/auth/auth-options.ts
index ebd9e3527..8d9c00f72 100644
--- a/packages/database/auth/auth-options.ts
+++ b/packages/database/auth/auth-options.ts
@@ -60,6 +60,17 @@ export async function decodeSessionToken(
return token;
}
+const OIDC_PROVIDER_ID = "oidc";
+
+type OidcClaims = {
+ sub: string;
+ email?: string;
+ email_verified?: boolean;
+ name?: string;
+ preferred_username?: string;
+ picture?: string;
+};
+
export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
let _adapter: Adapter | undefined;
let _providers: Provider[] | undefined;
@@ -94,13 +105,10 @@ export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
const oidcClientId = serverEnv().OIDC_CLIENT_ID;
const oidcClientSecret = serverEnv().OIDC_CLIENT_SECRET;
_providers = [
- // Any OpenID Connect provider, described by its discovery document.
- // Email linking matches the WorkOS provider below: an operator who
- // configures this controls the issuer and its email claim.
...(oidcIssuer && oidcClientId && oidcClientSecret
? [
{
- id: "oidc",
+ id: OIDC_PROVIDER_ID,
name: serverEnv().OIDC_NAME || "SSO",
type: "oauth" as const,
wellKnown: `${oidcIssuer.replace(/\/$/, "")}/.well-known/openid-configuration`,
@@ -110,16 +118,19 @@ export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
idToken: true,
checks: ["state" as const, "pkce" as const],
authorization: { params: { scope: "openid email profile" } },
+ // Linking is gated on a verified email in the signIn callback,
+ // because a generic issuer is not known to verify addresses.
allowDangerousEmailAccountLinking: true,
- profile(profile: Record<string, any>) {
+ profile(profile: OidcClaims) {
+ const email = profile.email?.trim().toLowerCase();
return {
id: profile.sub,
name:
profile.name ||
profile.preferred_username ||
- profile.email?.split("@")[0] ||
+ email?.split("@")[0] ||
profile.sub,
- email: profile.email?.trim().toLowerCase(),
+ email,
image: profile.picture ?? null,
};
},
@@ -245,6 +256,12 @@ export const authOptions = (ssoContext?: SsoAuthContext): NextAuthOptions => {
},
callbacks: {
async signIn({ user, email, credentials, account, profile }) {
+ if (account?.provider === OIDC_PROVIDER_ID) {
+ const claims = profile as OidcClaims | undefined;
+ if (!claims?.email || claims.email_verified !== true) {
+ return "/login?error=OidcEmailUnverified";
+ }
+ }
if (account?.provider === "workos") {
validatedSsoIdentity = null;
try {
diff --git a/packages/env/server.ts b/packages/env/server.ts
index b9c51a725..353f1f90a 100644
--- a/packages/env/server.ts
+++ b/packages/env/server.ts
@@ -76,13 +76,9 @@ function createServerEnv() {
WORKOS_API_KEY: z.string().optional(),
/// Generic OIDC
- // Provide all three to add a sign-in button for any OpenID Connect
- // provider. OIDC_ISSUER is the base URL whose
- // /.well-known/openid-configuration describes the endpoints.
OIDC_ISSUER: z.string().optional(),
OIDC_CLIENT_ID: z.string().optional(),
OIDC_CLIENT_SECRET: z.string().optional(),
- // Label for the sign-in button. Defaults to "SSO".
OIDC_NAME: z.string().optional(),
/// Settings