@better-auth/sso 1.4.6-beta.2 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/oidc.test.ts CHANGED
@@ -571,3 +571,167 @@ describe("provisioning", async (ctx) => {
571
571
  expect(res.url).toContain("http://localhost:8080/authorize");
572
572
  });
573
573
  });
574
+
575
+ describe("OIDC account linking with domainVerified", async () => {
576
+ const { auth, signInWithTestUser, customFetchImpl, cookieSetter } =
577
+ await getTestInstance({
578
+ account: {
579
+ accountLinking: {
580
+ enabled: true,
581
+ trustedProviders: [],
582
+ },
583
+ },
584
+ plugins: [
585
+ sso({
586
+ domainVerification: {
587
+ enabled: true,
588
+ },
589
+ }),
590
+ ],
591
+ });
592
+
593
+ const authClient = createAuthClient({
594
+ plugins: [ssoClient()],
595
+ baseURL: "http://localhost:3000",
596
+ fetchOptions: {
597
+ customFetchImpl,
598
+ },
599
+ });
600
+
601
+ beforeAll(async () => {
602
+ await server.issuer.keys.generate("RS256");
603
+ await server.start(8080, "localhost");
604
+ });
605
+
606
+ afterAll(async () => {
607
+ await server.stop().catch(() => {});
608
+ });
609
+
610
+ async function simulateOAuthFlow(authUrl: string, headers: Headers) {
611
+ let location: string | null = null;
612
+ await betterFetch(authUrl, {
613
+ method: "GET",
614
+ redirect: "manual",
615
+ onError(context) {
616
+ location = context.response.headers.get("location");
617
+ },
618
+ });
619
+
620
+ if (!location) throw new Error("No redirect location found");
621
+
622
+ let callbackURL = "";
623
+ const newHeaders = new Headers();
624
+ await betterFetch(location, {
625
+ method: "GET",
626
+ customFetchImpl,
627
+ headers,
628
+ onError(context) {
629
+ callbackURL = context.response.headers.get("location") || "";
630
+ cookieSetter(newHeaders)(context);
631
+ },
632
+ });
633
+
634
+ return { callbackURL, headers: newHeaders };
635
+ }
636
+
637
+ it("should allow account linking when domain is verified and email domain matches", async () => {
638
+ const testEmail = "linking-test@verified-oidc.com";
639
+ const testDomain = "verified-oidc.com";
640
+
641
+ server.service.on("beforeTokenSigning", (token) => {
642
+ token.payload.email = testEmail;
643
+ token.payload.email_verified = false;
644
+ token.payload.name = "Domain Verified User";
645
+ token.payload.sub = "oidc-domain-verified-user";
646
+ });
647
+
648
+ const { headers } = await signInWithTestUser();
649
+
650
+ const provider = await auth.api.registerSSOProvider({
651
+ body: {
652
+ providerId: "domain-verified-oidc",
653
+ issuer: server.issuer.url!,
654
+ domain: testDomain,
655
+ oidcConfig: {
656
+ clientId: "test",
657
+ clientSecret: "test",
658
+ authorizationEndpoint: `${server.issuer.url}/authorize`,
659
+ tokenEndpoint: `${server.issuer.url}/token`,
660
+ jwksEndpoint: `${server.issuer.url}/jwks`,
661
+ discoveryEndpoint: `${server.issuer.url}/.well-known/openid-configuration`,
662
+ mapping: {
663
+ id: "sub",
664
+ email: "email",
665
+ emailVerified: "email_verified",
666
+ name: "name",
667
+ },
668
+ },
669
+ },
670
+ headers,
671
+ });
672
+
673
+ expect(provider.domainVerified).toBe(false);
674
+
675
+ const ctx = await auth.$context;
676
+ await ctx.adapter.update({
677
+ model: "ssoProvider",
678
+ where: [{ field: "providerId", value: provider.providerId }],
679
+ update: {
680
+ domainVerified: true,
681
+ },
682
+ });
683
+
684
+ const updatedProvider = await ctx.adapter.findOne<{
685
+ domainVerified: boolean;
686
+ domain: string;
687
+ }>({
688
+ model: "ssoProvider",
689
+ where: [{ field: "providerId", value: provider.providerId }],
690
+ });
691
+ expect(updatedProvider?.domainVerified).toBe(true);
692
+
693
+ await ctx.adapter.create({
694
+ model: "user",
695
+ data: {
696
+ id: "existing-oidc-domain-user",
697
+ email: testEmail,
698
+ name: "Existing User",
699
+ emailVerified: true,
700
+ createdAt: new Date(),
701
+ updatedAt: new Date(),
702
+ },
703
+ forceAllowId: true,
704
+ });
705
+
706
+ const newHeaders = new Headers();
707
+ const res = await authClient.signIn.sso({
708
+ providerId: "domain-verified-oidc",
709
+ callbackURL: "/dashboard",
710
+ fetchOptions: {
711
+ throw: true,
712
+ onSuccess: cookieSetter(newHeaders),
713
+ },
714
+ });
715
+
716
+ expect(res.url).toContain("http://localhost:8080/authorize");
717
+
718
+ const { callbackURL } = await simulateOAuthFlow(res.url, newHeaders);
719
+
720
+ expect(callbackURL).toContain("/dashboard");
721
+ expect(callbackURL).not.toContain("error");
722
+
723
+ const accounts = await ctx.adapter.findMany<{
724
+ providerId: string;
725
+ accountId: string;
726
+ userId: string;
727
+ }>({
728
+ model: "account",
729
+ where: [{ field: "userId", value: "existing-oidc-domain-user" }],
730
+ });
731
+ const linkedAccount = accounts.find(
732
+ (a) => a.providerId === "domain-verified-oidc",
733
+ );
734
+ expect(linkedAccount).toBeTruthy();
735
+ expect(linkedAccount?.accountId).toBe("oidc-domain-verified-user");
736
+ });
737
+ });
@@ -8,14 +8,16 @@ import { generateRandomString } from "better-auth/crypto";
8
8
  import * as z from "zod/v4";
9
9
  import type { SSOOptions, SSOProvider } from "../types";
10
10
 
11
+ const domainVerificationBodySchema = z.object({
12
+ providerId: z.string(),
13
+ });
14
+
11
15
  export const requestDomainVerification = (options: SSOOptions) => {
12
16
  return createAuthEndpoint(
13
17
  "/sso/request-domain-verification",
14
18
  {
15
19
  method: "POST",
16
- body: z.object({
17
- providerId: z.string(),
18
- }),
20
+ body: domainVerificationBodySchema,
19
21
  metadata: {
20
22
  openapi: {
21
23
  summary: "Request a domain verification",
@@ -127,9 +129,7 @@ export const verifyDomain = (options: SSOOptions) => {
127
129
  "/sso/verify-domain",
128
130
  {
129
131
  method: "POST",
130
- body: z.object({
131
- providerId: z.string(),
132
- }),
132
+ body: domainVerificationBodySchema,
133
133
  metadata: {
134
134
  openapi: {
135
135
  summary: "Verify the provider domain ownership",